Unable to pass a text to another forms listbox - c#

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

Related

how to use listview data from another class?

im working on vb(or c#).
For example, i have a three class with different designs.
Class 1 of the main design has a function that receives data from the equipment, and this function returns the data as an array.
Class 2 of the second design invokes a function in class 1 to store and display the data in the listview.
I want to output and use the data of the listview of the second designer's class in the third designer.
To summarize, class 1 gets data, class 2 calls class 1 and outputs data to listview. Class 3 tries to use class 2's listview data.
Did my intentions pass exactly? I'm sorry I do not English well.
Thank you for your attention! :)
Your class2 should hold data in raw form (say list of strings) it should use this list to display data on listview and also expose this data to class3
I have created a sample program showing how to do SO!
lets take three forms Form1, Form2, and Form3
Form1 (class1) : this will hold the actual data, which we need to display on listview of Form2 and also we need to pass this information to Form3 via Form2
public partial class Form1 : Form
{
//Data which need to be shown in list view
//and also need to pass to form3 (class3)
public List<string> Data = new List<string>();
public Form1()
{
InitializeComponent();
Data.Add("item1");
Data.Add("item2");
}
private void btnForm2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
}
Form2 (class 2) : this class will hold data of Form1 as raw form (here list of string) so it doesn't have to expose entire listview to Form3 .
you see, form3 just need data, not control on which it is displayed, so better to keep control (listview) not accessible by external codes.
public partial class Form2 : Form
{
Form1 m_form1;
List<string> m_dataOfListview;
//exposing row data publically so form3 can use it
//note that, it is read only, so form3 can only read it. Not modify
public List<string> DataOfListView
{
get { return m_dataOfListview; }
}
public Form2(Form1 form1)
{
InitializeComponent();
m_form1 = form1;
}
private void btnFetchData_Click(object sender, EventArgs e)
{
m_dataOfListview = m_form1.Data;
foreach(string data in m_dataOfListview)
listView1.Items.Add(data);
}
private void btnShowForm3_Click(object sender, EventArgs e)
{
Form3 form3 = new Form3(this);
form3.Show();
}
}
and at last Form3 (class 3)
public partial class Form3 : Form
{
Form2 m_form2;
public Form3(Form2 form2)
{
InitializeComponent();
m_form2 = form2;
//here in this class you can get value of data of listview
List<string> dataOfListView = form2.DataOfListView;
}
}

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

Using the selected index of a combobox in an If statement of a different form

I'm trying to reference the selected index of a combobox on my mainform in an if statement inside a method on a second form. Some google searching has confused me a bit. The most obvious answer I can see is just making the combobox control on the mainform public, however the websites I've been reading seem to indicate that this is not the prefered method? If this is the case what is the prefered method? I've coded in the secondary constructor method on the second form to accept the first form as a parameter when called, for example:
Form2 form = new Form2(this);
form.Show();
And on the second form:
public partial class Form2 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 fr1)
{
InitializeComponent();
form1 = new Form1();
form1 = fr1;
So I thought I could just do something like form1.combobox1.SelectedIndex, but no dice....what is the 'community prefered' method to go about doing this?
Well you can just return SelectedIndex property of the combobox by doing something like this in Form1 class or whatever form that is containing the combobox.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public int SelectedIndex
{
get
{
return comboBox.SelectedIndex;
}
}
}
Then in order to call it, just continue what you were doing before
public partial class Form2 : Form
{
Form1 form1;
public Form2()
{
InitializeComponent();
}
public Form2(Form1 fr1)
{
InitializeComponent();
// get rid of this line it's unnecessary
// form1 = new Form1();
form1 = fr1;
}
}
and call the property wherever needed in your Form2 class like this form1.SelectedIndex.
Avoid this section if it's confusing, but you don't really need to create a field for Form1. Use Form's ParentForm instead and cast it to Form1 whenever needed like ((Form1)this.ParentForm).SelectedIndex
On your main form, create a public property that returns the combobox.

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