从列表框到消息框的选定索引

时间:2012-04-18 01:20:52

标签: c# winforms

  

可能重复:
  Displaying information in messagebox from listbox

我正在编写一个程序,要求用户为他们的房子或公寓输入七个不同的参数。提交信息后,输入的地址将进入列表框。当用户单击单独的按钮时,七个参数应显示在消息框中。我已经有一个名为DisplayInfo()的方法,它在调用时会在列中显示信息,所以我只需要帮助选择它的索引部分。

public virtual string DisplayInfo()
{
  return string.Format("Property ID: {0}\nProperty Address: {1}\nYear Built: {2}\nNumber of Bedrooms: {3}\nSquare Footage: {4}\nPrice: {5}",
    GetID(),
    GetAddress(),
    GetYearBuilt(),
    GetBedrooms(),
    GetSquareFootage(),
    GetPrice());
}

3 个答案:

答案 0 :(得分:3)

对于按钮,连接Click事件:

public Form1() {
  InitializeComponent();
  button1.click += new EventHandler(button1_Click);
}

void button1_Click(object sender, EventArgs e) {
  if (listBox1.SelectedIndex > -1) {
    MessageBox.Show(DisplayInfo());
  }
}

答案 1 :(得分:0)

if(lbox.SelectedItem != null)
 {
    DisplayInfo(lbox.SelectedItem);
 }

答案 2 :(得分:0)

hi在listbox的selectedIndex更改事件方法中调用" DisplayInfo"

的方法
Protected Void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
  if(listBox1.SelectedIndex!=-1)
  { 
     DisplayInfo(listBox1.SelectedItem.ToString());
  }
}