Invoking google search button - c#

My application has one button one text box and a webbrowser control which is going to navigate at google.com at form load event.
If I am setting the text attributes to the google textbox in webBrowser1_DocumentCompleted method after clicking the button I am able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
textElement.SetAttribute("value", "facebook");
}
private void btnLogin_Click(object sender, EventArgs e)
{
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
If I am setting the text attributes to the google textbox in btnLogin_Click method after clicking the button i am not able to invoke google search button.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Navigate("https://www.google.co.in/?gfe_rd=cr&ei=u6PkV4X9LovZ8Aec7bI4&gws_rd=ssl");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
textElement = webBrowser1.Document.All.GetElementsByName("q")[0];
}
private void btnLogin_Click(object sender, EventArgs e)
{
textElement.SetAttribute("value", "facebook");
ele = webBrowser1.Document.All.GetElementsByName("btnK")[0];
ele.InvokeMember("click");
}
Where I am going wrong please suggest me.

Related

Clear Data in chart c#

I want to create a delete button so when i press the delete button i can key in a new set of data and let it generate a new chart
private void button2_Click(object sender, EventArgs e)
{
chart1.Series.Clear();
}
and I had tried
private void button2_Click(object sender, EventArgs e)
{
chart1.Series["Data1"].Points.Clear();
chart1.Series["Data2"].Points.Clear();
chart1.Series["Total"].Points.Clear();
}
What I have did so far

1 button, 2 webpages, but one webpage at a time?

Here's what H have so far:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
}
private void button3_Click(object sender, EventArgs e)
{
audio.Stop();
if (button1.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
if (button2.Enabled == true)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
}
}
this is only my test so far but what i want to do is change what button 3 does, i.e. if button 1 is clicked button three will open webpage 1, if button2 is clicked button 3 will open webpage 2, button 3's image will change depending, but what im finding with what i have done so far is that it opens BOTH pages AT THE SAME TIME ... how to i prevent this? i have tried if, else and else if, same result every time.
Both of your buttons are enabled, you are checking to see if the buttons are enabled or disabled (clickable or not), not which one has been clicked.
also:if (button2.Enabled == true)
is nested in the first conditional, I'm not sure if that's what you want.
You can: disable buttons 1 and 2 after their clicked so that, for instance button2.Enabled will now = false; (but then you will not be able to reclick that button)
More sophisticated, but better, is to use a delegate for the button3, and assign them in your button1_Click and button2_Click events. Something like this:
private void button1_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.ai_yori_aoshi_5370;
button3.Click += new EventHandler(this.Button3_Click_First);
}
private void button2_Click(object sender, EventArgs e)
{
button3.BackgroundImage = slideshow_test.Properties.Resources.AiYoriAoshi_feature;
button3.Click += new EventHandler(this.Button3_Click_Second);
}
void Button3_Click_First(Object sender,
EventArgs e)
{
// When the button is clicked,
// change the button text, and disable it.
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide");
}
void Button3_Click_Second(Object sender,
EventArgs e)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start("http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide");
}
You may also have to check and make sure an event handler was not previously assigned, in calse someone clicks button1, then button2, then button1 ect. This is described here: Removing event handlers
You can handle your problem by storing the URL of the webpage in a private field, setting it when buttons 1 or 2 are clicked and reading from it after clicking button 3.
private string _address = null;
private void button1_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-guide";
}
private void button2_Click(object sender, EventArgs e)
{
// do other stuff
_address = "http://www.watchcartoononline.com/anime/ai-yori-aoshi-enishi-guide";
}
private void button3_Click(object sender, EventArgs e)
{
if (_address != null)
{
audio.Stop();
if (button1.Enabled || button2.Enabled)
{
timer1.Stop();
pictureBox1.Visible = false;
System.Diagnostics.Process.Start(_address);
}
}
}
I wasn't sure if all the code in button3_Click is necessary, so I cleared it up a little. I might be a bit off, though.
button.Enabled is always true for all buttons by default unless you set it to false. So you cannot use button1.Enabled property to check which button is pressed. try below approach.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = true;
}
protected void Button2_Click(object sender, EventArgs e)
{
ViewState["Button1Clicked"] = false;
}
protected void Button3_Click(object sender, EventArgs e)
{
if ((bool)ViewState["Button1Clicked"])
{
//open webpage2 code comes here
}
else
{
//open webpage2 code comes here
}
}

C# How to make another button appear by clicking a different button?

I want to click on a button and make a text block and another button appear. I managed to make the text block appear. How do I make the button appear. I can't seem to find a function that does so. Also, does it need to be a different private void statement. So far I have:
private void one_Click(object sender, RoutedEventArgs e)
{
oneBlock.Text = "one";
}
private void one_Click(object sender, RoutedEventArgs e)
{
one_Trans.ClickMode = "two";
}
You can do something like this:
private void button1_Click(object sender, RoutedEventArgs e)
{
button2.Visibility = Visibility.Visible;
}
XAML:
<Button x:Name="button2" Content="Button" Visibility="Collapsed"/>
More here: http://msdn.microsoft.com/en-us/library/system.windows.visibility(v=vs.95).aspx
use
protected void button1_Click(object sender, EventArgs e)
{
button2.Visible=true;
}
on the click event of button1
Put everything into the same event
private void one_Click(object sender, RoutedEventArgs e)
{
oneBlock.Text = "one";
one_Trans.ClickMode = "two";
button1.Visible = true;
}

My RichTextBox's cut/copy/paste doesn't cut, copy, or paste

Here's my code:
void CutAction(object sender, EventArgs e)
{
richTextBox2.Cut();
}
void CopyAction(object sender, EventArgs e)
{
Clipboard.SetData(DataFormats.Rtf, richTextBox2.SelectedRtf);
Clipboard.Clear();
}
void PasteAction(object sender, EventArgs e)
{
if (Clipboard.ContainsText(TextDataFormat.Rtf))
{
richTextBox2.SelectedRtf
= Clipboard.GetData(DataFormats.Rtf).ToString();
}
}
private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{ //click event
//MessageBox.Show("you got it!");
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox2.ContextMenu = contextMenu;
}
}
I have 2 problems:
After marking the text in richTextbox2 I need to simulate a right click on the mouse to see the cut paste copy menu.
When I click on Copy I can't afterwards paste it anywhere because there is nothing to paste. I didn't test yet the cut and paste options but after doing copy it's not working.
Remove the Clipboard.Clear();
void CopyAction(object sender, EventArgs e) {
Clipboard.SetData(DataFormats.Rtf, richTextBox2.SelectedRtf);
}
You can also use the Copy() method of a RichTextBox:
void CopyAction(object sender, EventArgs e) {
richTextBox2.Copy();
}
For paste:
void PasteAction(object sender, EventArgs e) {
if (Clipboard.ContainsText(TextDataFormat.Rtf)) {
SendKeys.Send("^v");
}
}
private void btnCopy_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
richTextBox1.Copy();
}
private void btnPaste_Click(object sender, EventArgs e)
{
richTextBox2.Paste();
}
private void btnCut_Click(object sender, EventArgs e)
{
richTextBox1.SelectAll();
richTextBox1.Cut();
}
Note:The windows forms has built in methods to copy the text from richTextBox to Clipboard.
like Copy, Paste, Cut (you have to select the text first which you want to copy or cut. Here in my example i had given select all text).
Here in this example basically it will copy the content to the clipboard, still there are overloading methods please see the definitions of the methods.
Try to add toolstripbar, add 3 toolstripbuttons. This is the code for copy, cut and paste
private void toolStripButton1_Click(object sender, EventArgs e)
{
SendKeys.Send("^x");
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
SendKeys.Send("^v");
}
private void toolStripButton3_Click(object sender, EventArgs e)
{
SendKeys.Send("^c");
}
The code works directly with the clipboard.
thanks for your answer mr Doron Muzar
private void richTextBox2_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
{ //click event
//MessageBox.Show("you got it!");
ContextMenu contextMenu = new System.Windows.Forms.ContextMenu();
MenuItem menuItem = new MenuItem("Cut");
menuItem.Click += new EventHandler(CutAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Copy");
menuItem.Click += new EventHandler(CopyAction);
contextMenu.MenuItems.Add(menuItem);
menuItem = new MenuItem("Paste");
menuItem.Click += new EventHandler(PasteAction);
contextMenu.MenuItems.Add(menuItem);
richTextBox2.ContextMenu = contextMenu;
}
}
void CutAction(object sender, EventArgs e)
{
richTextBox1.Cut();
}
void CopyAction(object sender, EventArgs e)
{
richTextBox1.Copy();
}
void PasteAction(object sender, EventArgs e)
{`
richTextBox1.Paste();
}

Web browser control not working to get text

I'm trying to get source code of a webpage and save it to richtextbox and that web browser control navigates to new URL. But I'm getting a blank rich text box. Here is my code:
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
webBrowser2.Navigate("http://www.gmail.com");
}
private void timer1_Tick(object sender, EventArgs e)
{
if(webBrowser2.ReadyState == WebBrowserReadyState.Complete)
{
timer1.Enabled = false;
richTextBox1.Text = webBrowser2.DocumentText;
webBrowser2.Navigate("new URL");
}
}
I see two alternatives to what you're doing.
1) Using DocumentCompleted:
private void Form_Load(object sender, EventArgs e) {
webBrowser.Navigate("www.google.com");
}
private void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) {
richTextBox.Text = webBrowser.DocumentText;
}
2) Using the WebClient:
private void Form_Load(object sender, EventArgs e) {
using (System.Net.WebClient wc = new System.Net.WebClient()) {
richTextBox.Text = wc.DownloadString("http://www.google.com");
}
}

Categories

Resources