Posting a message with WebBrowser Control - c#

I have written some code to post messages to a site. It works just fine when I run it with button clicks, but if I try to run the whole thing with one single shot it throws an error. I know that the problem is that when I try to run it all in one shot the WebBrowser is not loading the page totally and thus it can't post the data. I know this is an easy fix, but I am stumped.
private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
{
GetData();
SendData();//If I eliminate this and just fire the SendData method with a button click, program works fine
}
private void GetData()
{
webBrowser1.Navigate(inputURLID);
}
private void SendData()//if I replace this with button2_Post_Click it works fine
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}

You cant run SendData() because your page don't load element you must wait until page load completely:try this:
private void button1_Start_Click(object sender, EventArgs e)//this is just the Pseudocode
{
GetData();
button1_Start.Enable = false;
}
private void GetData()
{
webBrowser1.Navigate("inputURLID");
}
private void SendData()
{
webBrowser1.Document.GetElementById("subject").SetAttribute("value", textBox2_Subject.Text);//To (username)
webBrowser1.Document.GetElementById("message").SetAttribute("value", richTextBox1.Text);//Subject
webBrowser1.Document.GetElementById("Submit").InvokeMember("click");//Message
button1_Start.Enable = true;
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
SendData();
}

Related

How to perform Click-Event on a hidden button

In my C# form I have two buttons
button1.Hide()
private void button2_Click(object sender, EventArgs e)
{
button1.PerformClick();
}
The button1 is hidden at form loading, I want the logic behind button1 to be perfomed when it's hidden too.
Just let the function outside become another function, then you can call function although you hidden the button1.
private void button1(object sender, EventArgs e)
{
_button1();
}
private void button2(object sender, EventArgs e)
{
_button1();
}
//Here is the function
void _button1()
{
...
}
If your Button is hidden, it seems that you need the functionality behind not or just in special cases. Keeping functionality out of events is often a simple solution to avoid problems in the future.
private void btn_Reload_Click(object sender, EventArgs e)
{
// reload here - maybe you reload all your employees from a datasource
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
// you can use functionality here from a another button and call the
btn_Reload_Click(this, EventArgs.Empty); // DON'T DO THIS IN MY OPINION
// ....
}
Maybe this solution is better even if you need the functionality at other workflows.
private void btn_Reload_Click(object sender, EventArgs e)
{
Reload();
}
private void btn_Reload_With_Calculation_Click(object sender, EventArgs e)
{
Reload();
Calculate();
}
void Reload() { }
void Calculate() { }

serial port reading multiple lines

I have a board which I am trying to communicate with. When I give it some commands it should return string messages back and should get posted in a textbox. My problem is when the device has to return multiple lines of text only 1 of the lines gets posted. I have tried also with ReadExisting instead of ReadLine but after one command I get only empty strings back.
public partial class Form1 : Form
{
private string x;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
timer1.Start();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.WriteLine(textBox1.Text);
textBox1.Clear();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(x))
{
}
else
{
textBox2.AppendText(x + "\n\r");
x = "";
}
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)
{
x = serialPort1.ReadLine();
//x = serialPort1.ReadExisting();
}
private void Form1_Closing(object sender, EventArgs e)
{
serialPort1.Close();
timer1.Stop();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
You're always better off avoiding DataReceived and just using BaseStream. Like this:
private async void Form1_Load(object sender, EventArgs e)
{
serialPort1.Open();
using (var reader = new StreamReader(serialPort1.BaseStream)) {
while (serialPort1.IsOpen) {
string x = await reader.ReadLineAsync();
textBox2.AppendText(x + "\r\n");
}
}
}
The await keyword silently manages synchronization back to the UI thread -- you don't need Invoke, you don't get cross-thread failures, you don't get a second invocation of your event handler triggered while the first one is still running, you don't get race conditions from multiple threads accessing the same variable. Much easier to get right.
As a style issue, I wouldn't put this code directly in your Form Load event handler, I would put it in a suitably named helper function (OpenAndReadSerial() perhaps) and call that from overridden OnLoad().

How to call this method in a background worker?

I am a beginner in C# and WPF and I am building this project in which I have to trigger when the mouse is moved. Under some conditions, I have to use it as a background worker. I want to call the mouse_Moved method in the background, but I don't know how to actually do that . Can anyone help me please? This is my code so far:
public MainWindow()
{
InitializeComponent();
mouse = new MouseInput();
mouse.MouseMoved += mouse_MouseMoved;
}
void mouse_MouseMoved(object sender, EventArgs e)
{
//The code that I need
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
//where I want to call the mouse_Moved method
}
Create a method and call it from both:
void mouse_MouseMoved(object sender, EventArgs e)
{
DoMouseMovedWork();
}
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
DoMouseMovedWork();
}
private DoMouseMovedWork()
{
//The code I need
}

Make a timer perform bidder00_TextChanged Event

I am trying to figure out how to make it that when my timer ticks, it performs a bidder00_TextChanged, or something like that.
Is this even possible to do? and if it isn't, is there any other way to do it?
I tried to search Google for it but i didn't get any results, if you find anything that i missed please post it here.
I don't really have any code but here it is:
private void bidder00_TextChanged(object sender, EventArgs e)
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
That is my TextChanged Event
My timer doesn't have any code because it is going to perform the bidder00_TextChanged Event.
You could create a method Perform() and call it from within your event handlers :
private void timer1_Tick(object sender, EventArgs e)
{
Perform();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
Perform();
}
private void Perform()
{
if (bidder00.Text == addbidder1.Text)
{
bidBtn1.PerformClick();
}
}
I assume you have coupled your actual logic with your click event which is not a good idea. Separate the code out into a separate function and have both parts of the application call the same code e.g.
private void SubmitBid()
{
// code you want to execute
}
private void OnSubmitBid()
{
// confirm whether we can actually submit the bid
if (bidder00.Text == addbidder1.Text)
{
SubmitBid();
}
}
private void Timer1_OnTick(object sender, EventArgs e)
{
// trigger code from timer
OnSubmitBid();
}
private void bidder00_TextChanged(object sender, EventArgs e)
{
// trigger code from text change
OnSubmitBid();
}
private void btnBid_Click(object sender, EventArgs e)
{
// trigger code from button press
OnSubmitBid();
}
Notice all the UI controls trigger the same code. There is an extra call in there for the text control validation (i.e. OnSubmitBid()) - if this wasn't required then you would just call SubmitBid directly.

How can I make the WebBrowser control navigate to a specific address?

How can I make the code when run the code it go to example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
Please correct it when run program it go to example.com
private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (webBrowser1.Document != null)
{
IHTMLDocument2 document = webBrowser1.Document.DomDocument as IHTMLDocument2;
if (document != null)
{
IHTMLSelectionObject currentSelection = document.selection;
IHTMLTxtRange range = currentSelection.createRange() as IHTMLTxtRange;
if (range != null)
{
const String search = "ant";
if (range.findText(search, search.Length, 2))
{
range.select();
}
}
}
}
}
Can you Navigate to example.com at Form.Load event? It's working fine in my machine.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("www.example.com");
}
You need to handle the Load event of your form (Form1) if you want the WebBrowser control to automatically navigate to www.example.com whenever your form is shown on the screen.
As it's written now, you handle the Navigated event of the WebBrowser control and tell it to navigate somewhere else. However, the Navigated event is only raised when the browser has navigated to and begun loading a new page. Even if you get your code to work, it will be perpetually chasing its own tail.
Instead, try the following:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("http://www.example.com");
}
}
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("http://www.example.com");
}
This execute the navigate method after the app is initialized.
I'm not sure if I understand your question: The e variable in the webBrowser1_DocumentCompleted method contains the Url property that holds the current Uri object with the URL where the browser control has arrived:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser browser = (WebBrowser)sender;
if (e.Url.Host.EndsWith("example.com"))
{
// do something
}
}

Categories

Resources