如何在两个按钮单击事件之间传递变量 [英] how do i pass variables between two button click events

查看:59
本文介绍了如何在两个按钮单击事件之间传递变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在表格1上有两个按钮.

在按钮A的事件中,我使用sql从db中选择ID和名称的列表,并将其显示在列表框中.
现在,我要做的就是将列表框中当前所选项目的ID传递给bttn B的事件.

下面是我在列表框中显示名称的代码.

I have two buttons on form 1.

In button A''s event, I used sql to select a list of id''s and names from db and displayed it in a listbox.
Now what I want to do is to pass the id of the currently selected item in the listbox to bttn B''s event.

Below is my code for displaying the names in the listbox.

String connection;  
String sql = "select FileLoc, Lname from apps where dept = '" + cbdept.Text + "' and role = '" + cbrole.Text + "' and rating = '" + cbrating.Text + "' and yearsofexp = '" + exp.Text + "' and thirddeg = '"+edulev+"'";

SqlDataReader reader = null;
SqlConnection conn = new SqlConnection(connection);
SqlCommand cmd = new SqlCommand(sql, conn);

try
{
    conn.Open();
    reader = cmd.ExecuteReader();
    searchresults.Enabled = true;
                                               
    // ArrayList rslts = new ArrayList();
    while (reader.Read())
    {
    //    rslts.Add(new rslt(reader["Lname"].ToString(), reader["FileLoc"].ToString()));
                   
        searchresults.Items.Add(reader["Lname"].ToString());
        // searchresults.ValueMember = reader["FileLoc"].ToString();
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString());
}

推荐答案

列表通常与按钮具有相同的形式,因此它可以访问列表并可以检索索引本身:
Normally the list would be on the same form as the button so it has access to the list and can retrieve the index itself:
string curItem = listBox1.SelectedItem.ToString();
int index = listBox.SelectedIndex;



否则,在表单中为所选项目声明一个私有变量.此私有变量仅可用于表单中的方法,并且两个事件处理程序都可以访问,因此它们可以访问该私有变量并使用它们交换数据.



Otherwise declare a private variable for the selected item in your form. This private variable is only accessible for methods within your form and both event handlers are so they can access it and exchange data using that.

public class Form1 : System.Windows.Forms.Form
{
  private int selectedIndex;
...
}



祝你好运!



Good luck!


解决问题的一种方法是为数据创建一个小类,并将其实例添加到ListBox.

像这样的东西:
One way to resolve your problem would be to create a small class for your data and add instances of it to the ListBox.

Something like:
public class SearchItem
{
  public string LastName {get; set;}
  public int ID {get; set;}

  public override string ToString()
  {
      return this.LastName;
  }
  ..........
  ..........
  ..........
}



然后使用它:



Then to use this:

SearchItem newItem = null;
while (reader.Read())
{
  newItem = new SearchItem();
  newItem.LastName = reader["Lname"].ToString();
  newItem.ID = reader["ID"].ToString();
  searchresults.Items.Add(newItem);
}



要访问ID:



To access the Id:

int testID = ((SearchItem)searchresults.Items[x]).ID;



希望这可以帮助. :)



Hope this helps. :)


使用查询结果的属性为您的搜索结果创建一个类.
Create a class for your search results with properties of the query results.
public class Applicant
{
public int ID { get; set; }
public string LName { get; set; }
//the rest of parameters
}


阅读时,创建Applicant的新实例,填充参数并将这些实例插入到列表框中:


while reading, create new instances of Applicant, fill the parameters and insert these instances to the Listbox:

....
Applicant newApplicant;
using (SqlConnection conn)
{
...
using(SqlDatareader reader = cmd.ExecuteReader())
{
while (reader.Read())
    {
        newApplicant = new Applicant();
        newApplicant.ID = reader.GetInt32(0);
        newApplicant.LName = reader.GetString(1);
        ....
        ListBox.Items.Add(newApplicant);
    }
}
}


通过这种方式,在列表框中,您将拥有申请人实例.在按钮b下,您可以将选定的ID获取为:


By this way, in the listbox you will have instances of Applicant. Under button b, you can get the selected id as:

Applicant selectedApplicant = ListBox1.SelectedItem as Applicant;
int requiredID = selectedApplicant.ID;



作为两个要点,我强烈建议在sql查询中使用SqlParameter而不是添加字符串,并在完成后关闭任何资源(如连接,读取器等)



As two quick notes, I highly recommend using SqlParameter in sql queries instead of string addition and close any reasources after you are done (like connections, readers, etc)


这篇关于如何在两个按钮单击事件之间传递变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆