문제

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;
}

That will display a selected month in Form1 but I want to make it display in Form2?

도움이 되었습니까?

해결책

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));
}

To get it to appear on the next form, you can update Form2's constructor.

private DateTime _dt
public Form2(DateTime dt)
{
    _dt = dt;
    this.Text = dt.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();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top