Click here to Skip to main content
15,893,161 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys!

How can I make my program find a word after a specific word in textbox1 and then copy it to the clipboard? The word after the specific word can contain letters like "=#!?" btw.

What I have tried:

Clipboard.SetText("");
Posted
Updated 12-Nov-17 5:50am
v2
Comments
Karthik_Mahalingam 12-Nov-17 12:04pm    
show some example,
what is the input and expected output?

I wrote this code:

string FindWord(string specificWord)
{
    string[] words = textBox1.Text.Split(' '); // split the textBox1's Text into words

    for (var i = 0; i < words.Length; i++)
    {
        // Check if the specificWord is in the words array and
        // if the words array has room for at least another word
        if ((words[i] == specificWord) && (i < (words.Length - 1)))
        {
            return words[i + 1];
        }
    }

    return "";
}

void ProcessData()
{
    string result = FindWord("test");

    if (result != "")
    {
        try
        {
            Clipboard.SetText(result);
        }
        catch (Exception x)
        {
            MessageBox.Show("Error copying " + result + " to clipboard.\n\n" + x);
        }
    }
}

void Button1Click(object sender, EventArgs e)
{
    ProcessData();
}


So far, it works.
 
Share this answer
 
Comments
Kaj Lao 12-Nov-17 11:43am    
Hi, it's not working for me? When I am typing something in the textbox, it's not copying the last word to the clipboard. Please help. What does this code do exactly?
Wait a second: you didn't say that want the last word, but the word immediately after the specific word. My code is doing what you asked in the first place.

e.g. If "test" is the specific word:
word1 word2 test word4 word5
My code will copy word4 to clipboard.
 
Share this answer
 
Comments
Karthik_Mahalingam 12-Nov-17 11:56am    
dont post multiple solution, instead update the previous one and delete the rest.
Kaj Lao 12-Nov-17 12:00pm    
@alin1 That's a great solution. But 'word 4' does not get copied to the clipboard when I enter ctrl + v ?
alin1 12-Nov-17 18:38pm    
When you press Ctrl V you paste from clipboard, not copy to clipboard.
Kaj Lao 12-Nov-17 12:05pm    
Please explain more in depth how your code works. Will it copy the word to the clipboard so when I enter ctrl + v on my keyboard it will copy the word? Because that's what I am looking for
alin1 12-Nov-17 12:19pm    
My code copies that word automatically (if it's found). When you press Ctrl V you will get that word. Did you try adapting my code to your project?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900