简体   繁体   English

单击列表框中的值之一时,如何使多个列表框更改值?

[英]How to make multiple listboxes change the value when one of the value in a listbox is clicked?

Please forgive my grammar. 请原谅我的语法。 currently i have some codes that works perfectly on VB which make make multiple listbox move when one of the value in a listbox is selected, my question is can some one help me to convert these code to c#? 目前,我有一些代码可以在VB上完美运行,当选择列表框中的值之一时,可使多个列表框移动。我的问题是有人可以帮助我将这些代码转换为C#吗? thanks in advance. 提前致谢。

Private Sub ListBox5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox5.Click, ListBox4.Click, ListBox3.Click, ListBox2.Click, ListBox1.Click
        Dim lb As New ListBox
        lb = sender
        If lb.SelectedIndex <> -1 Then
            ListBox1.SelectedIndex = lb.SelectedIndex
            ListBox2.SelectedIndex = lb.SelectedIndex
            ListBox3.SelectedIndex = lb.SelectedIndex
            ListBox4.SelectedIndex = lb.SelectedIndex
            ListBox5.SelectedIndex = lb.SelectedIndex
            txtsn.Text = ListBox1.SelectedItem
            txtsa.Text = ListBox2.SelectedItem
            txtsadd.Text = ListBox3.SelectedItem
            txtsp.Text = ListBox4.SelectedItem
            txtse.Text = ListBox5.SelectedItem
        End If
    End Sub

Converted 转换

   private void ListBox5_Click(object sender, System.EventArgs e) {
    ListBox lb = new ListBox();
    lb = (ListBox)sender;
    if ((lb.SelectedIndex != -1)) {
        ListBox1.SelectedIndex = lb.SelectedIndex;
        ListBox2.SelectedIndex = lb.SelectedIndex;
        ListBox3.SelectedIndex = lb.SelectedIndex;
        ListBox4.SelectedIndex = lb.SelectedIndex;
        ListBox5.SelectedIndex = lb.SelectedIndex;
        txtsn.Text = ListBox1.SelectedItem;
        txtsa.Text = ListBox2.SelectedItem;
        txtsadd.Text = ListBox3.SelectedItem;
        txtsp.Text = ListBox4.SelectedItem;
        txtse.Text = ListBox5.SelectedItem;
    }
}

http://www.developerfusion.com/tools/convert/vb-to-csharp/?batchId=841a569f-485c-444d-9b8d-e5668f85965c http://www.developerfusion.com/tools/convert/vb-to-csharp/?batchId=841a569f-485c-444d-9b8d-e5668f85965c

You need cast. 你需要演员。

private void ListBox5_Click(System.Object sender, System.EventArgs e)
{
    ListBox lb = (ListBox)sender;
    if (lb.SelectedIndex != -1) {
        ListBox1.SelectedIndex = lb.SelectedIndex;
        ListBox2.SelectedIndex = lb.SelectedIndex;
        ListBox3.SelectedIndex = lb.SelectedIndex;
        ListBox4.SelectedIndex = lb.SelectedIndex;
        ListBox5.SelectedIndex = lb.SelectedIndex;
        txtsn.Text = ListBox1.SelectedItem;
        txtsa.Text = ListBox2.SelectedItem;
        txtsadd.Text = ListBox3.SelectedItem;
        txtsp.Text = ListBox4.SelectedItem;
        txtse.Text = ListBox5.SelectedItem;
    }
}

You may also need to add the following to your constructor, if you didn't tie the ListBox5_Click method to the list boxes at design time. 如果在设计时未将ListBox5_Click方法绑定到列表框,则可能还需要将以下内容添加到构造函数中。

ListBox1.Click += ListBox5_Click;
ListBox2.Click += ListBox5_Click;
ListBox3.Click += ListBox5_Click;
ListBox4.Click += ListBox5_Click;
ListBox5.Click += ListBox5_Click;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM