现在combobox里面有选项 a,b,c
当我选择a的时候, textbox1 将会显示1
当我选择b的时候, textbox1 将会显示2
当我选择c的时候, textbox1 将会显示3
想请问这样我应该如何去coding
现在combobox里面有选项 a,b,c
当我选择a的时候, textbox1 将会显示1
当我选择b的时候, textbox1 将会显示2
当我选择c的时候, textbox1 将会显示3
想请问这样我应该如何去coding
帮助到你可以点击右上角吗,谢谢~~如果需要工程文件可以发站内短信给我~
using System;
using System.Collections;
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;
using System.Text.RegularExpressions;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Dictionary<string, string> data = new Dictionary<string, string> { { "a", "1" }, { "b", "2" }, { "c", "3" } };
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = data[comboBox1.Text];
}
}
}
数据绑定版本,可以直接从数据库读取,不用做键值对隐射
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = comboBox1.SelectedValue.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
var data = new[] { new { text = "a", value = "1" }, new { text = "b", value = "2" }, new { text = "c", value = "3" } };//可以改为读数据库
comboBox1.DisplayMember = "text";
comboBox1.ValueMember = "value";
comboBox1.DataSource = data;
}