In need of some help T_T

Calling different forms from a combox

Page 1 of 1

3 Replies - 627 Views - Last Post: 30 September 2008 - 05:54 PM Rate Topic: -----

#1 Chris Ross  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 26-September 08

In need of some help T_T

Post icon  Posted 30 September 2008 - 05:22 PM

Hello eveorne, Well I am in need of some help, I am workin on a project in c#, and I was wondering how do you get a form to call a certain form when you select a item from a combox. So as in a example.

The form loads and there is the combo box called Character class, the user hits the down arrow and there are 5 different things they can chosse from fighter mage priest thief archer. Now the user select the Fighter choice, now in c# how would I call a window to show the fighter form. I have tired it and this is what my code looks likes.

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 Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            fighter FighterStats = new fighter();
            FighterStats.Show();

            Mage MageStats = new Mage();
            MageStats.Show();
        }
    }
}


*edit: Please use code tags in the future, thanks! :code:



Now when I click one of the items in the combox it loads both the form Fighter Stats and Mage Stats, how would I get it so that if I click fighter it would only load the fighter stats form. And call other forms from selecting the other choice so says I choose mage how would I call mage with out it calling the fighter from along with the mage form.

Thank you for spending time and reading this.

This post has been edited by Chris Ross: 30 September 2008 - 05:38 PM


Is This A Good Question/Topic? 0
  • +

Replies To: In need of some help T_T

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: In need of some help T_T

Posted 30 September 2008 - 05:39 PM

Well it is just a matter of recording which choice they made, evaluating it, and then showing the appropriate form. Here is an example...

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            // Collect which option was selected in the combo box
            int choice = comboBox1.SelectedIndex;

            // Evaluate the choice using a switch
            switch (choice)
            {
                // They chose a fighter, so show a fighter form
                case 0:
                    fighter fighterstats = new fighter();
                    fighterstats.Show();
                    break;
                // They chose a mage, show the mage form
                case 1:
                    Mage magestats = new Mage();
                    magestats.Show();
                    break;
                // They chose something else, so tell them via messagebox
                default:
                    MessageBox.Show("Other character selected");
                    break;
            }
        }



Here this assumes that the fighter choice is the first in the list (index 0) and that the mage is second (index 1) and we setup the forms accordingly using a switch statement. Here we create the proper form and then show it.

I assume this is what you are looking to do and hope it works out for you.

"At DIC we be fighter and mage showing code ninjas... what, no option for code ninja? Damn youuuu!" :snap:
Was This Post Helpful? 0
  • +
  • -

#3 Chris Ross  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 12
  • Joined: 26-September 08

Re: In need of some help T_T

Posted 30 September 2008 - 05:47 PM

Wow that is what I was looking for, thank you so much. ^_^
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4333
  • View blog
  • Posts: 12,128
  • Joined: 18-April 07

Re: In need of some help T_T

Posted 30 September 2008 - 05:54 PM

Glad I could help. Also a more robust solution can be grabbing the text of the item itself and check its name in the switch statement. This would make it independent of position in the combobox list. Just use the lines...

String choiceName = comboBox1.SelectedItem.ToString();



So if you select "mage" from the combo, choiceName will be set to "Mage".

Either way works depending on your design. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1