怎么把switch-case/if-else转换成委托
求助 如何把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会造成代码冗余等等问题还是有数)
哪位大神帮我写一份参考参考啊 万分感激!
------解决思路----------------------
Dictionary<string, int> dir = new Dictionary<string, int>();
dir.Add("TreeNode:A", 1);
dir.Add("TreeNode:B", 2);
dir.Add("TreeNode:C", 3);
dir.Add("TreeNode:D", 4);
listBox1.SelectedIndex = dir[treeView1.SelectedNode.ToString()];
Dictionary<string, string> dir2 = new Dictionary<string, string>();
dir2 .Add("TreeNode:A", "50");
dir2 .Add("TreeNode:B", "30");
dir2 .Add("TreeNode:C", "20");
dir2 .Add("TreeNode:D", "10");
DataText.Text = dir2[treeView1.SelectedNode.ToString()];
要学会变通,不要傻傻的,只会拿别人的代码COPY ,
代码如下:
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会造成代码冗余等等问题还是有数)
哪位大神帮我写一份参考参考啊 万分感激!
------解决思路----------------------
Dictionary<string, int> dir = new Dictionary<string, int>();
dir.Add("TreeNode:A", 1);
dir.Add("TreeNode:B", 2);
dir.Add("TreeNode:C", 3);
dir.Add("TreeNode:D", 4);
listBox1.SelectedIndex = dir[treeView1.SelectedNode.ToString()];
Dictionary<string, string> dir2 = new Dictionary<string, string>();
dir2 .Add("TreeNode:A", "50");
dir2 .Add("TreeNode:B", "30");
dir2 .Add("TreeNode:C", "20");
dir2 .Add("TreeNode:D", "10");
DataText.Text = dir2[treeView1.SelectedNode.ToString()];
要学会变通,不要傻傻的,只会拿别人的代码COPY ,