首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

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

2012-04-17 
刚学c#可视化编程,请教各位大神问题;谢谢啦using Systemusing System.Collections.Genericusing System.

刚学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("还没选择");} 

热点排行