Raising event when the checkbox of a webbrowser is checked in C# - c#

I want to raise an event when the checkbox is checked in a web browser.
Here is what I have come up with:
public class HTMLCheckBoxArgs : EventArgs
{
public Guid ElementGuid;
}
public delegate void CheckBoxChangeEventHandler(object sender, HTMLCheckBoxArgs e);
public event CheckBoxChangeEventHandler CheckPressed;
void OnCheckPressed(HTMLCheckBoxArgs args)
{
if (CheckPressed != null)
CheckPressed(this, args);
}
protected void CheckBoxEvents()
{
HtmlElementCollection elements = webBrowser1.Document.GetElementsByTagName("input");
foreach (HtmlElement el in elements)
{
HTMLCheckBoxArgs checkbargs = new HTMLCheckBoxArgs();
if (el.GetAttribute("type") == "checkbox")
{
checkbargs.ElementGuid = Guid.Parse(el.Id);
el.AttachEventHandler("onclick", (sender, args) => OnCheckBoxClicked(el, checkbargs));
}
}
}
public void OnCheckBoxClicked(object sender, EventArgs args)
{
OnCheckPressed((HTMLCheckBoxArgs)args);
}
The problem that I have is the fact that this way I raise the event when the check box is clicked.
I want the event to be raised when the check box is checked.

Try this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<html><body><input type=\"checkbox\" id=\"chk\" value=\"some\">some thing</body></html>";
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
foreach (HtmlElement el in webBrowser1.Document.All)
{
if (el.GetAttribute("type") == "checkbox")
{
el.AttachEventHandler("onclick", (send, args) => OnElementClicked(el, EventArgs.Empty));
}
}
}
private object OnElementClicked(HtmlElement el, EventArgs eventArgs)
{
if (el.GetAttribute("checked") == "True")
{
MessageBox.Show("checked");
}
return false;
}
}

Related

Can not transfer values in list between classes

my problem is that i can't transfer values in the list between two classes in WFA
public partial class Example : Form
{
public List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
new Example2().Show();
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
public partial class Example2 : Form
{
static Example ex = new Example();
private void Btn2_Click(object sender, EventArgs e)
{
ex.myList.Add("something");
Close();
}
Form "Example" is shown first. Then i click "Btn1" on the screen, and form "Example2" appears. When i click "Btn2" on "Example2" form, myList should get new value of "something" and "Example2" closes. But this part of script
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
shows that myList has no values (myList.Count equals 0).
What can i do?
you need to reference existing Example form with data from Example2 form. static Example ex is another (static) instance, which doesn't have data.
public partial class Example : Form
{
private List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
var e2 = new Example2();
e2.SetMainForm(this);
e2.Show();
}
public void AddItem(string item)
{
myList.Add(item);
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
}
public partial class Example2 : Form
{
private Example ex;
private void Btn2_Click(object sender, EventArgs e)
{
ex.AddItem("something");
Close();
}
public void SetMainForm(Example e1)
{
ex = e1;
}
}

ArgumentOutOfRangeException on an object listview when hitting tab

I have an object list view that has two text columns. When I edit the left column and hit tab to go to the right I receive a "ArgumentOutOfRangeException" with an index of -1. Looks like the index is something internal to the list view because I debugged my application and found no errors. Here is the code :
public partial class SummaryOverviewSettingsDlg : Form
{
private List<SummaryDataset> _localSummaryDatasets = new List<SummaryDataset>();
private bool _includeLimits;
private SummaryOverviewSettings _summaryOverviewSettings;
public bool IncludeLimits { get { return _includeLimits; } }
public SummaryOverviewSettingsDlg(SummaryOverviewSettings summaryOverviewSettings)
{
InitializeComponent();
if (summaryOverviewSettings.Datasets != null)
{
_localSummaryDatasets.AddRange(summaryOverviewSettings.Datasets);
}
_summaryOverviewSettings = summaryOverviewSettings;
}
private void DataFilesListDlg_Load(object sender, EventArgs e)
{
foreach(var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
LimitsCheckbox.Checked = _summaryOverviewSettings.ShowLimits;
}
private void OlvFilePaths_CellRightClick(object sender, CellRightClickEventArgs e)
{
var contextMenuSymbol = new ContextMenuStrip();
ToolStripItem item;
item = contextMenuSymbol.Items.Add("Add sample");
item.Click += ContextMenuAddFilePath;
if (e.Model != null)
{
contextMenuSymbol.Items.Add("-");
item = contextMenuSymbol.Items.Add("Delete sample");
item.Click += ContextMenuDeleteFilePath;
}
olvFilePaths.ContextMenuStrip = contextMenuSymbol;
}
private void ContextMenuAddFilePath(object sender, EventArgs e)
{
var item = new SummaryDataset()
{
SampleName = "Sample",
Path = "Path"
};
_localSummaryDatasets.Add(item);
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void ContextMenuDeleteFilePath(object sender, EventArgs e)
{
if (olvFilePaths.SelectedObject != null)
{
var item = (SummaryDataset)olvFilePaths.SelectedObject;
olvFilePaths.RemoveObject(item);
_localSummaryDatasets.Remove(item);
}
}
private void OlvFilePaths_CellEditFinished(object sender, CellEditEventArgs e)
{
if (e.Control is TextBox textBox)
{
var oldValue = (string)e.Value;
var newValue = (string)e.NewValue;
var col = e.Column.AspectName;
var index = e.ListViewItem.Index;
if (newValue != oldValue)
{
if (col == "SampleName")
{
_localSummaryDatasets[index].SampleName = newValue;
}
else
{
_localSummaryDatasets[index].Path = newValue;
}
}
}
// Rebuild the list in the GUI
olvFilePaths.ClearObjects();
foreach (var dataFile in _localSummaryDatasets)
{
olvFilePaths.AddObject(dataFile);
}
olvFilePaths.AutoResizeColumns();
}
private void OkButton_Click(object sender, EventArgs e)
{
_summaryOverviewSettings.Datasets.Clear();
_summaryOverviewSettings.Datasets.AddRange(_localSummaryDatasets);
_summaryOverviewSettings.ShowLimits = _includeLimits;
DialogResult = DialogResult.OK;
Close();
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void LimitsCheckbox_CheckedChanged(object sender, EventArgs e)
{
_includeLimits = LimitsCheckbox.Checked;
}
}

How to use multiple WebBrowser DocumentCompleted

I would like to be able to open a web page, log in, and be able to do things on the next page. DoPlans will not execute. Here is my code so far... (I do know that I have t for a lot of the variables)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.Navigate("t");
}
private void DoPlans(object sender,
WebBrowserDocumentCompletedEventArgs e)
{
textBox1.AppendText("started");
webBrowser1.Document.GetElementById("t").InvokeMember("Click");
webBrowser1.Document.GetElementById("t").InvokeMember("Click");
foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("input"))
{
if (elem.GetAttribute("value") == "Submit Weekend Plans")
{
elem.InvokeMember("Click");
}
}
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
webBrowser1.Document.GetElementById("username").SetAttribute("value", "t");
webBrowser1.Document.GetElementById("password").SetAttribute("value", "t");
foreach (HtmlElement elem in webBrowser1.Document.GetElementsByTagName("input"))
{
if (elem.GetAttribute("value") == "Login")
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(DoPlans);
elem.InvokeMember("Click");
}
}
}
}
Try something like this:
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
switch (e.Url.ToString())
{
case "home page":
{
// fire click
break;
}
case "next page":
{
// handle logged in user
break;
}
}
}

User control and how to get button events from form

i created user control which looks like this:
and code behind looks like:
using System;
using System.Windows.Forms;
namespace Controlls
{
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
}
private void pbNazaj_Click(object sender, EventArgs e)
{
}
private void pbNovo_Click(object sender, EventArgs e)
{
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
}
}
}
now i move this UC to form but there is a problem. How to get this events from UC. I need to implement click events for buttons in form where i use this user control. What i can see is that i can use it like one component and i can not get separate buttons from it.
Add 2 event handlers for yor control, and subscribe to the toobox event at the place you use it:
using System;
using System.Windows.Forms;
namespace Controlls
{
public enum ToolboxButtonType { Potrdi, Brisi, Novo, Nazaj, Naprej }
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
private void pbNaprej_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Novo);
}
private void bpbBrisi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Brisi);
}
private void pbPotrdi_Click(object sender, EventArgs e)
{
OnToolboxClick(ToolboxButtonType.Potrdi);
}
private void txtOd_TextChanged(object sender, EventArgs e)
{
OnTextChanged(txtOd.Text);
}
}
private OnToolboxClick(ToolboxButtonType button)
{
if (ToolboxClick != null)
{
ToolboxClick(this, new ToolboxClickEventArgs(button));
}
}
private OnTextChanged(string text)
{
if (ToolboxTextChanged!= null)
{
ToolboxTextChanged(this, new ToolboxTextChangedEventArgs(text));
}
}
public class ToolboxTextChangedEventArgs: EventArgs
{
public string Text { get; private set; }
public ToolboxClickEventArgs(string text) { Text = text; }
}
public class ToolboxClickEventArgs : EventArgs
{
public ToolboxButtonType Button { get; private set; }
public ToolboxClickEventArgs(ToolboxButtonType button) { Button = button; }
}
public event EventHandler<ToolboxClickEventArgs> ToolboxClick;
public event EventHandler<ToolboxTextChangedEventArgs> ToolboxTextChanged;
}
For example:
//Toolbar toolbar = new Toolbar();
toolbar.ToolboxTextChanged += (s, e) => { btn1.Text = e.Text; };
toolbar.ToolboxClick += (s, e) =>
{
swith(e.Button)
{
case ToolboxButtonType.Brisi: //
break;
default:
break;
}
};
You need to create event in MainForm class and generarate it in hand mode.
If you also draw your icons on canvas or smth, you need to handle onClick your MainForm, than calculate mouse click position, and generate event that you were create.
If it amount of buttons or smth that has their own onClick events, you can hang on this buttons event the action that will generate your even in main form.
Answer what approach you use, and I past some code.
ADDED:
public partial class Toolbar : UserControl
{
public Toolbar()
{
InitializeComponent();
}
public enum ToolBarCommands
{
Naprej, Nazaj, Novo
}
public Action<object, ToolBarCommands> MenuItemClick;
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
}
How to use it? easy:
// Define section
var toolbar = new Toolbar();
toolbar.MenuItemClick += MenuItemClickHandler;
...
MenuItemClickHandler(object sender, Toolbar.ToolBarCommands item)
{
switch(item)
{
case Toolbar.ToolBarCommands.Naprej:
// Naperej handler
break;
case Toolbar.ToolBarCommands.Nazaj:
// Nazaj handler
break;
case Toolbar.ToolBarCommands.Novo:
// Novo handler
break;
}
}
ADDED:
If names of your buttons same as enum members you can cut this part of code:
private void pbNaprej_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Naprej);
}
private void pbNazaj_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Nazaj);
}
private void pbNovo_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
MenuItemClick(this, ToolBarCommands.Novo);
}
It will look's like one method (hang this method to all your menu element events)
private void pbItem_Click(object sender, EventArgs e)
{
if(MenuItemClick != null)
{
ToolBarCommands command;
var res = ToolBarCommands.TryParse(((Button)sender).Name, command)
if(res == true)
MenuItemClick(this, command);
}
}

Detect scroll on a WebBrowser control

I have the following code that, weirdly, works for a couple of seconds and then stops working (my event handler method stops being called):
public partial class Form1 : Form
{
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
webBrowser1.Navigate("google.com");
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
if (!webBrowser1.IsBusy && webBrowser1.Url == e.Url && webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
HTMLWindowEvents_Event windowEvents = webBrowser1.Document.Window.DomWindow as HTMLWindowEvents_Event;
windowEvents.onscroll += new HTMLWindowEvents_onscrollEventHandler(windowEvents_onscroll);
}
}
private void windowEvents_onscroll()
{
HtmlDocument htmlDoc = webBrowser1.Document;
int scrollTop = htmlDoc.GetElementsByTagName("HTML")[0].ScrollTop;
string text = scrollTop.ToString();
}
}
OK Found a solution:
protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
{
Follow();
if (!IsBusy && Url == e.Url && ReadyState == WebBrowserReadyState.Complete)
{
Document.Window.AttachEventHandler("onscroll", DocScroll);
}
}
If attached that way it works OK (so far...). Don't even need to use mshtml.

Categories

Resources