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.
Related
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
In C# Windows Form Application,
I have another class Race.
I want to declare an Object of Race class
And access this Object when a button is clicked
.
Race class:
class Race
{
int player;
int position;
}
Object creation:
Race Obj = new Race();
Accessing Object:
private void button1_Click(object sender, EventArgs e)
{
Obj.position++;
}
The question is, where to create Race class Object, so i can access it when button is clicked?
You'll need to declare it at the class level. In your case, that means it needs to be a field in your form:
public class Form1 : Form
{
private Race race;
public Form1()
{
race = new Race();
}
public void button1_Click(object sender, EventArgs e)
{
race.position++;
}
}
You can add it as a private member in the form class and instanciate it there.
So something like
public class Form1
{
private Race Obj = new Rate();
private void button1_Click(object sender, EventArgs e)
{
Obj.position++;
}
}
Also make sure that the class is accessible.
I am creating a winforms application which uses Gmaps.net. I am unable to alter the order in which on Load methods are being called. For some reason the map_load is being called before the man_Load. Is there any way to change the order of this ?
If I can provide any more information to help just ask.
Thanks!
Dan.
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
MessageBox.Show("main_load");
}
private void map_Load(object sender, EventArgs e)
{
MessageBox.Show("map_load");
}
}
It seems that you used the WinForms designer to create the map. The code behind is in the InitializeComponent() method and seems that the map is being loaded before the MainForm is loaded.
My recommendation is to create the map, once the MainForm has been loaded:
public partial class main : Form
{
public main()
{
InitializeComponent();
}
private void main_Load(object sender, EventArgs e)
{
Control map = CreateMap();
map.Docking = DockStyle.Fill;
this.Controls.Add(map);
}
private Control CreateMap()
{
// Create a new GMaps.NET object, intialize it and return
}
}
Hope it helps.
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)
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.