Enter DateTime value from one form to another form in TextBox(在 TextBox 中将 DateTime 值从一个表单输入到另一个表单)
问题描述
好的,我有三个表格.在 Form1 我有 2 个 dateTimePicker 和 1 个按钮.当我单击该按钮时,我转到具有另一个按钮的 Form2 .当我单击该按钮时,它会显示一条消息,给出 dateTimePicker 的值.我有另一个 Form3 我有一个文本框.
Okay I have three forms. In Form1 I have 2 dateTimePicker and 1 button. When I click that button I go to Form2 which has another button. When I click that button it shows a message giving the value of dateTimePicker. I have another Form3 where I have a textbox.
问题:因此,我想要的不是在 Form 2 中显示消息中的值,而是当我在 Form1 中的 dateTimePicker 中选择一个值并单击 btn 时,Form2 应该打开,并且当我单击按钮 form3 时应该在 form2 中打开打开并在 Form3 的文本框中显示 dateTimePicker 值.这是我目前所拥有的.
Question: So instead of showing the value in message in Form 2, what I want is that when I select a value in dateTimePicker in Form1 and click the btn, Form2 should open and in the form2 when i click the button form3 should open and in the textBox in Form3 dateTimePicker value should be displayed. This is what I have so far.
private void button1_Click(object sender, EventArgs e) //form1
{
Form2 obj = new Form2(textBox1.Text,dateTimePicker1.Value,dateTimePicker2.Value);
this.Hide();
obj.ShowDialog();
this.Close();
Form3 f3 = new Form3(dateTimePicker1.Value);
}
string Name; //form2
DateTime DT1;
DateTime DT2;
DateTime DT3;
public Form2(string name, DateTime dt1,DateTime dt2)
{
InitializeComponent();
Name = name;
DT1 = dt1;
DT2 = dt2;
}
private void button2_Click(object sender, EventArgs e)
{
//MessageBox.Show("Check In Date: "+ DT1.ToString() + " Check Out Date: " + DT2.ToString());
Form3 f3 = new Form3(DT1);
f3.ShowDialog();
}
DateTime ChkInDate; // form3
public Form3(DateTime chkindate)
{
InitializeComponent();
ChkInDate = chkindate;
}
private void CheckInTxt_TextChanged(object sender, EventArgs e)
{
CheckInTxt.Text = ChkInDate.ToString();
}
推荐答案
需要在Form3的构造函数中设置TextBox的初始值.
You need to set the initial value of the TextBox in Form3's constructor.
public Form3(DateTime chkindate)
{
InitializeComponent();
ChkInDate = chkindate;
CheckInTxt.Text = chkindate.ToString(); // add this line
}
这篇关于在 TextBox 中将 DateTime 值从一个表单输入到另一个表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 TextBox 中将 DateTime 值从一个表单输入到另一个表单
基础教程推荐
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- JSON.NET 中基于属性的类型解析 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
