在 TextBox 中将日期时间值从一种形式输入到另一种形式 [英] Enter DateTime value from one form to another form in TextBox

查看:63
本文介绍了在 TextBox 中将日期时间值从一种形式输入到另一种形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有三种形式.在 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 中的 textBox 中显示 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 中将日期时间值从一种形式输入到另一种形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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