473,804 Members | 3,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 cmdGenerateInpu tBoxes_Click(ob ject sender, EventArgs e)
{
ReadXmlAndFillC ombo( (string)cboNumb erOfTeams.Selec tedItem
);
}

//This is where I want to add x number of comboboxes:
public void ReadXmlAndFillC ombo(string number)
{
int numberOfTeams = int.Parse(numbe r);
//Read xml and fill the combobox(es)
fs = new FileStream(path , FileMode.Open, FileAccess.Read ,
FileShare.ReadW rite);
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.A dd(cboTeam);
this.tabNewCup. Controls.Add(cb oTeam);
cboTeam.Formatt ingEnabled = true;
cboTeam.Locatio n = new System.Drawing. Point(220, 424);
cboTeam.Name = "cboTeam";
cboTeam.Size = new System.Drawing. Size(121, 21);
cboTeam.TabInde x = 0;
cboTeam.Visible = true;
}

xmlnode = xmldoc.SelectNo des("(//spain/name)");

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

Hope someone can help me :)

Oct 27 '06 #1
5 6888
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.F ind("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.A dd(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.Cont rols.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(cb oTeam);
cboTeam.Formatt ingEnabled = true;
cboTeam.Locatio n = new System.Drawing. Point(220, 424);
cboTeam.Name = "cboTeam" + i.ToString();
cboTeam.Size = new System.Drawing. Size(121, 21);
cboTeam.TabInde x = 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 InitializeCompo nent
tabControl = new TabControl();
tabOne = new TabPage("Tab 1");
tabTwo = new TabPage("Tab 2");
Button btnCreate = new Button();
Button btnLookup = new Button();
tabControl.TabP ages.Add(tabOne );
tabControl.TabP ages.Add(tabTwo );
tabControl.Dock = DockStyle.Fill;
btnCreate.Top = btnCreate.Margi n.Top;
btnCreate.Text = "Create";
btnCreate.Click += new EventHandler(bt nCreate_Click);
btnLookup.Top = btnCreate.Botto m + btnCreate.Margi n.Bottom +
btnLookup.Margi n.Top;
btnLookup.Text = "Lookup";
btnLookup.Click += new EventHandler(bt nLookup_Click);
this.Controls.A dd(tabControl);
tabOne.Controls .Add(btnCreate) ;
tabOne.Controls .Add(btnLookup) ;
}

void btnLookup_Click (object sender, EventArgs e)
{
Control[] found = this.Controls.F ind("Combo5", true);
if (found.Length == 1)
{
this.Text = "Combo5.Tex t = " + 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(oldCont rols, 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.AddRa nge(options);
cbo.Name = "Combo" + i.ToString();
top += cbo.Margin.Top;
cbo.Top = top;
top += cbo.Height + cbo.Margin.Bott om;
tabTwo.Controls .Add(cbo);
cbo.Text = "I'm " + cbo.Name;
cbo.SelectedInd exChanged += new
EventHandler(So methingChanged) ;
cbo.TextChanged += new EventHandler(So methingChanged) ;
}

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

void SomethingChange d(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
if (cbo == null) return; // didn't expect that!
object selected = cbo.SelectedIte m;
if (selected == null)
{
this.Text = cbo.Name + " is now " + cbo.Text;
}
else
{
this.Text = cbo.Name + " is now " + selected.ToStri ng();
}
}
}
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
4208
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 won't be broken when my app is opened with those versions. Also is the Scripting Runtime included as part of the A2000 Runtime Engine which some of my customers use.
9
1965
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 order to operate the program. -key function is to help automate some time consuming tasks that occupy a business owner the most and join several other programs functions into one precise and easy to use system that will eliminate other bulky...
5
1358
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 the controls with the keys that represent the item values. For example I want to create a dropdown box in which I display some data from my datasource that represents a period. The visible text could be anything, but each record is uniquely...
3
1366
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 to type 'set user.name=adam' and the code checks the user class instance for the property name provided (Name) and assigns that value? I cannot figure out how to do this without hardcoding the "set" code to know the Name property in advance. As...
7
1886
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 understand how to fix the problem. 'declaring variables Dim totalLiters As Double = Convert.ToDouble(txtLiters.Text) Dim totalPints As Double = Convert.ToDouble(txtPints.Text)
2
3287
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 width. Can this be done? I have set the multiline and wordwrap properties to be true.
5
4060
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 each record. I don't yet see the way to update the list of values in the second combobox. Thanks for any input, Tony
2
1997
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 external tables: One for Countries that store each Country as a ID (char(2)), and Name (varchar(50)) pair; One for Medical Conditions, using ID (int) and Name (varchar(50)). Also, the BillTo field refers back to the same table that is being...
0
1561
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 databases in that server. Then after the user selects a database, I have another button that he/she click and I want to retrieve file groups from a specific table. At this point when he/she clicks on that button I get an error:
0
9575
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10564
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10308
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7609
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6846
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5645
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3806
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2981
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.