MonthCalendar on another form

S

SePp

Hi there,

I have a WindowsForm which includes a label. I want to add a date to
this label which a user can select.

I thought it is probably the best to open another WindowsForm and to
add on this one a monthcalendar.

My prolbem is: How can I use this MonthcalendarForm to edit / add the
date of the label on other forms.
(It should be possible to use this monthcalendarform for diffrent
forms)

Hope you can understand my problem!

Thank you very much in advance for your help.

Kind regards,

SePp
 
M

Morten Wennevik [C# MVP]

Hi SePp,

The MonthCalendar form needs to know the date by receiving it in its
constructor or by exposing a public property the parent form can set before
displaying the MonthCalendar form. When the form closes, the parent can read
a property on the MonthCalendar form to find out which value the user
selected. Typically the code would like something like the below (will
probably need correction to work)

// Parent form

public void button1_Click(object sender, EventArgs e)
{
DateTime date = DateTime.Parse(label1.Text);

MonthCalendarForm mcFom = new MonthCalendarForm();
form.Date = date;

if(form.ShowDialog() == DialogResult.OK)
{
label1.Text = form.Date.ToString();
}
}

// MonthCalendarForm

private DateTime _date

public DateTime Date
{
get{ return _date;}
set{ _date = value;}
}

protected override void OnLoad(EventArgs e)
{
monthCalendar1.Date = Date;
}

public void button1_Click(object sender, EventArgs e)
{
Date = monthCalendar1.Date;
DialogResult = DialogResult.OK;
Close();
}
 
S

SePp

Hi SePp,

The MonthCalendar form needs to know the date by receiving it in its
constructor or by exposing a public property the parent form can set before
displaying the MonthCalendar form. When the form closes, the parent can read
a property on the MonthCalendar form to find out which value the user
selected. Typically the code would like something like the below (will
probably need correction to work)

// Parent form

public void button1_Click(object sender, EventArgs e)
{
DateTime date = DateTime.Parse(label1.Text);

MonthCalendarForm mcFom = new MonthCalendarForm();
form.Date = date;

if(form.ShowDialog() == DialogResult.OK)
{
label1.Text = form.Date.ToString();
}

}

// MonthCalendarForm

private DateTime _date

public DateTime Date
{
get{ return _date;}
set{ _date = value;}

}

protected override void OnLoad(EventArgs e)
{
monthCalendar1.Date = Date;

}

public void button1_Click(object sender, EventArgs e)
{
Date = monthCalendar1.Date;
DialogResult = DialogResult.OK;
Close();

}

--
Happy Coding!
Morten Wennevik [C# MVP]

SePp said:
Hi there,
I have a WindowsForm which includes a label. I want to add a date to
this label which a user can select.
I thought it is probably the best to open another WindowsForm and to
add on this one a monthcalendar.
My prolbem is: How can I use this MonthcalendarForm to edit / add the
date of the label on other forms.
(It should be possible to use this monthcalendarform for diffrent
forms)
Hope you can understand my problem!
Thank you very much in advance for your help.
Kind regards,

Hi Morten,

Thank you very much for helping!

Greets
SePp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top