读书人

刚学c#可视化编程大神有关问题;多谢

发布时间: 2012-04-17 15:06:33 作者: rapoo

刚学c#可视化编程,请教各位大神问题;谢谢啦
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 m1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e) {
this.listBox1.Items.Add("zhangsan");
this.listBox1.Items.Add("lisi");
this.listBox1.Items.Add("wangwu");
}
private void button1_click(object sender, EventArgs e) {
if (this.listBox1.SelectedIndex != 1)
{
MessageBox.Show("当前的选择是:" + this.listBox1.SelectedItem.ToString());
}
else {
MessageBox.Show("还没选择");
}
}



}
}
这是form1中的代码;
因为不会插图,所以抱歉;
就是在列表框中有三个选项,分别是zhangsan,lisi,wangwu;
点了任何一个再点按钮确定的话,消息框会有显示;
但是为什么在点了之后没有任何显示

[解决办法]
if (this.listBox1.SelectedIndex != 1)
改为
if (this.listBox1.SelectedIndex != -1),如果没有选择,SelectedIndex =-1;

[解决办法]
2楼正解
[解决办法]

C# code
        private void Form1_Load(object sender, EventArgs e)        {            this.listBox1.Items.Add("zhangsan");            this.listBox1.Items.Add("lisi");            this.listBox1.Items.Add("wangwu");        }        private void button1_Click(object sender, EventArgs e)        {            if (this.listBox1.SelectedIndex != -1)            {                MessageBox.Show("当前的选择是:" + this.listBox1.SelectedItem.ToString());            }            else            {                MessageBox.Show("还没选择");            }        }
[解决办法]
C# code
if (this.listBox1.SelectedIndex != -1){MessageBox.Show("当前的选择是:" + this.listBox1.SelectedItem.ToString());}else{MessageBox.Show("还没选择");} 

读书人网 >C#

热点推荐