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

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.

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

c# creating reference between 2 forms

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();

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