现在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;
}
给combobox设置一个onchange事件,在onchange事件中获取下拉选择框的值,用if判断,给textbox赋值。
void ComboBox1SelectedIndexChanged(object sender, EventArgs e)
{
String msg=comboBox1.Text;
if(msg.Equals("a")){
textbox1 .Text = "1";
}else if(msg.Equals("b")){
textbox1 .Text = "2";
}else{
textbox1 .Text = "3";
}
}
代码如上,万望采纳
您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~
ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓
【电脑端】戳>>> https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】 戳>>> https://mall.csdn.net/item/52471?utm_source=1146287632