Running Backgroundworker from another Form - c#

I have 2 form. Form1 and Form2. Form1 gets the date interval from pop-up form2.
Form2 has search button. Search button do below;
//form2
private void ubSAVE_Click(object sender, EventArgs e)
{
Form1 f1= new Form1();
f1.minDateCustomIO = Convert.ToDateTime(minDateString);
f1.maxDateCustomIO = Convert.ToDateTime(maxDateString);
f1.customIO = uosIO.CheckedIndex;
if (!f1.bgwCustomIO.IsBusy)
{
f1.bgwCustomIO.RunWorkerAsync(); // run bgw on form1
}
}
That bgw on Form1 loads data to datatable etc. And all is works perfectly. Just one except;
Then i use RunWorkerCompleted event on that bgw like below;
//form1
public void bgwCustomIO_RunWorkerCompleted(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
{
ultraGrid1.DataSource = dtCustomIOM; // Here is the problem.
MessageBox.Show(dtCustomIOM.Rows.Count.ToString());
}
MessageBox verifies that i have rows in that datatable however I can't assign to the grid.
No errors, Nothing. Also i changed to their modifiers to public.
Just it doesn't assign it. What is the reason behind of this, and how can i solve it ?

You have Form1 which you use to open Form2 and then in Form2 you create a brand new instance of Form1 so you have two instances of Form1 - you're updating the second Form1 correctly, but the first instance is unchanged.
You just need to pass a reference to Form1 in to Form2 when you create it. And then use the existing reference rather than creating a new one.
Do something like this:
public class Form2 : Form
{
private Form1 _form1;
public Form2(Form1 form1)
{
_form1 = form1;
}
}

Related

Unable to pass a text to another forms listbox

I am trying to pass a value from textbox1 of my form3 to the ListBox of my form1.
Here is the code for form3 :
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1(textBox1.Text);
f1.Show();
}
And here it is what is wrote in form1 :
public partial class Form1 : Form
{
public Form1(string something)
{
InitializeComponent();
listBox1.Items.Add(something);
}
The error is:
Form1' does not contain a constructor that takes 0 arguments.
Thank you for your help in advance!
You should take a look at the line where this error comes from (). I would wildly guess that there is a line in your code that uses a parameterless constructor like :
Form1 foo = new Form1();
or even if it is the starting form:
Application.Run(new Form1());
You should overload the constructor and not simply change it, since it is auto-generated it might highly probably be that it is already used in this form somewhere. Just add a second constructor:
public partial class Form1 : Form
{
public Form()
{
InitializeComponent();
}
public Form(string something)
{
InitializeComponent();
listBox.Items.Add(something);
}
}
Edit:
trying to pass a value from textbox1 of my form3 to the listbox of my form1
This is a slightly different problem then your error suggested in the first place. A different approach would be advisable. The constructor is useless, because it will create a different instance/object which is not the same that you see on the screen! In other words, all Elements will loose their values!
One of many solutions can be to create a method which would add items to the ListBox in the class Form1:
public void AddItemToListBox(string s)
{
listBox.Items.Add(something);
}
and pass the instance of the current window Form1 via Form2 to Form3. Have a variable in each class(Form2 and Form3) of type Form1
public Form1 temp_form1;
and pass the instance of the starting window Form1 to the variable temp_form1 when you call Form2 in the class Form1:
Form2 form2 = new Form2();
form2.temp_form1 = this; // "this" stands for the instance of the current object
and the same hold for Form3 when you call it in the class Form2:
Form3 form3 = new Form3();
form3.temp_form1 = this.temp_form1;
At the end just call the method to update the listbox in the class Form3:
temp_form1.AddItemToListBox("yourstring");
Do not delete the default constructor of your form, leave it there and add another one with your custom parameters under it instead.
public Form1()
{
//Default constructor
InitializeComponent();
}
public Form1(string something)
{
//Your custom constructor
InitializeComponent();
listBox1.Items.Add(something);
}

How can i call a method with parameter of form1 from form2 and plot on chart on form1

I have a form1 and form2, in form1 there is a chart which plots dot when i call a method defined in the form1 by using a button, now in form2 when i call a form1's method by passing two parameter to the method of form1 it should display dot in form1's chart , say the parameter is temperature and humidity. I hope there is a way to do this but I don't know this.Any help would be appreciated thanks in advance.
First of all, you should refactor your code and separate that graph plotting method in it's separate class and then you shouldn't face this situation.
In your case, you can have a Form1 instance in your Form2 and using that instance call the method like
Public class Form2 : Form
{
public Form1 frm1 = null;
public Form2(Form1 frm)
{
this.frm1 = frm;
}
protected void btn_click(object sender, EventArgs e)
{
frm1.Plottingmethod();
}
}

How to know when a button is clicked in form1 but know in form2 C#

C#. Hello... I have 2 forms => Form1 with a button and Form2. I want to know how i can mannage the event of the button of the Form2 in Form1. So when i make a click in form2 i want to know in form1 to do something
Form1 requires a reference to Form2. Then, you just hook up an event handler in Form1 to the button in Form2.
// code in form1 might look something like
public void SubscribeToEvents()
{
// get Form2 reference
var form2 = GetForm2Reference();
form2.Button.Click += ButtonOnForm2EventHandler;
}
public void ButtonOnForm2EventHandler(object sender, EventHandlerArgs e)
{
// some code
}
Form1 can subscribe to the events of Form2. You need a handle onto Form2 in Form1 but then you can simply write:
instanceOfForm2.Button.Click += handlerInForm1;
As stated by others but without making public controls or finding references if you already have them:
In form2 create a method to send other method to subscribe the button click:
public void SubscribeToButton(EventHandler eh) {
button1.Click += eh;
}
in form1 subscribe to form2:
form2.SubscribeToButton(this.f2_button_Click);
and have a method in it that will execute on click
private void f2_button_Click(object sender, EventArgs e) {
}
I actually written the wrong order, so please threat form1 as form2 and viceversa.

Change NotifyIcon on separate form

I have a form (Form1) that has a NotifyIcon on it. I have another form (Form2) that I would like to change the NotifyIcon's icon from. Whenever I use this code, I get an extra icon that shows up in the system tray, instead of changing the current icon:
Form1 (ico is the name of the NotifyIcon):
public string DisplayIcon
{
set { ico.Icon = new Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("Alerts.Icons." + value)); }
}
Form2:
Form1 form1 = new Form1();
form1.DisplayIcon = "on.ico";
I suspect is has something to do with creating a new instance of Form1 on Form2, but I'm not sure how to access "DisplayIcon" without doing this. Thanks.
UDPATE: I'm a little confused on writing the custom property on Form 2, would it be something like:
public Form Form1
{
set {value;}
}
I assume form1 at one point creates form2. At that point you can pass a reference of form1 to form2 so form2 can access the DisplayIcon property of form1.
So you would end up with something like
//Somewhere in the code of form1
public void btnShowFormTwoClick(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Form1 = this; //if this isn't done within form1 code you wouldn't use this but the form1 instance variable
form2.Show();
}
//somewhere in the code of form2
public Form1 Form1 { get;set;} //To create the property where the form1 reference is storred.
this.Form1.DisplayIcon = "on.ico";
Your suspicion is correct, you are creating a second instance of Form1 which results in a duplicate NotifyIcon.
You need a reference to Form1 from Form2 in order to set the DisplayIcon property on the correct instance.
A possible solution is to pass the reference from Form1 to Form2 when creating Form2 (I assume you create Form2 from Form1).
For example:
Form2 form2 = new Form2();
form2.Form1 = this; // Form1 is custom property on Form2 that you need to add
form2.Show();
On Form2 the custom property would be defined as:
//Note the type is Form1, in order to get to your public DisplayIcon property.
public Form1 Form1 { get;set;}

C# Using Multiple Forms

I have an application that has 2 forms. When I click a button on form 2 I want it to be able to change the text in form1:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.label1.Text = "Fred";
}
}
The compiler throws an error
How do I do this?
You are confusing forms and form instances. A form is just a class. When Form1 displays, what's displaying is an instance of the Form1 class. When Form2 displays, an instance of Form2 is displaying.
You're trying to use
Form1.label1.Text = "Fred";
But you can only set a field or member of an instance. You're referring to the class "Form1".
You need two things. I'll assume that Form2 is launched from a button on Form1. Add a constructor to Form2 which accepts an instance of Form1:
private Form1 _starter;
public Form2(Form1 starter) : this()
{
_starter = starter;
}
Then add a property to Form1 that exposes the label text: do not directly expose controls - only a given form should know what controls are on it:
public string LabelText
{
get {return label1.Text;}
set {label1.Text = value;}
}
Then have Form2 set that property:
private void button1_Click(object sender, EventArgs e)
{
_starter.LabelText = "Fred";
}
You probably launch an instance of Form2 from an instance of Form1, like this:
Form2 f2 = new Form2();
f2.Show();
If that's the case, you can then change text in the f2 instance of Form2 like this:
f2.label1.Text = "new text";
Note that you will need to make label1 a public field (not a good practice), or encapsulate it using a property. Hope this helps.

Categories

Resources