如果单击列表框中的某个值,如何使多个列表框更改值?

时间:2014-02-22 06:47:38

标签: c# wpf vb.net listbox

请原谅我的语法。 目前我有一些在VB上完美运行的代码,当选择列表框中的一个值时,使多个列表框移动,我的问题是可以帮助我将这些代码转换为c#吗?提前谢谢。

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

3 个答案:

答案 0 :(得分:1)

已转换

   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

答案 1 :(得分:0)

你需要演员。

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;
    }
}

答案 2 :(得分:0)

如果您未在设计时将ListBox5_Click方法绑定到列表框,则可能还需要将以下内容添加到构造函数中。

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