Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,825 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,912 people online right now. Registration is fast and FREE... Join Now!




In need of some help T_T

 
Reply to this topicStart new topic

In need of some help T_T, Calling different forms from a combox

Chris Ross
30 Sep, 2008 - 04:22 PM
Post #1

New D.I.C Head
*

Joined: 26 Sep, 2008
Posts: 4

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.

csharp

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.gif



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 Sep, 2008 - 04:38 PM
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: In Need Of Some Help T_T
30 Sep, 2008 - 04:39 PM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,228



Thanked: 218 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

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

csharp

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!" decap.gif
User is online!Profile CardPM
+Quote Post

Chris Ross
RE: In Need Of Some Help T_T
30 Sep, 2008 - 04:47 PM
Post #3

New D.I.C Head
*

Joined: 26 Sep, 2008
Posts: 4

Wow that is what I was looking for, thank you so much. happy.gif
User is offlineProfile CardPM
+Quote Post

Martyr2
RE: In Need Of Some Help T_T
30 Sep, 2008 - 04:54 PM
Post #4

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,228



Thanked: 218 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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...

csharp

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. smile.gif
User is online!Profile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 03:26PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month