473,399 Members | 2,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

Add ComboBoxes in runtime based on user input, how?

I'm creating an application where the user first selects a number from
a combobox. Based on his choice of number, ComboBoxes and buttons are
to be created (at runtime of course).

Let's say that the ComboBox is to be named cboTeam and the index number
(cboTeam1, cboTeam2 ...) It is also to be attached inside a specific
tabpage. How can I do this?

Some code:

//The button that gets the number the user choose:
private void cmdGenerateInputBoxes_Click(object sender, EventArgs e)
{
ReadXmlAndFillCombo( (string)cboNumberOfTeams.SelectedItem
);
}

//This is where I want to add x number of comboboxes:
public void ReadXmlAndFillCombo(string number)
{
int numberOfTeams = int.Parse(number);
//Read xml and fill the combobox(es)
fs = new FileStream(path, FileMode.Open, FileAccess.Read,
FileShare.ReadWrite);
xmldoc = new XmlDocument();
xmldoc.Load(fs);

for (int i = 0; i <= numberOfTeams; i++)
{ //I know this is wrong, but you get the idea.. :)
ComboBox cboTeam = new ComboBox();
this.Controls.Add(cboTeam);
this.tabNewCup.Controls.Add(cboTeam);
cboTeam.FormattingEnabled = true;
cboTeam.Location = new System.Drawing.Point(220, 424);
cboTeam.Name = "cboTeam";
cboTeam.Size = new System.Drawing.Size(121, 21);
cboTeam.TabIndex = 0;
cboTeam.Visible = true;
}

xmlnode = xmldoc.SelectNodes("(//spain/name)");

foreach (XmlNode node in xmlnode)
{
cboTeams.Items.Add(node.InnerText);
}
}

Hope someone can help me :)

Oct 27 '06 #1
5 6860
Was there a /specific/ problem point / question?

That's /about/ right... the first thing you need to remember is that
"cboTeam" (as a variable) is only defined inside the loop; you should set
cboTeam.Name = "cboTeam" + i.ToString() [although this will be 0-based, not
1-based], and then to find the correct one, either cast the sender of an
event, or use this.Controls.Find("cboTeam5", true) to find the control.

You need to decide where the control sits; is it under "this", or under
"this.tabNewCup"; I suspect the latter, so drop the
this.Controls.Add(cboTeam); it can have only 1 parent.

The only other thing is that you might want to remove any options from the
last selection; this.tabNewCup.Controls.Clear() [before the for] may help,
but *strictly* speaking it would be better to Dispose() them as well. This
can mean taking a copy (tabNewCup.Controls.CopyTo(...)), then clearing the
collection, then enumerating the copy calling Dispose().

Oh, and you might have an off-by-one issue; <, not <=

Marc
Oct 27 '06 #2
Thanks for the reply Marc!

But I'm a newbie in programming really, if you could write some sample
code, I'd be really happy :)

Oct 27 '06 #3
When I use this code, no comboboxes appear..I have now decleared the
cboTeam variable as a public variable

for (int i = 0; i < numberOfTeams; i++)
{
cboTeam = new ComboBox();

this.tabNewCup.Controls.Add(cboTeam);
cboTeam.FormattingEnabled = true;
cboTeam.Location = new System.Drawing.Point(220, 424);
cboTeam.Name = "cboTeam" + i.ToString();
cboTeam.Size = new System.Drawing.Size(121, 21);
cboTeam.TabIndex = 0;
cboTeam.Visible = true;
}

Oct 27 '06 #4
Fair enough; try the two buttons, and changing values, etc.

In particular, look at the event handlers, as this is where the fun is.

Marc

using System;
using System.Windows.Forms;
class MyForm : Form
{
TabPage tabOne, tabTwo;
TabControl tabControl;
public MyForm() {
// setup the form; similar to InitializeComponent
tabControl = new TabControl();
tabOne = new TabPage("Tab 1");
tabTwo = new TabPage("Tab 2");
Button btnCreate = new Button();
Button btnLookup = new Button();
tabControl.TabPages.Add(tabOne);
tabControl.TabPages.Add(tabTwo);
tabControl.Dock = DockStyle.Fill;
btnCreate.Top = btnCreate.Margin.Top;
btnCreate.Text = "Create";
btnCreate.Click += new EventHandler(btnCreate_Click);
btnLookup.Top = btnCreate.Bottom + btnCreate.Margin.Bottom +
btnLookup.Margin.Top;
btnLookup.Text = "Lookup";
btnLookup.Click += new EventHandler(btnLookup_Click);
this.Controls.Add(tabControl);
tabOne.Controls.Add(btnCreate);
tabOne.Controls.Add(btnLookup);
}

void btnLookup_Click(object sender, EventArgs e)
{
Control[] found = this.Controls.Find("Combo5", true);
if (found.Length == 1)
{
this.Text = "Combo5.Text = " + found[0].Text;
}
else
{
this.Text = "Couldn't find Combo5";
}
}

void btnCreate_Click(object sender, EventArgs e)
{
SuspendLayout();
try
{
// cleanly dispose of any controls left on tab 2
Control[] oldControls = new Control[tabTwo.Controls.Count];
tabTwo.Controls.CopyTo(oldControls, 0);
tabTwo.Controls.Clear();
foreach (Control control in oldControls)
{
control.Dispose();
}

// add the new controls
object[] options = { "abc", "def", "ghi", "jkl", "mno", "pqr",
"stu", "vwx", "yz" };
int top = 0;
for (int i = 0; i < 10; i++)
{
ComboBox cbo = new ComboBox();
cbo.Items.AddRange(options);
cbo.Name = "Combo" + i.ToString();
top += cbo.Margin.Top;
cbo.Top = top;
top += cbo.Height + cbo.Margin.Bottom;
tabTwo.Controls.Add(cbo);
cbo.Text = "I'm " + cbo.Name;
cbo.SelectedIndexChanged += new
EventHandler(SomethingChanged);
cbo.TextChanged += new EventHandler(SomethingChanged);
}

// switch tab
tabControl.SelectedTab = tabTwo;
}
finally
{
ResumeLayout();
}
}

void SomethingChanged(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
if (cbo == null) return; // didn't expect that!
object selected = cbo.SelectedItem;
if (selected == null)
{
this.Text = cbo.Name + " is now " + cbo.Text;
}
else
{
this.Text = cbo.Name + " is now " + selected.ToString();
}
}
}
class Program
{
static void Main()
{
using (MyForm form = new MyForm())
{
Application.Run(form);
}
}
}
Oct 27 '06 #5
Thank you very much, this worked fine :)

Oct 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Karl Irvin | last post by:
To use the Textstream object, I had to set a Reference to the Microsoft Scripting Runtime. This works good with A2000 Is the Scripting Runtime included with A2002 and A2003 so the Reference...
9
by: Charisma | last post by:
I have a client who is requesting the following: Description: -database should be a runtime version of MS Access so that it would not be necessary for the user to have any Microsoft products in...
5
by: Henry | last post by:
Please forgive me if I have missed an obvious answer to this question... I am looking at controls like comboBoxes, ListBoxes, and Trees and trying to understand how to associate what is viewed in...
3
by: AdamM | last post by:
At runtime, how do you take a user-supplied string, and then match that up with a class property name? For example, I have a User class with the property User.Name. At runtime, I want the user...
7
by: Charlie Brookhart | last post by:
I have a program (posted below) that is supposed to take liters, which is the user input, and convert it to pints and gallons. The pints and gallons are displayed in a read only textbox. I don't...
2
by: avanti | last post by:
Hi, I have a multiline textbox in my application. It starts out as a single line textbox. I want it to increase in height to accomodate another line at runtime if the user input exceeds it's...
5
by: Anthony Bollinger | last post by:
I have two tables in a master-detail relationship. When I make a selection in one combobox, how do I have it display the values from a second combobox? Each table has a key and a text value for...
2
by: Wingot | last post by:
Hey, I have a view to a database that I have created for Client Maintenance. It has a number of fields, but the important ones are Medical Condition, Bill To, and Country. I have a couple of...
0
by: TG | last post by:
Hi! Once again I have hit a brick wall here. I have a combobox in which the user types the server name and then clicks on button 'CONNECT' to populate the next combobox which contains all the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.