views:

374

answers:

3

Hi I'm new to C#. And I'm doing some exercises about switch. I just did it from console application and I would like to do it in window forms applications. I'm looking for syntax on how to do switch in window forms. In console it's usually like this:

switch (wordValue)
    {
     case 1:
     Console.WriteLine("You have entered numbered two");
     break;
     default:
     break;

how can I do this in my window forms, if I would like to display this cases in listbox1?

Thanks

=======

Thank you. I tried this one but I'm getting an error. This is what I've tried:

     public static void WriteNumber(int wordValue)
    {
        switch (wordValue)
        {
            case 1:
               listbox.Items.Add("You have entered number one");
                break;
        }
    }

========

This is the code I'm trying to do:

    private void btnOk_Click(object sender, EventArgs e)
    {
        string strUserInputNumber;
        strUserInputNumber = textBox1.Text.Trim();
        Int32 intNumber;
        if (Int32.TryParse(textBox1.Text, out intNumber))
        {
            listBox1.Items.Add(intNumber.ToString());
        }
    }


  public static void WriteNumber(int wordValue)
    {
        switch (wordValue)
        {
            case 1:
               this.listBox1.Items.Add("You have entered numbered one");
                break;
        }
     }

====

This is the new code:

    private void btnOk_Click(object sender, EventArgs e)
    {
        string strUserInputNumber;
        strUserInputNumber = textBox1.Text.Trim();
        Int32 intNumber;
        if (Int32.TryParse(textBox1.Text, out intNumber))
        {
            listBox1.Items.Add(intNumber.ToString());
            WriteNumber(intNumber);

        }
        else
        {
           MessageBox.Show("Please enter an integer not a character");
        }
    }

    public void WriteNumber(int wordValue)
    {
        switch (wordValue)
        {
            case 1:
                listBox2.Items.Add("You have entered numbered one");
                break;
            case 2:
                listBox2.Items.Add("You have entered numbered two");
                break;
            case 3:
                listBox2.Items.Add("You have entered numbered three");
                break;
            default:
                listBox2.Items.Add("You have exceeded the range of 1-3. Please enter the number between 1-3");
                break;
        }
+4  A: 

The switch/case syntax is identical between WinForms and a console app (or any other type of application or class library), the only difference is how you display the data. If you want to add a string to a listbox (which is apparently what you're asking), it's as simple as

listBox1.Items.Add("Here is the text of the list box item");
Factor Mystic
A: 

This works fine:

switch (wordValue)
{
  case 1:
    this.listBox1.Items.Add("You have entered numbered two");
    break;
  default:
    break;
}
Razzie
thanks Razzie, but I'm still getting an error maybe because I'm declaring it as another function. I'll post the additional code maybe that helps
tintincute
+3  A: 

This should work:

public void WriteNumber(int wordValue) 
{ 
   switch (wordValue) 
   { 
      case 1: 
         listbox.Items.Add("You have entered number one"); break; 
   } 
}

You need to remove the static keyword to get access to the listbox, which is an instance variable.

BengtBe
it still says unexpected token BengtBe. I'm quite stuck now.
tintincute
You have written listbox in two different ways: listBox1 and listbox1, maybe that is the problem?
BengtBe