How can I navigate WebBrowser1 to a website, from another class file?
Bassically I have 2 classes, default one (Form1) and second (MyClass), in MyClass I have a method with is navigating WebBrowser1(from Form1) to some random site:
class MyClass : Form1
{
public void navigate()
{
webBrowser1.Navigate("https://www.google.pl/webhp?sourceid=chrome-instant&ion=1&espv=2&ie=UTF-8");
}
}
Button
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
MyClass mc = new MyClass();
mc.navigate();
}
}
Visual Studio is not showing any errors, and application is not crashing after I call this method(by button), but it still not working.
Im a Newb, soo please help me in easiest way.
MyClass:
public class MyClass
{
public void Navigate(WebBrowser webBrowser)
{
webBrowser.Navigate(.....); //you can navigate webBrowser in Form1 from here
}
}
in Form1:
private void button2_Click(object sender, EventArgs e)
{
MyClass mc = new MyClass();
mc.Navigate(webBrowser1); // send the reference of webBrowser1 to MyClass
}
Because you are inheriting MyClass from Form1, newing up an instance of MyClass in the button click handler will create a new instance of the form, which, I am assuming, is not what you want.
You have two options:
First Option
Remove MyClass altogether and just have the Form1 class have this code:
public partial class Form1 : Form
{
public void Navigate(string url)
{
webBrowser1.Navigate(url);
}
private void button2_Click(object sender, EventArgs e)
{
this.Navigate("https://www.google.com");
}
}
Second Option
Or, if you are really sure that you want to subclass Form1, then keep MyClass but make two changes. Do not new up an instance of MyClass from within the button click handler. Instead, just write:
public class MyClass : Form1
{
public void Navigate(string url)
{
webBrowser1.Navigate(url);
}
private void button2_Click(object sender, EventArgs e)
{
this.Navigate("https://www.google.com");
}
}
and, in the Program.cs file, change the line:
Application.Run(new Form1());
to:
Application.Run(new MyClass());
At any rate, even though either of the two options will work just fine, you need to ask yourself why you are creating MyClass.
I don't understand your code structure but I see that you want to control the navigation of WebBrowser control from a different class, is that right?
In your MyClass:
public class MyClass
{
// Holds the instance of web browser control
public WebBrowser webBrowser;
//Inject the WebBrowser control
public MyClass(WebBrowser webBrowser)
{
this.webBrowser = webBrowser;
}
public void navigate(string url)
{
this.webBrowser.Navigate(url);
}
}
And in your form class:
private void button2_Click(object sender, EventArgs e)
{
MyClass mc = new MyClass(webBrowser1);
mc.navigate("http://google.com/");
}
You can also store a single reference of MyClass in your Form class
Related
I'm trying to call methods from class "Form1" from an other class.
Here's my Code
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
MessageBox.Show("loaded");
orders.ObjectForScripting = new ScriptInterface();
}
private void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { }
private void button1_Click_1(object sender, EventArgs e) { }
}
[System.Runtime.InteropServices.ComVisibleAttribute(true)]
public class ScriptInterface
{
public void callMe(string currid)
{
MessageBox.Show(currid);
// the following throws security error
Form1.webBrowser2.Navigate("http://www.mywebpage.com/client/index.php?id="+currid);
}
}
}
INFO: I have 2 WebBorwsers. I'm catching events from webBrowser1 for updating webBrowser2.
My problem is, that i cannot call the webbrowser2 methods outside from Form1.
Any Ideas how i can solve this problem?
Your WebBrowser components are not static (this is a good thing), therefore you cannot refer to them directly off of Form1 as your code sample is attempting to do. You must obtain a reference to a Form1 instance and then you can call methods on them so long as they are marked public. Depending on your needs it is probably even better to just pass along a reference directly to your WebBrowser components
Perhaps something like this:
orders.ObjectForScripting = new ScriptInterface(this.webBrowser2);
...
public class ScriptInterface
{
private WebBrowser _browser;
public ScriptInterface(WebBrowser browser)
{
_browser = browser;
}
public void callMe(string currid)
{
_browser.Navigate("http://www.mywebpage.com/client/index.php?id="+currid);
}
}
Form1 in:
Form1.webBrowser2.Navigate(...)
Is not an object, but it is your type. You need to create an instance of your class, or if you prefer, create an object to be able to call it's method:
Form1 form = new Form1();
form.webBrowser2.Navigate(...)
On top, your method are flagged private, which mean they can only be call from inside your instanced. You should flag them public if you want other object to be able to call them.
public void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { ... }
Or if you don't want to create an instance, you can declare this method as being static
public static void webBrowser2_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e) { ... }
But you won't be able to access the fields that this class define unless they are static too.
I'm a newbie in c# and visual studio, but not programming in general.
I searched for answer to my question for 3 days and I found plenty of them, but for some weird reason (I'm sure I'm missing something very obvious) I cannot get it to work.
I think it's the most basic question newbies like me ask.
I have a form (Form3) with a text box and a button (I set it up is just for testing purposes).
I want to populate and read this text box from another class. I understand the most proper way to do this is to create a property in Form3.cs with GET and SET accessors. I did that but I cannot get it to work. I'm not getting any error messages, but I'm not able to set the value of the text box either. It just remains blank.
Here's my sample code:
namespace WindowsFormsApplication1
{
public partial class Form3 : Form
{
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
public Form3()
{
InitializeComponent();
}
private void Form3_Load(object sender, EventArgs e)
{ }
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
}
public class a
{
public static void b()
{
Form3 v = new Form3();
v.setCodes = "abc123";
}
}
}
Can someone lend me a hand solving this?
The problem is you are setting the value to a new instance of the form. Try something like this:
public partial class Form3 : Form {
public string setCodes
{
get { return test1.Text; }
set { test1.Text = value; }
}
private A a;
public Form3()
{
InitializeComponent();
a = new A(this);
}
private void button1_Click(object sender, EventArgs e)
{
a.b();
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
public class A
{
private Form3 v;
public a(Form3 v)
{
this.v = v;
}
public void b()
{
v.setCodes = "abc123";
}
}
You're creating a brand new Form3() instance.
This does not affect the existing form.
You need to pass the form as a parameter to the method.
Try this:
public partial class Form3 : Form
{
/* Code from question unchanged until `button1_Click` */
private void button1_Click(object sender, EventArgs e)
{
a.b(this);
}
}
public class a
{
public static void b(Form3 form3)
{
form3.setCodes = "abc123";
}
}
This passes the current instance of the form to the other class so that it can update the setCodes property. Previously you were creating a new form instance rather than updating the current form.
Sending form instance to other other class
Form1 objForm1=new Form1();
obj.Validate(objForm1);
Easy way to access controls in another class by modifying Controls Private to Public in the Form(Designer.cs)
Currently have a mainForm.cs which calls a class panel.cs
panel.cs holds multiple pictureboxes with events such as _click, _mousedown, _mouseup
I wish to call a function in mainForm.cs from panel.cs
do i need to use a callback / delegate. Is there another way to access this function
I have tried
Main main = new Main();
main.functioninMain does not work
any help or direction would be appreciated.
for example
panel.cs
private void pb_button1_Click(object sender, EventArgs e)
{
this.BeginInvoke(new Action(main.functioninMain));
}
You should not instantiate another object of MainForm, which creates a duplicate and all the objects withing the new MainForm will not have the values of your actual MainForm.
Approach 1
You can try creating a static instance of you MainForm like below
public partial class MainForm : Form
{
public static MainForm Instance = null;
public MainForm()
{
InitializeComponent();
Instance = this;
}
public SomeMethod()
{
}
}
Now if you have your panel class, then you can easily access public methods and variables of MainClass
class Panel : Form
{
public Panel()
{
MainForm.Instance.SomeMethod();
}
}
Edit: Pass Handle as parameter to the form (From Ben Voigt's suggestion)
Approach 2:
As Ben suggested, it is also important to have a safer code, so much cleaner approach will be passing handle of the control as parameter and then to access them.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
TestClass test = new TestClass();
test.ModifyText(textBox1);
}
}
public class TestClass
{
public void ModifyText(TextBox textBox)
{
textBox.Text = "New text";
}
}
If we can use delegate & events something like this
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Panel formPanel = new Panel();
formPanel.OnPanelClick += new Panel.OnPanelButtonClick(formPanel_OnPanelClick);
formPanel.Show();
}
void formPanel_OnPanelClick(string a)
{
MessageBox.Show(a);
}
}
public partial class Panel : Form
{
public delegate void OnPanelButtonClick(string a);
public event OnPanelButtonClick OnPanelClick = null;
public Panel()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (OnPanelClick != null)
{
OnPanelClick("from Panel.cs");
}
}
}
You should avoid circular dependencies at all costs (class A depends on class B, and class B depends on Class A).
There are so many ways to do this, but perhaps one of the easier ways is to use an interface. mainForm.cs implements it like this:
public class mainForm : IMainForm {
// This method is defined in the interface, so mainForm
// must implement it.
public string GetStringFromMainForm() {
return "Hello from MainForm";
}
public void CreatePanel() {
// pass in a reference to myself so panel knows how to
// talk to me.
var panel = new Panel(this);
}
}
And IMainForm (the interface) could look like this:
public interface IMainForm {
string GetStringFromMainForm();
}
Then your panel class can talk to the Main form without having to reference mainForm.cs explicitly:
public class Panel {
// use a private variable to keep track of main form
private IMainForm _mainForm;
// Constructor: pass in a class that implements IMainForm. It isn't
// a type of MainForm, so there's no dependency on the concrete class.
public Panel(IMainForm mainForm) {
_mainForm = mainForm;
}
public void TalkToMainForm() {
var resultFromMainForm = _mainForm.GetStringFromMainForm();
Console.WriteLine(resultFromMainForm);
}
}
Hi I'm creating test application that has a user control with a Button that will be reference in a Form.
Is it possible to bind interface
public interface ICRUD
{
void Test();
}
to the user Control button1 click event
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//CALL ICRUD.Test() execute when click on form1 and then show I am Clicked
}
}
so that
i just need to implement only the interface functions to my form1.
Form1:
using System.Windows.Forms;
namespace TEST
{
public partial class Form1 : Form , ICRUD
{
public Form1()
{
InitializeComponent();
}
public void Test()
{
MessageBox.Show("I am Clicked");
}
}
}
Thanks in Regards.
This doesn't seems right to me. It's probably not the Form that should be implementing this interface. What's more, the interface doesn't bring anything here.
But if you really want to do that, you can access the ParentForm property, cast it to your interface and then call the method:
private void button1_Click(object sender, EventArgs e)
{
var crud = (ICrud)ParentForm;
crud.Test();
}
Also, the convention in .Net is to write abbreviations (at least the long ones) the same as other words, so you should name your interface ICrud.
private void button1_Click(object sender, EventArgs e)
{
for(var parent = this.Parent; parent != null; parent = parent.Parent)
{
var crud = parent as ICRUD;
if (crud != null)
{
crud.Test();
break;
}
}
}
I have a DataGridView in Form1 and I'm using this code to display another form called Generator:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator();
// Form gen = new Generator(Form this); //* I tried this but is not working *//
gen.Show();
}
In the Generator form I need to read or modify something in the datagridview which is in the Form1.
public partial class Generator : Form
{
public Form myForm;
public Generator()
{
InitializeComponent();
}
public Generator(Form frm)
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.mydatagridview.! // this is not working
}
}
How can I resolve this problem, so I can manipulate the DataGridView from the Generator form?
Form 1:
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this.mydatagridview);
gen.Show();
}
Generator Form:
DataGridView _dataGridView;
public Generator(DataGridView dataGridView)
{
InitializeComponent();
this._dataGridView = dataGridView;
}
private void button1_Click(object sender, EventArgs e)
{
this._dataGridView...! // this will work
}
Things that you must do, and know (just tips, you are not forced to do these but I believe you will be a better programmer if you do! ;)
Always call InitializeComponent() in all form constructors. In your sample you didn't call it in one of the constructors.
C# knows only information of the type you have passed. If you pass a Form, then you only get Form properties (i.e. the properties of type Form), not the properties of your own form.
Try to encapsulate things. Do not pass a whole form to another form. Instead, pass things that you would like to use on the other form.
A few things are going on here.
You have to use the constructor of Generator that take in a form as a parameter.
You have to expose the datagridview as a public or internal property on the form that you will pass into Generator.
The normal Form class will not know about this property, so you should cast the variable appropriately.
You should call the default constructor of Generator when the other constructor is used to make sure everything is initialized correctly. See code sample below.
Something like this should work:
public class Generator
{
private MyForm myForm;
public Generator()
{
InitializeComponent();
}
public Generator(MyForm frm) : this() // DON'T FORGET THIS()
{
myForm = frm;
}
private void button1_Click(object sender, EventArgs e)
{
myForm.MyDataGridView... // Yay, it works!
}
}
public class MyForm : Form
{
public MyForm()
{
InitializeComponent(); // a datagridview is created here, say "datagridview1"
}
public DataGridView MyDataGridView
{
get { return datagridview1; }
}
}
And then in your button click event (which I assume is defined somewhere in MyForm):
private void button1_Click(object sender, EventArgs e)
{
Form gen = new Generator(this);
gen.Show();
}
The easiest way from there is to open the designer for the DataGridView (myDataGridView) on Form1 and set the Modifiers property from private to internal or public
This will let you call myForm.myDataGridView from the Generator form.