Member Avatar for Gamer0077

First of all, sorry for my bad english, that doesn't make any sence at all. So, please correct me.

I made a programm to use SendKeys, but I want a delay between the word.
I was able to split the string in words, but I wasn't be able to send it with a delay.
My program pasts the text of the clipboard (using RadioTextbox) and from the textbox (using the radiobutton RadioClipboard and textbox TextBox1). Button1 starts the whole thing. I have a timer, which counts down to zero.

This snippet splits the string:

string[] text = TextBox1.Text.Split(' ');
foreach (string sendword in text)
{
    //Sendkeys.Send(word); and some sort of delay or something
    //Or a delay which sends the string word
}

This is my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Windows.Forms.VisualStyles;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;

namespace Tio_Copy_Paster
{
    public partial class Form1 : Form
    {
        int timer1value = 5;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            RadioTextbox.Enabled = false;
            RadioClipboard.Enabled = false;
            TextBox1.ReadOnly = true;
            Button1.Enabled = false;
            Button1.Text = "5";
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timer1value == 0)
            {

                if (RadioTextbox.Checked == true)
                {
                    Sendkeys.Send(TextBox1.Text); //Sends the whole string
                    //Should split the string and delay it.
                    //Maybe using a timer, it might send the string
                }
                else if (RadioClipboard.Checked == true)
                {
                    SendKeys.Send(Clipboard.GetText());
                    //The same here
                }

                timer1.Enabled = false;
                RadioClipboard.Enabled = true;
                RadioTextbox.Enabled = true;
                Button1.Text = "Plak!";
                Button1.Enabled = true;
                timer1value = 5;
                TextBox1.ReadOnly = false;
            }
            else
            {
                timer1value = timer1value - 1;
                Button1.Text = timer1value.ToString();
            }
        }
    }
}
Member Avatar for Gamer0077

Might a extra process work?
But I don't know where to begin...

Here is a sample program that takes what is in textBox1 and puts it into textBox2 one word at a time. The form has the two textbox, a button, and a timer (with a delay of 5 for my test).

using System;
using System.Collections;
using System.Windows.Forms;

namespace WindowsFormsApplication2 {
    public partial class Form1 : Form {
        IEnumerator iterator;
        String[] array;

        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            array = textBox1.Text.Split(' ');
            iterator = array.GetEnumerator();
            timer1.Enabled = true;
        }

        private void timer1_Tick(object sender, EventArgs e) {
            if (iterator == null || iterator.MoveNext() == false) {
                timer1.Enabled = false;
            } else {
                textBox2.Text += iterator.Current + " ";
            }
        }
    }
}

Modify as you see fit.

Good post from Momerath. I'd like to suggest the use of Invoke and Thread's sleep methods.

private void button1_Click(object sender, EventArgs e)
       {
            Action a = () => {
                foreach (char ch in textBox1.Text)
                {
                    System.Threading.Thread.Sleep(100);
                    
                    textBox2.Focus();
                    SendKeys.SendWait(ch.ToString());

                }
            };

            textBox1.Invoke(a);
        }
Member Avatar for Gamer0077

Thank you very much, both of you!
But Momerath, is it possible to do the same with each character?
Because your solution worked the best for me.

So are there any bugs and is there a solution to sendkeys characters.

This is my current code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Windows.Forms.VisualStyles;
using System.IO;
using System.Text.RegularExpressions;
using System.Drawing.Drawing2D;
using System.Collections;

namespace Tio_Copy_Paster
{
    public partial class Form1 : Form
    {
        int timer1value = 5;
        IEnumerator iterator;
        String[] array;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
            RadioTextbox.Enabled = false;
            RadioClipboard.Enabled = false;
            TextBox1.ReadOnly = true;
            Button1.Enabled = false;
            Button1.Text = "5";

            if (RadioTextbox.Checked == true)
            {
                array = TextBox1.Text.Split(' ');
                iterator = array.GetEnumerator();
            }
            else if (RadioClipboard.Checked == true)
            {
                array = Clipboard.GetText().Split(' ');
                iterator = array.GetEnumerator();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (timer1value == 0)
            {
                timer2.Enabled = true;
                timer1value = 5;
                timer1.Enabled = false;
            }
            else
            {
                timer1value = timer1value - 1;
                Button1.Text = timer1value.ToString();
            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            if (iterator == null || iterator.MoveNext() == false)
            {
                timer2.Enabled = false;
                RadioClipboard.Enabled = true;
                RadioTextbox.Enabled = true;
                Button1.Text = "Plak!";
                Button1.Enabled = true;
                TextBox1.ReadOnly = false;
            }
            else
            {
                SendKeys.Send(iterator.Current + " ");
            }
        }
    }
}

Sure, you can do one character at a time. You won't need the String[] array; anymore, as you can get an IEnumerator from the text itself. So line 46 of your code becomes

iterator = Clipboard.GetText().GetEnumerator();

With this you'll get one character each time the timer triggers.

Member Avatar for Gamer0077

Thanks Momerath, it worked for me!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.