Jump to content
  • Advertisement
Sign in to follow this  
namingway

C# assistance

This topic is 3842 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In my current project I have combo boxes and some of there collections I want to be generated from list boxes. e.g. a combo box to select different users and the list box is where you would add different users, so if your listbox had- Johnny Tim Ralph then the combo box would show those as its items. How can I do this?

Share this post


Link to post
Share on other sites
Advertisement
Hi,

OK, so if I understand you correctly you have a Form with a number of Combo Boxes' and a List Box and you want to populate the List Box with values from the Combo Boxes'.

If you look at the events on a Combo Box you will find a SelectedIndexChanged event - you should subscribe to this event for each Combo Box. Now, you could have one event handler for all of your Combo Boxes' or you could have a separate one for each Combo Box - I'd personally go for one event handler for each Combo Box. Inside each event handler you need to get the currently selected item in the Combo Box by casting the sender like this:

((ComboBox)sender).SelectedItem



or by getting the SelectedItem from the specific Combo Box this event handler relates to:

comboBox1.SelectedItem



and then add this item to your List Box's items collection. You may also want to do some checks before you stick the selected item in the List Box's collection - maybe to check for duplicates, or something else.

The code below shows how to do all this with one Combo Box and a List Box:


public Form1()
{
InitializeComponent();

// Subscribe to the selected index changed event for the combo box
this.comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
}

/// <summary>
/// Event handler for the combo box. Adds the new selected item to the list box.
/// </summary>
/// <param name="sender">Combo box that raised the event.</param>
/// <param name="e">Default event arguments.</param>
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Do some check on the item to add (if required).

// Add the selected item from the combo box
this.listBox1.Items.Add(((ComboBox)sender).SelectedItem);

// You can also add the item like this if you don't want to cast the sender
//this.listBox1.Items.Add(this.comboBox1.SelectedItem);
}




Hope this helps.

James

EDIT: Spellings and source Tags.

Share this post


Link to post
Share on other sites
Okay I meant the other way round but based on what youve given me and the two components similarities it shouldnt be to hard to apply it in reverse.

Thanks :D -Namingway

Share this post


Link to post
Share on other sites
I am having trouble getting this to work, the list box holds objects in which you can alter its attributes, in particular the name, the problem I'm having is that the name of the combobox item doesnt update as the listbox items do.

Share this post


Link to post
Share on other sites
Sorry, missread your original post. What problem are you currently having? Do you have any code?

I'm not sure if I understand the problem fully but the following code should do something similar to what you're asking:


// User class
public class user
{
// Private fields
string name = string.Empty;
string iD = string.Empty;

// Public properties
public string ID
{
get { return iD; }
set { iD = value; }
}
public string Name
{
get { return name; }
set { name = value; }
}

// Constructor
public user(string name, string iD)
{
Name = name;
ID = iD;
}

// To string method to display object correctly
public override string ToString()
{
return string.Format("{0}, {1}", Name, ID);
}
}

// Form1 class
public partial class Form1 : Form
{
// Constructor
public Form1()
{
// Initialise component
InitializeComponent();

// Add some items
listBox1.Items.Add(new user("James", "one"));
listBox1.Items.Add(new user("Tom", "two"));
listBox1.Items.Add(new user("Sam", "three"));

// Subscribe to selected index changed
listBox1.SelectedIndexChanged += new EventHandler(listBox1_SelectedIndexChanged);

// Set the selected index of the listbox to 0 - display first item
listBox1.SelectedIndex = 0;
}

// Listbox selectedindexchanged event
void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
// Clear contents of the combo box
comboBox1.Items.Clear();

// Add properties of current item to the combo box
comboBox1.Items.Add(((user)listBox1.SelectedItem).Name);
comboBox1.Items.Add(((user)listBox1.SelectedItem).ID);

// Set the selected index of the combo box to be first in list (this will display the name)
comboBox1.SelectedIndex = 0;
}



I would strongly advise you to start using the MSDN library (F1 in Visual Studio - loads the internet version if you don't have the full version installed). From here you can look through all the methods / properties of different components and get an idea of what they do and how to use them.

Good luck,

James

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!