show-notice
hide-notice

Friday 5 July 2013

Child to Child Form Display month using MonthCalendar




Questions


I want to display a selected month in words and not in numbers. Both Form1 and Form2 are child forms of parent MasterForm. Form1 has a MonthCalendar and a button called btnCreate. User will select a month and click the button. After that, Form2 will appear and display the selected month on the Form title.
The code below displays a selected month, in numbers


private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
    {
        this.Text = "Selected Month: " + e.Start.Month;
    }





Answers

e.Start is a DateTime object so you can format its string override and, optionally, pass in a particular culture.

private void monthCalendar1_DateChanged(object sender, DateRangeEventArgs e)
{
this.Text = "Selected Month: " + e.Start.ToString("MMMM", CultureInfo.InvariantCulture));
}






Then when you open that form, pass in the DateTime object from the calendar

Form2 f2 = new Form2(dtObjectFromCalendar);
f2.ShowDialog();



Form2 can then display the month name as shown above.
As per comment, you can pass a string instead.
public Form2(string textToDisplay)
{

    this.Text = textToDisplay;
}






You can then call that form like this (assuming you keep the code in your question)

//this.Text because you set the value of this.Text in your question
Form2 f2 = new Form2(this.Text);
f2.ShowDialog();

SHARE THIS POST   

0 comments :

Post a Comment

Design by Gohilinfotech | www.gohilinfotech.blogspot.com