如何在Form2中显示日期时间选择器 [英] How to show date time picker in form2

查看:79
本文介绍了如何在Form2中显示日期时间选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,
我的问题是,我的form1中有一个日间选择器,当用户选择要加或减10或15或...的日期时,我想要第二个形式的值.我有fom1 1和2,可能在form2的标签上.

我已经尝试过,但没有结果.

有很多thanxs

/B

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

推荐答案

你好

我想您想要WinForm.

例如这样的:
Hello

I suppose you want it for WinForm.

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



在Form2中:

定义delegate



In Form2:

define a delegate

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



然后

在DateTimeControl的ValueChanged事件中:



Then

In ValueChanged event of the DateTimeControl:

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




在Form1中:

为委托定义一个处理程序.
例如:




In Form1:

Define a handler for the delegate.
for example:

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



并定义ChengeValue方法:



And define ChengeValue method:

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




如果DateTimePicker的格式为1:
1.在form2中定义一个公共方法(例如MyMethod(DateTime value))
2.在DateTimePicker调用ValueChanged事件form2.MyMethod(值)




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)


解决方案1中给出的解决方案很好,适用于以非模式方式显示表单.但是,正如解决方案2中OP所述,由于DataTimePicker 中选择的值是在Button的click事件中修改的,然后以子窗体形式显示,因此我认为可以使用以下解决方案. />
Button add
Click 事件中
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

//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();


感谢您的嵌套解决方案.

是否可以选择日期形式的dateTime选择器,而不是显示在孩子的标签上?

我已将标签替换为dateTime选择器.

有可能吗?

最好的问候

错过
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


这篇关于如何在Form2中显示日期时间选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆