c# - 如何从 2 个不同的组合框中获取选定的值以显示在 1 个文本框或标签内
问题描述
您好,我需要在文本框或标签内显示来自 2 个不同组合框中的选定值。我试过这段代码
int i = (int)comboBox1.SelectItem - (int)comboBox2.SelectedItem;
i += int;
{
textBox1.Text = i.ToString
}
除了我不知道将代码放在哪里之外,该代码根本没有做任何事情。我在其中一个组合框中尝试了不同的代码,SelectedIndexChanged
但它只适用于特定的代码,comboBox
不适用于两者。
for (int i = 0; i < comboBox1.Items.Count; i++)
{
textBox1.Text = comboBox1.Text;
}
我真的很感激我能得到的任何帮助。谢谢你们。
解决方案
一个解决方案是在表单中有一个按钮和两个组合框和一个标签。当您按下按钮时,会添加从组合框中选择的文本,然后标签中的文本会更改。这是表格
private void button1_Click(object sender, EventArgs e)
{
//make sure that an item is selected
if(comboBox1.SelectedItem != null &&
comboBox2.SelectedItem != null)
{
//treat these like integers
//label1.Text = Convert.ToString((Int32.Parse(comboBox1.SelectedItem.ToString()) + (Int32.Parse(comboBox2.SelectedItem.ToString()))));
//treat them like strings
label1.Text = comboBox1.SelectedItem.ToString() + comboBox2.SelectedItem.ToString();
}
}
推荐阅读
- c# - 加入并排序 2 个不相关的表
- java - Java 8 Streams:流式传输文件并在读取后删除
- android - RazorpayFlutterPlugin 类中的 RazorpayFlutterPlugin 不能应用于给定类型;
- javascript - 在函数中使用更新状态
- arrays - 更新中数组中位置元素的参考值
- node.js - 如何自定义/覆盖strapi组件
- sql - 如何使用 hive 将表中的列值转换为行值
- angular - Angular 应用程序组件动画不起作用
- android - Firebase 实时数据库:监听具有特定值的更改
- android - 将图片从手机上传到 RecyclerView GridView