使用Listbox或List <t> </t>中的特定变量填充文本框和标签

时间:2015-02-26 18:32:22

标签: c#

如何让我的代码填充文本框,或者在这种情况下使用变量id填充lblUserId.Text?我还想知道如何在每次从列表框中选择不同的用户时更改此ID,我将其命名为lstEditUser。

QueryResult queryResult = null;
String SOQL = "";
SOQL = "select Id, FirstName, LastName, UserRole.Name from user where isactive = true"; /// Pulls All active Users! 2/25/2015 8:30AM
queryResult = Sfdcbinding.query(SOQL);
string id = null;
string firstName = null;
string lastName = null;
string role = null; 


if (queryResult.size > 0)
{               
    for (int i = 0; i < queryResult.size; i++)
    {                    
         User user = (User)queryResult.records[i];
          id = user.Id;
          firstName = user.FirstName;
          lastName = user.LastName;
          role = user.UserRole.Name;
          string[] uSers = { id, firstName, lastName, role };
         listEdit.AddRange(uSers);
         // adds Items in uSers to lstEditUser
         lstEditUser.Items.Add(string.Format("{0}  {1} {2}   {3}", uSers));      
         lstDeleteUser.Items.Add(string.Format("{0}  {1} {2}       {3}",uSers));     

         foreach (var li in listEdit) /// THIS IS WHAT I DONT KNOW HOW TO DO!
         {
             lblUserID.Text = li.Select<string>; /// I WANT IT TO POPULATE THIS LABEL W/ the Variable 'ID' based on the SelectedIndex or Selected Item in the lstEditUser or listEdit
         }

    }
    MessageBox.Show("The query result has found " + queryResult.size  + " users.");

}
else
{

1 个答案:

答案 0 :(得分:1)

您需要订阅lstEditUser.SelectedIndexChanged事件并相应地更改lblUserID.Text

if (queryResult.size > 0)
{               
    for (int i = 0; i < queryResult.size; i++)
    {                    
         User user = (User)queryResult.records[i];
          id = user.Id;
          firstName = user.FirstName;
          lastName = user.LastName;
          role = user.UserRole.Name;
          string[] uSers = { id, firstName, lastName, role };
         listEdit.AddRange(uSers);
         // adds Items in uSers to lstEditUser
         lstEditUser.Items.Add(string.Format("{0}  {1} {2}   {3}", uSers));      
         lstDeleteUser.Items.Add(string.Format("{0}  {1} {2}       {3}",uSers));     
    }
lstEditUser.SelectedIndexChanged += SelectNewUser;
lstEditUser.SelectedIndex = 0;
    MessageBox.Show("The query result has found " + queryResult.size  + " users.");

}

private void SelectNewUser (object sender, EventArgs e)
{
    int idx = lstEditUser.SelectedIndex;
    if (idx < 0)
        return;
    lblUserID.Text = listEdit[idx*4];
}