Hi,
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:
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;
namespace WindowsFormsApplication1 { public partial class Form1 : Form { public int counter; public ComboBox combB; public string[] combValuesShip; public string[] combValuesCar; public string[] combValuesAirPlane; public Form1() { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e) { AddNewComboboxLine(); }
private void AddNewComboboxLine() { counter++; Label label = new Label(); label.Text = "Label " + counter; ComboBox combA = new ComboBox(); string[] combAValues = new string[] { "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 = new string[] { "Ferry", "OilTanker", "ContainerVessel" }; combValuesCar = new string[] { "Toyota", "Lada", "Ferrari" }; combValuesAirPlane = new string[] { "Boeing", "Airbus", "Sailplane" };
}
private void combAChange(object sender, EventArgs e) { MessageBox.Show(((ComboBox)sender).Name); }
private void Form1_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:
private void combAChange(object sender, EventArgs e) { ComboBox currentCombobox = (ComboBox)sender; string lastChar = currentCombobox.Name.Substring(currentCombobox.Name.Length - 1, 1); if (lastChar == "1") { combB.Items.Clear(); combB.Items.AddRange(combValuesShip); }
if (lastChar == "2") { combB.Items.Clear(); combB.Items.AddRange(combValuesCar); }
if (lastChar == "3") { combB.Items.Clear(); combB.Items.AddRange(combValuesAirPlane); }
}
|
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
Sam HobbsPosted Nov 23, 2010, 3:49 AM
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.
AmundPosted Nov 22, 2010, 5:42 AM
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?
Zoran HorvatPosted Nov 22, 2010, 5:14 AM
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?