求助 如何把switch-case/if-else转换成委托啊
代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace killIfElse
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
createListBox();
}
public void createListBox()
{
listBox1.Items.Add("1");
listBox1.Items.Add("2");
listBox1.Items.Add("3");
listBox1.Items.Add("4");
}
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
if(listBox1.SelectedIndex==0)
{
textBox1.Text = listBox1.SelectedItems[0].ToString();
}
else if (listBox1.SelectedIndex == 1)
{
textBox1.Text = listBox1.SelectedItems[0].ToString();
}
else if (listBox1.SelectedIndex == 2)
{
textBox1.Text = listBox1.SelectedItems[0].ToString();
}
else if (listBox1.SelectedIndex == 3)
{
textBox1.Text = listBox1.SelectedItems[0].ToString();
}
}
}
}
这里是鼠标点击ListBox1后会将ListBox1的内容显示到TextBox上,我参考了张子阳的委托实例,结果还是不对。
(因为我这里要写个项目,listbox1里面起码得有三百行,如果全是ifelse或swithcase会造成代码冗余等等问题还是有数)
哪位大神帮我写一份参考参考啊 万分感激!