简体   繁体   English

每当更新另一个文本框时更新一个文本框

[英]Update a textbox whenever another textbox is Updated

I have three textboxes. 我有三个文本框。 One for FirstName, the other for LastName, the Last for FullName. 一个用于名字,另一个用于姓氏,最后一个用于全名。

What i want to do is that whenever the firstname textbox is filled in, the FullName should be reset to the FirstName text and the FullName textbox should be updated whenever textbox is filled. 我想做的是,每当填写名字文本框时,FullName都应重置为FirstName文本,并且每当填写文本框时,FullName文本框都应更新。

I have tried 我努力了

    private void txtFirstName_TextChanged(object sender, EventArgs e)
    {
        txtFullName.Text = txtFirstName.Text;
    }

    private void txtLastName_TextChanged(object sender, EventArgs e)
    {
        if (_New == false)
        {
            txtFullName.Text += " ";
            _New = true;
        }
        string newletter = txtLastName.Text.Substring(txtLastName.Text.Length - 1);
        txtFullName.Text += newletter;
    }

This code works to an extent but the main problems with this code is that The fullname textbox is not updated whenever a user deletes a text from the lastname textbox also, i want it that when text is deleted from the firstname textbox when there is text in the lastname textbox, The entire text shouldn't be reset but the text should be updated. 该代码在一定程度上有效,但是此代码的主要问题在于,每当用户也从姓氏文本框中删除文本时,全名文本框也不会更新,我希望当文本中包含文本时从姓氏文本框中删除文本姓氏文本框,不应重置整个文本,而应更新文本。

Any help will be appreciated. 任何帮助将不胜感激。

This should work for you 这应该为你工作

    private void Form()
    {
        txtFirstName.TextChanged += TextBoxChanged;
        txtLastName.TextChanged += TextBoxChanged;
    }

    private void TextBoxChanged(object sender, EventArgs e)
    {
        txtFullName.Text = txtFirstName.Text + " " + txtLastName.Text;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM