Cant access Checkbox from another form - c#

Any help is appreciated
Basically I am having trouble accessing a checkedlistbox on a tab.
I have a checkedlistbox on tab 1 of form 1. I want to pass this checkbox to form 2 and place the results in a listbox on form 2
Form1
public static void ShowResults(string strRoutine, string strCaption)
{
ResultsForm.Routine = strRoutine;
ResultsForm.Title = strCaption;
strXMLFileName = xmlDocConfig.SelectSingleNode("config/routine[#key='" + strRoutine + "']/outputfname").Attributes.GetNamedItem("value").Value;
strXMLFileName = clsUtilities.ReplacePathWildcards(strXMLFileName);
strXMLFileName = clsUtilities.ReplacePathWildcards(frmNSTDBQC.xmlDocConfig.SelectSingleNode("config/routine[#key='G']/outputfname").Attributes.GetNamedItem("value").Value) + "\\" + strXMLFileName;
ResultsForm.DisplayFile = strXMLFileName;
ResultsForm.ShowDialog();
}
In form 2 I can access the tab control, I can access QCForms.tcTabs.SelectedTab.Text with the correct result but QCForm.chkLstLines.Items.Count says 0 event though I have 10 Items checked
Form 2
public void frmResults_Load(object sender, EventArgs e)
{
int i = 0;
this.Text = "Results - " + this.Title;
switch (QCForm.tcTabs.SelectedTab.Text)
{
case "Line Checks":
i = 0;
while (i < QCForm.chkLstLines.Items.Count)
{
if ( QCForm.chkLstLines.GetItemChecked(i))
{
lstFeatures.Items.Add(QCForm.chkLstLines.Items[i].ToString()); // VB6.GetItemString(QCForm.chkLstLines, i));
}
i++;
}
}
}
Edit
Form 1
public static void ShowResults(string strRoutine, string strCaption)
{
var ResultsForm = new Form(this);
//ResultsForm.Routine = strRoutine;
//ResultsForm.Title = strCaption;
strXMLFileName = xmlDocConfig.SelectSingleNode("config/routine[#key='" + strRoutine + "']/outputfname").Attributes.GetNamedItem("value").Value;
strXMLFileName = clsUtilities.ReplacePathWildcards(strXMLFileName);
strXMLFileName = clsUtilities.ReplacePathWildcards(frmNSTDBQC.xmlDocConfig.SelectSingleNode("config/routine[#key='G']/outputfname").Attributes.GetNamedItem("value").Value) + "\\" + strXMLFileName;
//ResultsForm.DisplayFile = strXMLFileName;
ResultsForm.ShowDialog();
}
Form 2
private frmNSTDBQC QCForm;
public frmResults(frmNSTDBQC qcForm)
{
InitializeComponent();
QCForm = qcForm;
}

Get same instance of Form1 where you add checkboxes in this way
Form2 is:
private Form1 form1;
public Form2(Form1 form)
{
form1 = form;
}
// now you can use form1 as object
and now show Form2 from Form1
var form2 = new Form(this); //pass instance
form2.ShowDialog();

If you have Both Forms open and you want them to interact, If you create first Form1 and then Form2, you can make Form2 able to see the changes in Form1 creating a CheckedListBox property inside Form2 and initializing it to the CheckedListBox object that stands inside Form1.
Pay attention that the reference is set after the CheckedListBox is created and its content initialized.
This however is a little bit a "raw" way to proceed. A better way to proceed would be to have a List of objects representing the data in the Form1
(E.g. List<Yourobject>)
as datasource of the CheckedListBox, bound to the properties in the listbox so that a Checked property in the data is set/reset by your listbox, and then share just this list of data between the two forms using a
List<YourObject>
collection that you set in a property of Form2 When opening it after you initialize it in the Form1.
Remember that the class YourObject must implement a PropertyChanged on the Checked property to inform of changes and also that if that is the case, it is better to use a BindingList not just a List collection object because the BindingList implements the events to inform the UI that its content has changed such as the ObservableCollection does for WPF.
HTH

Related

Access parameters from an object listed in a listbox c#

I have several classes in my program and I'm using Windows Form to create the objects from the different classes and list them in different listboxes.
Right now I have a form with all the listboxes (Form 1) and another form (CreerVoiture) where i put all the info to create my selected object.
An example is when i click on the button "Voiture!" my second form opens, when I add all the info and push on "Ajouter" it adds the object to the listbox selected.
Code from Form1:
private void button1_Click(object sender, EventArgs e)
{
CreerVoiture creervoiture = new CreerVoiture();
if (creervoiture.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of form2's TextBox.
Voiture voiture = new Voiture(creervoiture.GetMarque(), creervoiture.GetPrix(), creervoiture.GetConsommation(), creervoiture.GetReservoir());
this.list_voiture.Items.Add(voiture);
this.list_voiture.DataSource = null;
}
creervoiture.Dispose();
}
What I would like to know is how I can access a parameter I added in my second form once I added it to the listbox.
I was thinking to use something like:
list_voiture.SelectedItem.Prix
Prix is a getter from my class Voiture
public double Prix
{
get { return this.prix; }
set { this.prix = value; }
}
But that doesn't seem to be possible. Is this possible and if so, how?
Thanks in advance,
Jeremy
If the type of object is Voiture then you can cast the object to that type:
Voiture voiture = (Voiture)list_voiture.SelectedItem;
double prix = voiture.Prix;
Or in one line:
double prix = ((Voiture)list_voiture.SelectedItem).Prix;

Keep data on multiple form WindowsForms c#

I 'am making a WindowsForms application. I have 2 Forms :
On the first form (Form1), There are many fields (Textboxs) that should be filled by the user, then click to a button (Transfer).
This button should show all the input datas of Form1 in a new Form (Form2).
I'm not sure how to begin to move data from one form to another. Can somebody guide me how to do it?
If Form1 creates Form2 specifically to show the data, then you can use a non-default constructor to pass the information as you create the form.
First, let's consider an example of the information you want to transfer, and name it Form2Info:
// This class is an example of the information you want to transfer
public class Form2Info
{
public string text1;
public int number1;
}
Then, you modify Form2's constructor to take the information:
public partial class Form2 : Form
{
private Form2Info info;
public Form2(Form2Info information)
{
InitializeComponent();
info = information;
// Do something with this information, such as populate a TextBox or Label on the form.
}
}
Finally, you want to create a Form2 instance from your Form1:
// Create the information you want to pass; we fill it with some placeholder data here.
Form2Info info = new Form2Info();
info.text1 = "Hello"
info.number1 = 5;
// Now create the form and pass the data
Form2 form2 = new Form2(info);
form2.ShowDialog(); // Show modal dialog.
Each Textbox has a value (Textbox.Text property). You will have to transfer the content of these different properties to the new controls in your second form.
The easiest way will be through a custom Form constructor for the second form.
public Form2(string textBox1Value, string textBox2Value)
// etc... add as many as you like or use an object that holds all values in properties
{
InitializeComponent(); // Required by WinForms
this.TextBox1.Text = textBox1Value;
this.TextBox2.Text = textBox2Value;
}
Make sure you match the text boxes you want using the names. Finally when creating the form on the Transfer button code, call this constructor instead.

How to put Combo Boxes generated by the Windows Form Designer in an array

I have a bunch of different ComboBoxes that were made with the windows form Designer I would like to be able to access them by index, so that I could do something like this:
for (int i = 0; i < numOfBoxes; i++)
{
ComboBoxes[i].visible = false;
}
I tried putting them in an array of ComboBoxes, but this creates an array of nulls.
private ComboBox[] ComboBoxes;
public MainForm()
{
ComboBoxes = new ComboBox[] {ComboBox1, ComboBox2, ComboBox3};
}
What's the right way to do this?
You need to make sure you make your array of ComboBox after InitializeComponent is called.
private ComboBox[] ComboBoxes;
public MainForm()
{
InitializeComponent();
ComboBoxes = new ComboBox[] {ComboBox1, ComboBox2, ComboBox3};
}
Prior to this all of the windows form designer objects will be null because they are only first instantiated in InitializeComponent.

Can´t add item to ListBox in different form

I´m trying to build an application that can store different persons in a ListBox-control.
My ListBox-control is located in Form1 and so is this method that´s giving me some problem:
public void addPersonToList(Person person) {
string newPerson = person.firstName + " " + person.lastName + " " + person.age;
personList.Items.Add(newPerson);
}
In another Form, I call the addPersonsToList-method like this:
Form1 form1 = new Form1();
form1.addPersonToList(person);
Now, I´ve checked (while debugging) that the string newPerson in addPersonToList actually stores the correct string. The problem is that the string won´t show up in my ListBox(named personList).
Any suggestions?
Using new, you are creating a brand new instance of an item.
Form1 form1 = new Form1();
form1.addPersonToList(person);
So this code creates a new instance of Form1 and adds an item to that instance, which is probably not the one you are viewing. You somehow need a reference to the instance being displayed so you can reference that.
To reference an existing open form, do this:
foreach (Form frm in Application.OpenForms)
{
if (frm.GetType() == typeof(Form1))
{
Form1 frmTemp = (Form1)frm;
frmTemp.addPersonToList(person);
fromTemp.Dispose();
}
}
Similarly for MDI forms :
foreach (Form frm in MdiParent.MdiChildren)
{
}

I need to know how to take the selected item of a comboBox and make it appear on a windows form application?

I have a windows form application with a ComboBox on it and I have some strings in the box. I need to know how when I select one of the strings and press my create button, how can i make that name show up on another windows form application in the panel I created.
Here is the code for adding a customer
public partial class AddOrderForm : Form
{
private SalesForm parent;
public AddOrderForm(SalesForm s)
{
InitializeComponent();
parent = s;
Customer[] allCusts = parent.data.getAllCustomers();
for (int i = 0; i < allCusts.Length; i++)
{
Text = allCusts[i].getName();
newCustomerDropDown.Items.Add(Text);
newCustomerDropDown.Text = Text;
newCustomerDropDown.SelectedIndex = 0;
}
now when i click the create order button I want the information above to be labeled on my other windows form application.
private void newOrderButton_Click(object sender, EventArgs e)
{
//get the info from the text boxes
int Index = newCustomerDropDown.SelectedIndex;
Customer newCustomer = parent.data.getCustomerAtIndex(Index);
//make a new order that holds that info
Order brandSpankingNewOrder = new Order(newCustomer);
//add the order to the data manager
parent.data.addOrder(brandSpankingNewOrder);
//tell daddy to reload his orders
parent.loadOrders();
//close myself
this.Dispose();
}
The context is not very clear to me, but if I got it right, you open an instance of AddOrderForm from an instance of SalesForm, and when you click newOrderButton you want to update something on SalesForm with data from AddOrderForm.
If this is the case, there are many ways to obtain it, but maybe the one that requires the fewer changes to your code is this one (even if I don't like it too much).
Make the controls you need to modify in SalesForm public or at least internal (look at the Modifiers property in the Design section of the properties for the controls). This will allow you to write something like this (supposing customerTxt is a TextBox in SalesForm):
parent.customerTxt.Text = newCustomerDropDown.SelectedItem.Text;

Categories