how to use listview data from another class? - c#

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

Related

how to call a combobox from another class C#

I have 2 forms, let's call them form1 and form2. Inside form1 i have created a method that connects to an access database, and also inserts the values from the database into a combobox. My question is : how do i call my method which is inside my form1, for a combobox inside form2? The error that i get says The name "MyComboBoxName" does not exist in the current context.
I have tried to use inheritence between my 2 classes, but then i get 2 of the same comboboxes.
The easiest solution is to define your method statically in the first form:
public partial class Form1 : Form
{
public static void CustomMethod()
{
//Your codes
}
public Form1()
{
InitializeComponent();
}
}
Then call it from the second form:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Form1.CustomMethod();
}
}

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

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.

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