I'm having difficulties populating the right combobox when I
generate several combobox' at runtime. I've got a windows forms
application, where the form consists of a button and a FlowLayoutPanel.
By clicking the button, a control combination consisting of a label and
two combobox' are added to the Form (to the FlowLayoutPanel). The code
so far is given below:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespaceWindowsFormsApplication1
{
publicpartialclassForm1 : Form
{
publicint counter;
public ComboBox combB;
publicstring[] combValuesShip;
publicstring[] combValuesCar;
publicstring[] combValuesAirPlane;
publicForm1()
{
InitializeComponent();
}
privatevoidbutton1_Click(object sender, EventArgs e)
{
AddNewComboboxLine();
}
privatevoidAddNewComboboxLine()
{
counter++;
Label label = new Label();
label.Text = "Label " + counter;
ComboBox combA = new ComboBox();
string[] combAValues = newstring[] { "Ship", "Car", "AirPlane" };
combA.Items.AddRange(combAValues);
combB = new ComboBox();
combA.SelectedIndexChanged += new EventHandler(this.combAChange);
label.Name = "label" + counter.ToString();
combA.Name = "combA" + counter.ToString();
combB.Name = "combB" + counter.ToString();
flowLayoutPanel1.Controls.Add(label);
flowLayoutPanel1.Controls.Add(combA);
flowLayoutPanel1.Controls.Add(combB);
combValuesShip = newstring[] { "Ferry", "OilTanker", "ContainerVessel" };
combValuesCar = newstring[] { "Toyota", "Lada", "Ferrari" };
combValuesAirPlane = newstring[] { "Boeing", "Airbus", "Sailplane" };
}
privatevoidcombAChange(object sender, EventArgs e)
{
MessageBox.Show(((ComboBox)sender).Name);
}
privatevoidForm1_Load(object sender, EventArgs e)
{
}
}
}
the
first combobox in each control combination (combA) is populated with
"Ship", "Car", "AirPlane". So far so good. The problem is getting
combobox combB to be populated with respective values. I can always add
something like this under the eventhandler for combobox combA:
This
works fine if i add one control combination at a time and select the
values in the combobox' before adding new. However, editing a previous
added combobox is now not possible. I.e. if i have added three lines
(each with a label and two combobox'), but now want to change values in
the first line, this is not possible.
Any suggestinons would be highly appreciated
-Amund
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Zoran HorvatRank #792 · 2.9K reputationPosted Nov 22, 2010, 12:14 AM EST
0
This code of yours is quite a mess. You should work a lot on the way you're thinking about the problem to solve, before coding. Do not use control name to make decisions.
Interconnections between controls should be held in separate structures, e.g. in objects that connect combo box A with combo box B instance.
Anyway, what exactly is the problem you're solving?
Amund8 reputationPosted Nov 22, 2010, 12:42 AM EST
0
OK. I'm quite new to programming (as you probably can see)
Here is my problem:
Lets say i want to sell Ships, Cars and AirPlanes. To do that I want to have a Form with a button. By clicking the button I want to add a sale. I.e. two combo boxes, where one specifies if I have sold a ship, car or airplane, and a combo box stating what kind of ship, car or airplane (if ship: ferry, oiltanker, containervessel. if car: toyota, lada, ferrari. if airplane: boeing, airbus, sailplane). I dont know how many sales i will be doing, so i want to add the combo boxes in runtime. And I also want to be able to edtih previous sold items. Take an instance where i have sold ten items. Suddenly customer #2 comes back and states that he dont want to buy and Car - Toyota, but a Ship - OilTanker. Then it should be easy to make that change.
Did that make sense?
0
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 10:49 PM EST
0
What you are describing sounds like cascading comboboxes. You will probably find articles and samples if you search for that. Something else that will likely help is master/detail applications, where there is a single row of a database table with multiple related rows in another table.
I certainly agree with Zoran that it helps to design the application. Make some sample forms for things; for this application, the forms will help you design the application.
Also, what you are describing is a typical Order Entry application. Look for descriptions and tutorials and articles and samples of OE applications; there are even useful books on the subject. In a typical OE application, there are customers, inventory items, vendors, orders and order items. Not all products are the same so of course there are differences among applications and/or configurations of OE applications. Examples of exceptions are custom-built items and items with serial numbers. Whatever your products are, it is nearly certain that there are already many OE applications that would work. I am not suggesting that you use existing software, except you can probably use existing designs to help you with your design. Microsoft might even have a useful sample application, such as a sample bookstore application. An order is a typical master/detail application in the sense of the order is a row in an orders table and each order has multiple order items in an order items table.
It is not clear to me whether you are filling the comboboxes from database tables, but that would be a useful thing to do. Consider usng DataGridView controls instead of ComboBox controls so you can show multiple columns for each level.
Zoran HorvatRank #792 · 2.9K reputationPosted Nov 22, 2010, 12:14 AM EST
Interconnections between controls should be held in separate structures, e.g. in objects that connect combo box A with combo box B instance.
Anyway, what exactly is the problem you're solving?
Amund8 reputationPosted Nov 22, 2010, 12:42 AM EST
Here is my problem:
Lets say i want to sell Ships, Cars and AirPlanes. To do that I want to have a Form with a button. By clicking the button I want to add a sale. I.e. two combo boxes, where one specifies if I have sold a ship, car or airplane, and a combo box stating what kind of ship, car or airplane (if ship: ferry, oiltanker, containervessel. if car: toyota, lada, ferrari. if airplane: boeing, airbus, sailplane). I dont know how many sales i will be doing, so i want to add the combo boxes in runtime. And I also want to be able to edtih previous sold items. Take an instance where i have sold ten items. Suddenly customer #2 comes back and states that he dont want to buy and Car - Toyota, but a Ship - OilTanker. Then it should be easy to make that change.
Did that make sense?
Sam HobbsRank #107 · 20.5K reputationPosted Nov 22, 2010, 10:49 PM EST
I certainly agree with Zoran that it helps to design the application. Make some sample forms for things; for this application, the forms will help you design the application.
Also, what you are describing is a typical Order Entry application. Look for descriptions and tutorials and articles and samples of OE applications; there are even useful books on the subject. In a typical OE application, there are customers, inventory items, vendors, orders and order items. Not all products are the same so of course there are differences among applications and/or configurations of OE applications. Examples of exceptions are custom-built items and items with serial numbers. Whatever your products are, it is nearly certain that there are already many OE applications that would work. I am not suggesting that you use existing software, except you can probably use existing designs to help you with your design. Microsoft might even have a useful sample application, such as a sample bookstore application. An order is a typical master/detail application in the sense of the order is a row in an orders table and each order has multiple order items in an order items table.
It is not clear to me whether you are filling the comboboxes from database tables, but that would be a useful thing to do. Consider usng DataGridView controls instead of ComboBox controls so you can show multiple columns for each level.