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");
}
}
Related
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.
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
}
}
I want to stop browser navigating,when i click button.I tried in the below method but it doesn't worked.
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
button2.Click += new RoutedEventHandler((object caller,System.Windows.RoutedEventArgs f) =>
{
e.Cancel = true;
});
}
Try this one-
private void webBrowser_Navigating(object sender, NavigatingEventArgs e)
{
stop.IsEnabled=true;
stop.Click += new RoutedEventHandler((object caller, System.Windows.RoutedEventArgs f) =>
{
stopPressed = true;
});
if (stopPressed == true)
e.Cancel = true;
}
If you want to stop the browser navigating you can try like this:
private void stop_Tap(object sender, System.Windows.Input.GestureEventArgs e)
{
MiniBrowser.InvokeScript("eval", "document.execCommand('Stop');");
}
i try to add the option to my application to drag file into my Listbox instead of navigate into the file folder and this is what i have try:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
listBoxFiles.Items.Add(e.Data.ToString());
}
but instead of the full file path e.Data.ToString() return System.Windows.Forms.DataObject
This code I found here:
private void Form1_Load(object sender, EventArgs e)
{
listBoxFiles.AllowDrop = true;
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
listBoxFiles.DragEnter += listBoxFiles_DragEnter;
}
private void listBoxFiles_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void listBoxFiles_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
foreach (string file in files)
listBoxFiles.Items.Add(file);
}
I tried using #AsfK's answer in WPF, had to delete
listBoxFiles.DragDrop += listBoxFiles_DragDrop;
from public MainWindow() else I got the dragged files duplicated.
Thanks!
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();
}