PDA

View Full Version : switch درc#



sara.sherafati
دوشنبه 20 شهریور 1391, 14:41 عصر
n = listBox1.Items(listBox1.SelectedIndex).Tostring();
به itemsارور میده و نمیدونم چرا
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 test3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string n;
string m = "";
n = listBox1.Items(listBox1.SelectedIndex).Tostring();
switch (n)

{
case "sara":
m = "yellow";
break;
case"masood":
m = "blue";
break;
case "hasan":
m = "Morning Mist";
break;
case "sahar":
m = "dokhi amoo";
break;
case"reza":
m = "name of boy";
break;
case"roya":
m = "name of girl";
default:
break;
}
MessageBox.Show(n+"chizi k bayad gofte she"+ m, "select demo");
}
}
}

tooraj_azizi_1035
دوشنبه 20 شهریور 1391, 14:47 عصر
داخل [] بذار.

sinaprog
دوشنبه 20 شهریور 1391, 14:52 عصر
به جای پرانتز [ ] بزن

sara.sherafati
دوشنبه 20 شهریور 1391, 15:02 عصر
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 test3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
string n;
string m = "";
n = listBox1.Items
[listBox1.SelectedIndex].Tostring();
switch (n)

{
case "sara":
m = "yellow";
break;
case"masood":
m = "blue";
break;
case "hasan":
m = "Morning Mist";
break;
case "sahar":
m = "dokhi amoo";
break;
case"reza":
m = "name of boy";
break;
case"roya":
m = "name of girl";
default:
break;
}
MessageBox.Show(n+"chizi k bayad gofte she"+ m, "select demo");
}
}
}



be tostring hala eror mide:متعجب:

Farshid007
دوشنبه 20 شهریور 1391, 15:59 عصر
بعد roya
break رو یادتون رفته
باید اول بریک بشه بعد default بیاد
default اینجا یعنی در غیر اینصورت
در ضمن
ToString
نه Tostring

sara.sherafati
دوشنبه 20 شهریور 1391, 16:38 عصر
ممنون حل شد