Run textbox in C #

0

How can I do to identify the character ~ in a keypress. I need to do this for a textbox, that when the person scans a number, run the scan until it completes and has the two characters ~ the start and the end ~.

private void formpress(object sender, KeyPressEventArgs e)
    {
        string buscar = "";

        if (e.KeyChar == char(126) )
        {


        }
    }

I have no idea how to implement this so that I can execute the number until it has the start character and the end character. If someone can help me, thank you.

    
asked by use2105 29.11.2016 в 15:25
source

2 answers

3

I do not know if I understand exactly what you intend, but instead uses the event KeyPress the TextBox.TextChanged . It would be something like this:

private void textBox1_TextChanged(object sender, EventArgs e)
{
    if (textBox1.Text.StartsWith("~") && textBox1.Text.EndsWith("~") && textBox1.Text.Length>1)
    {
        MessageBox.Show("El texto está completo");
    }
}
    
answered by 29.11.2016 / 15:30
source
0

Good morning, it will be quite difficult with the event KeyPress , what I would recommend is that you use the event TextChanged , which is executed every time the text of the control changes (information here and here ). Now, to verify that there is at the beginning and at the end you can use something like this:

private void yourTextBox_TextChanged(object sender, EventArgs e)
        {
            if (yourTextBox.Text.Length > 3)
            {
                if (yourTextBox.Text [0] == '~' && yourTextBox.Text [textBox1.Text.Length - 1] == '~')
                {

                }
            }
        }

You can also build a regular expression and verify it by means of this (but with regular expressions I do not have much experience), but in the same way, here is a video from YouTube, where they explain a bit about this.

    
answered by 29.11.2016 в 15:39