Ankit Gupta
Ankit Gupta

Reputation: 33

Get Date in Text Box from month calendar with button in c#

I want to use month calendar date in text box after click on button for window form. Calendar should be hide when form open and after click on button that calendar be visible.

I am using c# with visual studio 2008.So please give me response according this tool.

Upvotes: 1

Views: 13534

Answers (2)

bitbonk
bitbonk

Reputation: 49609

Use the DateTimePicker class for this. It has exactly the behavior you described.

Upvotes: 1

Ria
Ria

Reputation: 10367

Try to use Control.Show() method and Control.Visible property:

private MonthCalendar _monthCalendar;

public Form1()
{
    InitializeComponent();
    // invisible on startup
    _monthCalendar.Visible = false;
    _monthCalendar.MaxSelectionCount = 1;
}

private void button1_Click(object sender, EventArgs e)
{
    //show when needed
    _monthCalendar.Show();
}   

private void textBox1_Enter(object sender, EventArgs e)
{
    _monthCalendar.Show();
}

private void textBox1_Leave(object sender, EventArgs e)
{
    if (!_monthCalendar.Focused)
        _monthCalendar.Visible = false;
}

private void monthCalendar_DateSelected(object sender, DateRangeEventArgs e)
{
    var monthCalendar = sender as MonthCalendar;
    textBox1.Text = monthCalendar.SelectionStart.ToString();
}

private void monthCalendar_Leave(object sender, EventArgs e)
{
    var monthCalendar = sender as MonthCalendar;
    monthCalendar.Visible = false;
}

Upvotes: 0

Related Questions