The name "webBrowser" does not exist in the current context - c#

I have this code:
public void button1_Click(object sender, EventArgs e)
{
for(int i = 0; i < numericUpDown1.Value; i++)
{
WebBrowser webBrowser = new WebBrowser();
webBrowser.Visible = false;
webBrowser.ScriptErrorsSuppressed = true;
webBrowser.Navigate("want to keep it private.");
webBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser_DocumentCompleted);
}
}
public void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement botNick = webBrowser.Document.GetElementById("nick");
botNick.SetAttribute("value", textBox1.Text);
HtmlElement botPlay = webBrowser.Document.GetElementsByTagName("button")[2];
botPlay.InvokeMember("click");
}
Why am i getting "The name "webBrowser" does not exist in the current context"?

Firstly, "The name "webBrowser" does not appear in the current context" error means that you propably have not defined such a variable/field/property/etc. in the current context.
Secondly, since you have not clarified the line of the error, I will assume that it appeared on these 2 lines:
HtmlElement botNick = webBrowser.Document.GetElementById("nick");
HtmlElement botPlay = webBrowser.Document.GetElementsByTagName("button")[2];
This happens because, while you have declared webBrowser here:
WebBrowser webBrowser = new WebBrowser();
which exists in function void button1_Click(object sender, EventArgs e)
and therefore is invisible to the function void webBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e).
To rectify this situation you can either pass the webBrowser as an argument, or define a new var with the name of webBrowser in the latter function.

Related

Why the external IE is openings from .NET webbrowser

I have this form which has a System.Windows.Forms.WebBrowser controller in it. One of the html pages has some href in it like this
<a href="https://twitter.com/xxx" target="_blank">
<h3 style="margin-top:15%; text-align:center">xxx</h3>
</a>
I am catching the clicks in the
private void webBrowser1_NewWindow(object sender, CancelEventArgs e)
and opening the page as so
webBrowser1.Navigate(page);
Now this works and the page is shown inside the webBrowser1, but this also opens the external IE with the same page. Actually, the external IE opens first before the webBrowser1 shows the page.
So my question is: Why does the external IE opens and how can I stop this.
cheers,
es
Instead of opening a new page try to create custom onClick event
private bool bCancel = false;
private void webBrowser_DocumentCompleted(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
int i;
for (i = 0; i < webBrowser.Document.Links.Count; i++)
{
webBrowser.Document.Links[i].Click += new
HtmlElementEventHandler(this.LinkClick);
}
}
private void LinkClick(object sender, System.EventArgs e)
{
bCancel = true;
MessageBox.Show("Link Was Clicked Navigation was Cancelled");
}
private void webBrowser_Navingating(object sender,
WebBrowserNavigatingEventArgs e )
{
if (bCancel == true)
{
e.Cancel=true;
bCancel = false;
}
}

Find method name which opened new window (c# wpf)

I'm trying to find a way to get the string name of the method call which pops up a new window. I have three button click event handlers which will open the new window but I need to know which called the .Show();
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
I don't want to have to have three separate windows! is there an opening event handler parameter which I can fetch the caller from?
well, you can simply add a public variable at MobilityPortfolioSettings class and set its value in each method, ex: in buttonSettingsPortfolio1_Click add MobilityPortfolioSettings.Variable = 1 and so on.
Here
Console.write(triggeredBy); you can output the value by logging to file or some other way . This value will indicate which path your code took.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio1_Click");
}
private void buttonSettingsPortfolio2_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio2_Click");
}
private void buttonSettingsPortfolio3_Click(object sender, RoutedEventArgs e)
{
Open("buttonSettingsPortfolio3_Click");
}
private Open(string triggeredBy){
Console.write(triggeredBy); // You can write to file or output in some different way here.
var settingsWindow = new MobilityPortfolioSettings();
settingsWindow.Show();
}
Try this:
Cast sender as button and then get it's name.
Change the MobilityPortfolioSettings constructor so that it needs a string parameter.
Pass the button name to the constructor.
private void buttonSettingsPortfolio1_Click(object sender, RoutedEventArgs e)
{
string buttonName = "";
if (sender is Button)
buttonName = ((Button)sender).Name;
Window settingsWindow = new MobilityPortfolioSettings(buttonName);
settingsWindow.Show();
}
BTW use Window as variable type instead of var.
Cheers

c# web browser and statustext to show hyperlink

So I have a custom windows form with a tabbed browser. I want to be able to see the hoovered hyperlink in the status bar. I have searched everywhere but nothing yet.
I checked the MSDN under the web browser class which led me to the StatusTextChanged.
So in my code I have
wb.StatusTextChanged += MainWindow_StatusChange;
then
private void MainWindow_StatusChange(Object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
this.toolStripStatusLabel2.Text = wb.StatusText;
}
Well, the tool.StripStatusLabel2.Text is empty.
Any help?
You have subscribed the wrong event.
Do this...
private void Form1_Load(object sender, EventArgs e)
{
WebBrowser wb = new WebBrowser();
wb.StatusTextChanged += new EventHandler(wb_StatusTextChanged);
wb.Navigate("http://www.google.de");
}
void wb_StatusTextChanged(object sender, EventArgs e)
{
this.toolStripStatusLabel2.Text = ((WebBrowser) sender).StatusText;
}

Using a control created on Form_Load in another event

I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
ComboBox kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
kombo.Items.Add("Panel"); //just an example
}
Is there a way to make this work?
Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.
What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.
// kombo is now scoped for use throughout this class
ComboBox kombo = null;
private void Form1_Load(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Name = "przycisk";
przycisk.Dock = DockStyle.Bottom;
przycisk.Text = "Wybierz";
// Assign to our kombo instance
kombo = new ComboBox();
kombo.Name = "kombo";
kombo.Dock = DockStyle.Bottom;
kombo.Items.Add("Przycisk");
kombo.Items.Add("Etykeita");
kombo.Items.Add("Pole tekstowe");
Controls.Add(kombo);
Controls.Add(przycisk);
przycisk.Click += new EventHandler(przycisk_Click);
}
private void przycisk_Click(object sender, EventArgs e)
{
// Using the kombo we created in form load, which is still referenced
// in the class
kombo.Items.Add("Panel"); //just an example
}
You will have to use the FindControl() method to find the object first.
private void przycisk_Click(object sender, EventArgs e)
{
ComboBox kombo = (ComboBox)FindControl("kombo");
kombo.Items.Add("Panel");
}

Tooltip not showing for nested control

I have the following piece of code that works great in all but one instance.
private void tbxLastName_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender)
{
var control = sender as TextEdit;
var maxChars = control.Properties.MaxLength;
tipCharacterCounter.Show(control.Text.Length + "/" + maxChars, this, control.Location.X, control.Location.Y - control.Height);
}
I just repeat this process from any textbox. Unfortunately, I have one control that is more complicated and I cannot get this to work. The Event portion looks like this -->
private void memDirectionsToAddress_Popup(object sender, EventArgs e)
{
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e)
{
GetRemainingChars(sender);
}
What I don't understand is if I replace the tipCharacterCounter portion with, say updating a label, it works fine. It's like the ToolTip is hidden or something but I have tried feeding Show() different points to no avail.
Ideas?
Which version of DXPerience are you using? I've tried the following code using DXperience 10.1.5 and it works fine here:
private void memoExEdit1_Popup(object sender, EventArgs e) {
MemoExPopupForm popupForm = (sender as DevExpress.Utils.Win.IPopupControl).PopupWindow as MemoExPopupForm;
MemoEdit meDirections = popupForm.Controls[2] as MemoEdit;
meDirections.EditValueChanging += new DevExpress.XtraEditors.Controls.ChangingEventHandler(meDirections_EditValueChanging);
}
void meDirections_EditValueChanging(object sender, DevExpress.XtraEditors.Controls.ChangingEventArgs e) {
GetRemainingChars(sender);
}
public void GetRemainingChars(object sender) {
TextEdit control = sender as TextEdit;
int maxChars = control.Properties.MaxLength;
tipCharacterCounter.ShowHint(control.Text.Length + "/" + maxChars, control, ToolTipLocation.RightBottom);
}

Categories

Resources