你好, 我正在尝试将复选框与选定的组合框连接起来,但是我不确定如何将其连接到阵列. 有人可以帮忙吗? 这是代码.
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
发布评论