Reputation: 728
I've been trying to change the value of a checkbox control defined in an html file,this html file is shown in a webbrowser control and the webbrowser itself is defined as a user control in C# I am willing to set the value of the checkbox control (defined in the html file and user control) from the form that contains my usercontrol The related code in user control:
public bool _checkBoxProperty
{
set
{
if (webBrowser1.Document != null && webBrowser1.Document.GetElementById("Checkbox1") != null)
{
bool s = false;
string chpro = webBrowser1.Document.GetElementById("Checkbox1").GetAttribute("checked").ToString();
if (chpro == "false")
s = false;
s = value;
webBrowser1.Document.GetElementById("Checkbox1").SetAttribute("checked", value.ToString());
}
}
get
{
if (webBrowser1.Document != null && webBrowser1.Document.GetElementById("Checkbox1") != null)
{
{
string bls = webBrowser1.Document.GetElementById("Checkbox1").GetAttribute("checked");
return Convert.ToBoolean(bls);
}
}
else
return false;
}
}
this piece of code brings the checkbox property in my form and I can set its value,but when I run the program it resets itself to null, I've been working on this piece of code for days and I fully appreciate some help :)
Upvotes: 0
Views: 1073
Reputation: 10153
You can do it simply,set web-browser property Modifiers
to Public
in user control:
In your form you have access to web-browser directly:like this:
private void Form1_Load_1(object sender, EventArgs e)
{
userControl11.usercontrolbrowser.DocumentText="htmlfile";
userControl11.usercontrolbrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(usercontrolbrowser_DocumentCompleted);
}
void usercontrolbrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//get
userControl11.usercontrolbrowser.Document.GetElementById("Checkbox1").GetAttribute("checked");
//set
userControl11.usercontrolbrowser.Document.GetElementById("Checkbox1").SetAttribute("checked",true or false value);
}
And in OOP:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public bool _checkBoxProperty
{
get
{
if (usercontrolbrowser.Document != null)
{
return Convert.ToBoolean(usercontrolbrowser.Document.GetElementById("Checkbox1").GetAttribute("checked"));
}
else
{
return false;//error
}
}
set
{
if (usercontrolbrowser.Document != null)
{
usercontrolbrowser.Document.GetElementById("Checkbox1").SetAttribute("checked", value.ToString());
}
}
}
public void DocHtml(string dochtml)
{
usercontrolbrowser.DocumentText = dochtml;
}
}
private void Form1_Load_1(object sender, EventArgs e)
{
userControl11.DocHtml("htmlfile");
}
private void getcheckbox()
{
var getval= userControl11._checkBoxProperty;
userControl11._checkBoxProperty = false;//set value
}
Upvotes: 2