简体   繁体   中英

How to submit and retrieve text from dynamic textbox when Enter key is pressed in Windows forms using C#?

I have a button which when pressed generates a textbox. The user writes something on the text box and on pressing the Enter key, the text must be displayed at the same position through a label.

I have been able to create the dynamic text box and I think I know how to assign the text to a label and display it provided I get the text.

Most of the tutorials on the internet talk about using the submit button and creating an AcceptButton as the default button and then linking them both. But here I do not have a submit button per se. And I have not been able to figure out how to write the keypress event handler and where to write it since the textboxes are already inside an event handler.

Please help.

Here is what I could come up with.

int CMonSub;
private void btnMonSub_Click(object sender, EventArgs e)
{
    TextBox txtMonSub = new TextBox();
    txtMonSub.Name = "MonSub" + CMonSub;
    txtMonSub.Location = new System.Drawing.Point(155 + (100 * CMonSub), 60);
    CMonSub++;
    txtMonSub.Size = new System.Drawing.Size(100, 25);
    this.Controls.Add(txtMonSub);
    string s = txtMonSub.Text;
    Label l = new Label();
    l.Text = s;
    l.Location = new System.Drawing.Point(155 + (100 * CMonTime), 60);
    l.Size = new System.Drawing.Size(100, 25);
    this.Controls.Add(l);
}

I just need to get that string s when the user presses Enter key.

And maybe the part where I'm assigning the text to a label is also not the correct way either. Help with that is also appreciated.

Thanks in advance.

If you have more than one textbox and label, you will need to keep track of the pairs so you know which one you're dealing with. A Dictionary is the easiest way to do this.

You can use the KeyPress event to see when the user clicks a key, and do something when that key is enter.

Here is some example code:

    Dictionary<TextBox, Label> textBoxLabelPairing = new Dictionary<TextBox, Label>();

    private void btnMonSub_Click(object sender, EventArgs e)
    {
        TextBox txtMonSub = new TextBox();
        txtMonSub.Name = "MonSub" + CMonSub;
        txtMonSub.Location = new System.Drawing.Point(155 + (100 * CMonSub), 60);
        CMonSub++;
        txtMonSub.Size = new System.Drawing.Size(100, 25);

        //ADDED: keypress event
        txtMonSub.KeyPress += txtMonSub_KeyPress;

        this.Controls.Add(txtMonSub);
        string s = txtMonSub.Text;
        Label l = new Label();
        l.Text = s;
        l.Location = new System.Drawing.Point(155 + (100), 60);
        l.Size = new System.Drawing.Size(100, 25);
        this.Controls.Add(l);

        //ADDED: Dictionary Pairing
        textBoxLabelPairing.Add(txtMonSub, l);
    }

    void txtMonSub_KeyPress(object sender, KeyPressEventArgs e)
    {
        //if enter key is pressed
        if (e.KeyChar == (char)13)
        {
            TextBox thisTextBox = (TextBox)sender;
            Label associatedLabel = textBoxLabelPairing[thisTextBox];
            associatedLabel.Text = thisTextBox.Text;
        }
    }

This code will make an event for every new textbox you make. The only issue is that it is always triggering the same method (txtMonSub_KeyPress). To combat this, you need to use the Sender from the event (as shown in code) so you know what textbox was just updated. From there, you can use your dictionary object to see what label needs to be updated (since we paired them in the previous method).

Update:

If you really want to dispose the textbox, you can do it like this:

    void txtMonSub_KeyPress(object sender, KeyPressEventArgs e)
    {
        //if enter key is pressed
        if (e.KeyChar == (char)13)
        {
            TextBox thisTextBox = (TextBox)sender;
            string textBoxText = thisTextBox.Text;
            Label associatedLabel = textBoxLabelPairing[thisTextBox];
            associatedLabel.Text = textBoxText;
            this.Controls.Remove(thisTextBox);
            thisTextBox.Dispose();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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