将复选框连接到组合框C#GUI

编程入门 行业动态 更新时间:2024-10-11 13:28:26
本文介绍了将复选框连接到组合框C#GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

你好, 我正在尝试将复选框与选定的组合框连接起来,但是我不确定如何将其连接到阵列. 有人可以帮忙吗? 这是代码.

Hello, I''m trying to connect the checkbox with the selected combobox but I''m just not sure how to connect it to the arrays. Can someone please help? Here''s the code.

string[] regionType = { "Europe", "Asia", "Australia" }; double[] rate = { 1950.00, 2250.00, 2550.00 }; double regionRate, airfareCost = 0.0; public Form1() { InitializeComponent(); } private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { int index; index = cboReg.SelectedIndex; regionRate = rate[index]; displayRate(); } private void displayRate() { txtPriceLand.Text = regionRate.ToString("C"); } private void Form1_Load(object sender, EventArgs e) { cboReg.Items.AddRange(regionType); } private void chkYes_CheckedChanged(object sender, EventArgs e) { }

[Edited]代码包装在"pre"标签中[/Edited]

code is wrapped in "pre" tags[/Edited]

推荐答案

您也可以执行以下操作: you can also do this: private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { if (chkYes.Checked) { regionRate = rate[cboReg.SelectedIndex]; displayRate(); } }

如果您希望在复选框的检查更改事件中发生这种情况,请从comboBox1_SelectedIndexChanged事件中删除所有代码,然后将其粘贴到chkYes_checkedChanged方法中.像这样:

if you want that to happen on checkbox''s check change event remove all code from comboBox1_SelectedIndexChanged event and paste it to chkYes_checkedChanged method. like this:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e) { //nothing here } private void chkYes_CheckedChanged(object sender, EventArgs e) { //Make sure cboReg.SelectedIndex is >= 0 because if nothing is selected //it will be -1 and that will cause an exception if (chkYes.Checked && cboReg.SelectedIndex >= 0) { regionRate = rate[cboReg.SelectedIndex]; displayRate(); } }

谢谢, 提示

Thanks, Hemant

这里是解决方案:设置属性Autopostback = True Here is the solution: Set your properties Autopostback = True private void chkYes_CheckedChanged(object sender, EventArgs e) { if (chkYes.checked == true && comboBox1.SelectedValue ="Europe" ) { txtPriceLand.Text = "1950.00" } else if (chkYes.checked == true && comboBox1.SelectedValue ="Asia" ) { txtPriceLand.Text = "2250.00" }

}. 这对您来说要容易得多.

} . this is much easier for you.

更多推荐

将复选框连接到组合框C#GUI

本文发布于:2023-11-12 11:09:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1581327.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   连接到   复选框   GUI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!