eltel2910
eltel2910

Reputation: 345

Same webpage always loads with webBrowser

I have a program that I wrote for sending messages to some of my friends through a website. It works fine, but for some reason I can't get it to work correctly with just one button click event. If I don't have a second button click for the SEND data method (whichs POSTs the data), it will always just keep sending the message to the same person, eventhough the new URL is loaded into the webBrowser URL*, but if I have that second click event all works fine. What am I missing?

*Using the debugger I see a new URL load with every iteration, but I do see on the HTTP debugger that the program is sending to the same URL each time

private void button1_Click(object sender, EventArgs e)
    {
        ListBox();
    }
 private void ListBox()
    {
        //gets name from ListBox        

        GetData();


    }

private void GetData()
    {

       webBrowser1.Navigate(inputURLID);

        //SendData ();  Always sends to the same person if I call from here, so I made a second button click and it works fine
    }

private void button2_Click(object sender, EventArgs e)// works fine like this
     {
         webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)

         webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject

         webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message

     }

....

private void SendData()// always sends to the same person if I just do it like this
    {


        webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)

        webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject

        webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
    }

Upvotes: 0

Views: 175

Answers (1)

Andrea
Andrea

Reputation: 12385

Try to populate your fields only when the url has been loaded (when DocumentCompleted event is fired):

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{

   webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2.Text);//To (username)

   webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject

   webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}

Upvotes: 1

Related Questions