【问题标题】:Enter DateTime value from one form to another form in TextBox在 TextBox 中将 DateTime 值从一个表单输入到另一个表单
【发布时间】:2014-10-31 23:35:56
【问题描述】:

好的,我有三个表格。在 Form1 我有 2 个 dateTimePicker 和 1 个按钮。当我单击该按钮时,我会转到Form2,它有另一个按钮。当我单击该按钮时,它会显示一条消息,给出dateTimePicker 的值。我有另一个Form3,我有一个文本框。

问题:因此,我想要的不是在 Form 2 中显示消息中的值,而是当我在 Form1 中的 dateTimePicker 中选择一个值并单击 btn 时,Form2 应该打开,并且当我单击按钮 form3 时应该在 form2 中打开打开并在 Form3 的文本框中显示 dateTimePicker 值。 这是我目前所拥有的。

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

【问题讨论】:

  • CheckInTxt_TextChanged 中的代码没有意义。您的问题到底出在哪里?
  • 是的,我删除了那部分。我想要的只是在我的 Form3 文本框中显示 DateTime

标签: c# winforms forms textbox datetimepicker


【解决方案1】:

需要在Form3的构造函数中设置TextBox的初始值。

public Form3(DateTime chkindate)
{
    InitializeComponent();
    ChkInDate = chkindate;
    CheckInTxt.Text = chkindate.ToString(); // add this line
}

【讨论】:

  • 同时删除 CheckInTxt_TextChanged
  • 非常感谢。我终于明白了。
  • @samjohal,如果有帮助,请将其标记为答案,谢谢。你可以像ToString("dd MM yyyy")这样格式化它,它会显示日月年
猜你喜欢
  • 1970-01-01
  • 2010-12-06
  • 1970-01-01
  • 2021-03-09
  • 1970-01-01
  • 2011-08-04
相关资源
最近更新 更多