ryuzdev
ryuzdev

Reputation: 37

how to see the writing of the text c # winforms

I am using the following code that should write letter by letter the string that I send but when it starts to write it really blocks the application and then shows me what it wrote that I can do

simulator.Keyboard.TextEntry("pruebva").Sleep(typingDelay);

the application crashes and then you see the text already written as I can see when each character is written

https://ourcodeworld.co/articulos/leer/520/simulando-eventos-keypress-de-la-manera-correcta-usando-inputsimulator-con-c-en-winforms

 private async void Start_Click(object sender, EventArgs e)
        {
            
            var simulator = new InputSimulator();
            int typingDelay = 100;


            Cursor.Position = pictureBox1.PointToScreen(new Point(171, 80));

            simulator.Mouse.LeftButtonClick();
            Thread.Sleep(5000);
            simulator.Keyboard.TextEntry("id-cellphone");
            simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);

            //enviar
            Cursor.Position = pictureBox1.PointToScreen(new Point(585, 577));
            simulator.Mouse.LeftButtonClick();



            simulator.Keyboard.TextEntry(dataGridView1.Rows[0].Cells[2].Value.ToString());
            simulator.Keyboard.TextEntry("probe_text").Sleep(typingDelay);
            simulator.Keyboard.KeyPress(VirtualKeyCode.SPACE);
            simulator.Keyboard.TextEntry(dataGridView1.Rows[0].Cells[7].Value.ToString());
            simulator.Keyboard.KeyPress(VirtualKeyCode.SPACE);
            simulator.Keyboard.TextEntry(dataGridView1.Rows[0].Cells[5].Value.ToString());
            simulator.Keyboard.KeyPress(VirtualKeyCode.SPACE);

            simulator.Keyboard.KeyPress(VirtualKeyCode.RETURN);

            dataGridView1.Rows.RemoveAt(dataGridView1.CurrentRow.Index);
          

thanks it worked

simulator.Keyboard.TextEntry(dataGridView1.Rows[0].Cells[2].Value.ToString());
            await Task.Delay(1000);
            simulator.Keyboard.TextEntry("pruebva").Sleep(typingDelay);
            await Task.Delay(1000);
            simulator.Keyboard.KeyPress(VirtualKeyCode.SPACE);
            await Task.Delay(1000);

I can see when you write the text at last thank you very much

Upvotes: 0

Views: 64

Answers (1)

Theraot
Theraot

Reputation: 40315

The application does not crash. Windows will report that a program is not responding when it stop processing operating system messages for 5 seconds. And guess what happens when you do Thread.Sleep(5000). Yep, five seconds of "do nothing" right there. It is preventing the UI thread to handle messages.

Please use Task.Delay like this: await Task.Delay(5000);. And yes, event handlers can be async.

See also:

Upvotes: 2

Related Questions