c# creating reference between 2 forms - c#

My question may sound weird,but I have no idea on how to create a reference between 2 windows forms(for example to create a referrence and call it TheMainForm1).I'm fighting with this since 2 days(yes I know...) What I'm trying to do is to hide the Form1,but until now the only method I found is:
this.Hide();
Form2 form2 = new Form2();
form2.Show();
But the above code creates a new Form1...
Please guide me on how to create a reference,I'm trying to learn c#.
HUGE thanks for any replay!

Why don't you just pass it in in the constructor for Form1 and store it in a member variable?
public Form1(Form2 form2)
{
// store in member
_form2 = form2;
}

You can try this solution:
There should be a Program.cs which contains a Program class. Add two static properties (or fields) and use them.
static class Program
{
internal static Form1 form1;
internal static Form2 form2;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
form1 = new Form1();
form2 = new Form2();
Application.Run(form1);
}
}
And if you want to switch from Form1 to Form2, you can use this:
this.Hide();
Program.form2.Show();

Related

Running Backgroundworker from another Form

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;
}
}

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 do i pass/use a method from form1 in a new class?

In form1 i have a method i changed it to public:
public bool ComparingImages(Bitmap File1)
{
}
Then i created a new class and did:
private Form f1;
public SimulateDownloads(Form form1)
{
f1 = form1;
}
Then in the new class bottom in another method i did:
bool downloaded = f1.ComparingImages(test);
But f1 does not contain ComparingImages
I tried also to add to form1 constructor:
private SimulateDownloads simdownloads;
public Form1()
{
InitializeComponent();
simdownloads = new SimulateDownloads(this);
But i don't want to run the program first i want to be able now without running the program to call the method ComparingImages in form1 to call it from the new class.
The problem you're having is that your SimulateDownloads method is taking in an object of type Form instead of type Form1. Remember, you put the public method on a class called Form1, you did not add it to the base Form class!
Try this:
// (This is in your other form, not Form1)
private Form1 f1;
public SimulateDownloads(Form1 form1)
{
f1 = form1;
}
Then you should be able to do this in your other form:
bool downloaded = f1.ComparingImages(test);
It sounds like your ComparingImages shouldn't be in the form1 at all. Create a new class Utils or similar and put ComparingImages in that class. Then you can use
bool downloaded = new Utils().ComparingImages(test); //or Utils.ComparingImages(test); if you make it static
from both SimulateDownloads and form1

C# on Program start up, the 2nd form is already running?

Hi i got a program that have two forms..
it goes like this
Form1(my main form)
From2
Form2 will only show if it is called by form1,
basically when i start the program form1 is the only one there.
but whats weird is.. the timer inside Form2 is already running.
Anyone got an idea why this is happening?
UPDATE:
here the code i used
public partial class MainForm : Form, IMessageFilter
{
public Form2 f2 = new Form2();
}
public void ShowForm2()
{
f2.Show();
}
When you said:
public Form2 f2 = new Form2 (); // its inside public partial class MainForm
Is it like this:
public class MainForm {
public Form2 f2 = new Form2();
public void ShowForm2() {
f2.Show();
}
}
You should not do that, else you should create an instance of Form2 when needed, like this:
public class MainForm {
public void ShowForm2() {
Form2 f2 = new Form2();
f2.Show();
}
}
Now you are sure that the instance will exist only when the message is received and will avoid to running the timer in Form2 if it is hidden.
So based on your clarification, you're instantiating Form2 when Form1 is created... and so I'm guessing that the Timer is enabled by default and is instantiated when Form2 is instantiated, so it starts right away.
You need to change it so that the timer isn't enabled until you show Form2 (maybe in the Load event) or some other point later.
More code might be helpful though - I still feel like I'm stabbing the dark here.

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;}

Categories

Resources