Hi,

I'm pretty new to C# but I'm very familiar with C++ and first off I wanted to know if anyone has a good website with some tutorials for C#.

Other than that, I have a few basic questions regarding GUI programming.

I suppose one question at a time is good enough so I want to have 5 drop down boxes (I'm assuming that a combo box is what I should use) and each of these boxes will pull from the same list (and array of strings I'm also assuming). When one item from the list is selected in one box, it will not be available for selection in another, and you cannot input text into the field.

Visual Studio 2010 has a good WYSIWYG designer for this but it still isn't perfectly clear on how to tie all of the boxes to the same list and put conditions on the selections.

Any advice would be great.
Thanks.

Edited 5 Years Ago by tjm6f4: n/a

Hmmm, first of all msdn.com (or what ever the ending it), they moreexplain methods, and calls and such more indepth, giving you examples, and some pages list all the members of a call (or such). I just learned C# last year, and use VS2010 for my GUIs and have become extremely good.

As for you second question you mind explain it more in detail for me? or even better pictures, like screenshots and such, I can do well when I see images

Thanks for responses.

Okay, I'll just explain the following picture. It is a rough outline of where I want to go but it should get my point across rather simply.

The 5 larger selection boxes will be drop downs and all 5 will feed off the same list. Furthermore, if "Cheeseburger" is selected in the first box, then it cannot be selected in any of the other four boxes. Then, from the smaller boxes on the right I should be able to select a specific quantity of whatever was selected on the left.

Finally, hit calculate and it takes all of the box selections and calculates some fantastic number and produces it in the box at the bottom left.

Ultimately, it is way more complicated than this BUT knowing how to do these basic operations will be a huge step in the right direction.

Thanks.

Attachments Alpha.png 20.59 KB

Put your comboboxes into a groupbox. Give each of your comboboxes a unique label in the Tag property ("CB1", "CB2" etc is what I used).

Make a single SelectedIndexChanged method and hook it up to all of your comboboxes (in the Form1.Designer.Cs file), so treat it as if you had separate comboBox_SelectedIndexChanged methods, but make them look like: this.comboBox2.SelectedIndexChanged += new System.EventHandler(this.comboBox_SelectedIndexChanged); instead of having this.comboBox2_SelectedIndexChanged.

private void comboBox_SelectedIndexChanged(object sender, EventArgs e)
{
      string selectedtag = (string)(sender as ComboBox).Tag;

      foreach (ComboBox cb in groupBox1.Controls)
      {
            if ((string)cb.Tag != selectedtag)
            {
                    cb.Items.Remove((sender as ComboBox).SelectedItem.ToString());
            }
        }
  }

The only drawback is once the entries are eliminated, they won't come back if deselected. You could have a reset button on there to reload all of the boxes at once.

I won't claim this is the most effective method, so I'm sure someone can improve upon it.

Edited 5 Years Ago by jonsca: n/a

I think you should consider a ListBox. Select from it, make it impossible to select the item again and put the selection in a second ListBox.
It seems more "natural" to me than your approach with the ComboBoxes.
Think of making a grocery list: you have a great big list of all possible items in your head, you select from it and you write down a new list of what you need on a piece of paper.
Look here for some explanation and an example.
http://msdn.microsoft.com/en-us/library/system.windows.forms.listbox.aspx
I made a new WidowsFromsApplication dropped in a button and filled its Click event with the example code. I also added 2 labels to my form label1 and label2 and changed the last two Debug lines into

label1.Text = listBox1.SelectedItems[1].ToString();
            label2.Text = listBox1.SelectedIndices[0].ToString();

There must be something wrong with my debug settings.
Only minus is that ListBox is HUGE and has many options.
Success!

Edited 5 Years Ago by ddanbe: n/a

Jonsca pretty much stated the idea I had, which is if they select that item in one comboBox, then have a loop that will go threw the other 4 comboBoxes searching for that same string and removing it. The only probelm I see with this is you can't do this right after they select the option, because they might have accidently chosen the wrong thing. This could also become a problem if they decide to go back threw and change there items. In that case I would have a backup for data storage (Array, Stack, ext) that can be refered to to refill the removed data. You could just also make a if statement when they hit calculate that checks to make sure the same string wasn't selected in multiple comboBoxes. (sorry this can be how I program, I think of the possiblilites and the outcomes) (I had another idea to but it slipped my mind at the moment)

@ddanbe, I think he might be avoiding listBoxes for the size and look of them

Oh and as for the smaller comboBoxes there is a command such as AddRange that will allow you to add and array of elements to a comboBox in one line of code

http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.objectcollection.addrange.aspx

That link should help with that. You could also just give them a textLine here, and use something like this piece of code to make sure they entered a number

int num = 0;
      
bool valid = Int32.TryParse(((textBox1.Text).Trim()), out num);
//all this does is converts a string and drops the number out to an int but also
//returns a boolean. If the boolean is true that means that the string 
//successfully converted to an integer, and put the number (in my case)
//in num. (Oh I forgot to mention the Trim removes excess white space)


if (valid == true)
 {
  //...

Hope I gave you something to work with and didn't just waste your time. I tend to get very excite about programming especially in things I know well about. Need anymore help or wasn't exactly what your looking for feel free to ask.

Edited 5 Years Ago by JOSheaIV: n/a

If you take Jonsca's idea and sodomise it slightly...

On your form, keep a list of the selected items. (As in List<String> MySelectedItemNames; )
Every time you make a selection, clear this list and re-add all selected items again.
Every time you make a selection, clear each box apart from the selected item.
Every time you make a selection, re-add all the items that appear in your original list and NOT in your new list to each box.

This is probably a bad way to do it, but is the only way I can think of, off the top of my head.

Semi-Psuedo-Code (ie. I have no idea if this works. I wrote it without the IDE)

List<String> MySelectedItems = new List<String>();
// Assign Selected index changed to this method
private void ItemSelectedIndexChanged(object sender, EventArgs e)
{
    /* In my Form Designer, each Combo box is assigned the tag "ItemSelection" */
    MySelectedItems.Clear();
    foreach(ComboBox comboBox in this.Controls)
    {
        if(!comboBox.Tag.ToString().Equals("ItemSelection"))
            continue;

        String temporaryItem = comboBox.SelectedItem.ToString();
        comboBox.Items.Clear();
        MySelectedItems.Add(temporaryItem);
        comboBox.Items.Add(temporaryItem);
        comboBox.SelectedIndex = 0;
    }

    // Unfortunately we have to do this loop again... =(
    foreach(ComboBox comboBox in this.Controls)
    {
        if(!comboBox.Tag.ToString().Equals("ItemSelection"))
            continue;

        // Ugh embedded loops >.<. The original list is whereever you keep your original item list or whatever
        foreach(String itemString in originalList)
        {
            if(MySelectedItems.Contains(itemString))
                continue;
            comboBox.Items.Add(itemString);
        }
    }
}

EDIT: I also know that this CAN be improved upon with LINQ. But without the editor I wouldn't like to start guessing at the query structure needed ^^

Edited 5 Years Ago by Ketsuekiame: n/a

Comments
Interesting metaphor. Stay away from my children, just kidding.
This article has been dead for over six months. Start a new discussion instead.