Click here to Skip to main content
15,312,347 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello all,
my question is, I have a daytime picker in my form1, when the usser pick a date that will be added or subtracted by 10 or 15 or ... then, i want the value in the second form. I have fom1 one and 2, possiblely on the lable in the form2.

I have tried, but no result.

with many thanxs

/B
Posted
Comments
[no name] 23-Mar-12 18:40pm
   
What environment? Windows or Web?

Hello

I suppose you want it for WinForm.

For example like this:
C#
Form form2 = new Form2();
this.AddOwnedForm(form2);
form2.Show();


In Form2:

define a delegate

C#
public delegate void DatePickedHandler(DateTime value);
public DatePickedHandler onDatePicked;


Then

In ValueChanged event of the DateTimeControl:

C#
if (onDatePicked != null)
    onDatePicked(this.MyDateTimePicker.Value);



In Form1:

Define a handler for the delegate.
for example:

C#
Form form2 = new Form2();
this.AddOwnedForm(form2);
form2.onDatePicked += new Form1.DatePickedHandler(ChangeValue);
form2.Show();


And define ChengeValue method:

C#
private void ChangeValue(DateTime value)
{
  //some code that do something you want...
}



If DateTimePicker is in form1:
1. Define a public method in form2 (For example MyMethod(DateTime value))
2. in ValueChanged event of the DateTimePicker call form2.MyMethod(the value)
   
v4
Comments
Sergey Alexandrovich Kryukov 23-Mar-12 21:07pm
   
Shahin, I don't know how much should I repeat: none of those forms are child or parent of each other. In all normal situations, parent-child relationship between forms is defunct and not used (I don't mean MDI here, but this is different thing; also, who needs MDI?). If this is not clear, I can explain.
--SA
Shahin Khorshidnia 23-Mar-12 21:24pm
   
Hello SA
Of course Form2 is not a child of Form1. My intention of naming child and parent is:
I have Form1 and Form2 and I am creating and showing a new instance of Form2, form an instance of Form1.
Form2 form2 = new Form2();
form2.Show();
I didn't mean from the view point of object oriented sence (Form2 is not child of Form1). Maybe "Master and secondary" is a better explanation.
Sergey Alexandrovich Kryukov 23-Mar-12 23:40pm
   
That is good that you understand it, but this terminology is very confusing, because the form has the property Control.Parent, and trying to make one form a parent of another cause exception, but possible if you preliminary assign TopLevel = false. In this way, a form will really appear inside another form like a control, which is, of course, very bad. Do you see how many complications? I suggest you fix this, to avoid such confusion.

What's more, showing a form like that is pretty bad. A second (non-main) form should be added as AddOwnedForm. The difference is little, but very essential: if you make all forms owned by, say, main form (except the main form itself), they are activated much, much more accurately; they go all together in Z dimension (no "foreign" form can get in between). Did you noticed that?

Another important thing is "ShoInTaskbar" (it's the best to allow only for the main form, but there are exclusions).

In other words, non-main form need extra care; and it does not come by default (in my opinion, unfortunately).

As to the this question, it is absolutely does not matter which form was main and which was not. Again, in any sense, not only "parent-child" is irrelevant, but any other relationship. It's enough to know they are non-modal (or they are).

--SA
Shahin Khorshidnia 24-Mar-12 6:59am
   
Sorry about delay.
Yes SA, your description is accurate and right.
Thank you. I'm going to aprove the solution.
The solution given in solution 1 is good and is suitable when the forms are displayed in non modal way. But, as elaborated by OP in solution 2, since the the value selected in DataTimePicker is modified in the Button's click event and then shown in the child form, I think the following solution can be used.

In the Click event of Button add

C#
//Work out the date to be displayed in the child form by adding the hours 
//or with any other modification as required
DateTime modifiedDate = DateTimePicker1.Value.AddHours(10);
//Declare the Label1 on which the date is to be displayed in the child form as
//public for accessing it from the parent form 
//Option1
//If the form2 is already open, i.e. non modal form
if (form2 != null)
    //Use the required format of the date. 
    form2.Label1.Text = modifiedDate.ToShortDateString();

//Option2
//If the form2 is required as modal form
Form2 form2 = new Form2();
//Use the required format of the date. 
form2.Label1.Text = modifiedDate.ToShortDateString();
form2.ShowDialog();
form2.Dispose();
   
Comments
Shahin Khorshidnia 23-Mar-12 21:36pm
   
+5 Nice

Meanwhile, thank you for reminding my solution.
ProEnggSoft 23-Mar-12 21:38pm
   
Thank you
(__Aaron__) 24-Mar-12 2:03am
   
nice
ProEnggSoft 24-Mar-12 2:12am
   
Thank you
Thank you for the nest solution.

is it possible to pick a date form dateTime picker instead of show up on the lable on child?

i have replaced the lable to dateTime picker.

is it possible?

best regards

misse
   
Comments
ProEnggSoft 23-Mar-12 23:37pm
   
Please post your queries as comments under your question or modify the question using improve question wizard, so that one can easily see further additions to your question.
Regarding picking a date from the DateTimePicker, you have already stated in your question that the user picks a date from DateTimePicker. I could not get want you want from the above info.
Hi again,
Thank you for the solution,

I am doing that in ASP.NET, but the calendar is shown not like windows application, i mean not as a list box. The whole view of calendar is shown.

The question is, am trying to do the samething in asp.net, but it is not working.

my code is like,

1)DateTime modifiedDate = DateTimePicker1.Value.AddDay(14);
when the user picks a date the value will be bosted by 14

2) the i have a list box right after the DateTime picker and contain 1 to 30 hen wthe user picks a number like 5

then the value of the DateTime picker is redused by 5.
it means the result shows in another windows, like form 2.

my code is, when the user click the butten
C#
txt1.dtxtc.Text = modifiedDate.ToShortDateString();
txt1.ShowDialog();

but all are mess

need help
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900