Program is a test to see is it working, I just want to use 2 comboboxes to return me Selected.Index, so I can point to a location in my table (matrix) which has probability values (double).When I run it, I get "female"=-1 and warning that of course i can't access table's location with -1 index, as comboBox2.SelectedIndex always returns me -1,I don't know why is this happening. comboxBox1.SelectedIndex returns ok value. this my "main" code (Form1.cs). I created custom event handler "chance" and add it to comboBox1 and comboBox2 SelectedIndexChange event
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespacetest
{
publicpartialclassForm1 : Form
{
privatedouble[,] table
={
{.5,.8,.6,.4,.2,.3},
{.8,.5,.4,.2,.6,.8}};
double outcome;
publicForm1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0; //male
comboBox2.SelectedIndex = 0; //female
}
publicvoidchance(object sender, EventArgs e)
{
int male, female;
male = comboBox1.SelectedIndex;
female = comboBox2.SelectedIndex;
outcome = table[male, female];
label1.Text = outcome.ToString();
}
}
}
And designer's code:
C#
namespacetest
{
partialclassForm1
{
//////
Required designer variable.
///private System.ComponentModel.IContainer components = null;
//////
Clean up any resources being used.
//////trueif managed resources should be disposed; otherwise,
false.
protectedoverridevoidDispose(bool disposing)
{
if (disposing && (components !=
null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code//////
Required method for Designer support - donot modify
///
the contents of this method with the code editor.
///privatevoidInitializeComponent()
{
this.comboBox1 = new
System.Windows.Forms.ComboBox();
this.comboBox2 = new
System.Windows.Forms.ComboBox();
this.label1 = new
System.Windows.Forms.Label();
this.SuspendLayout();
// //
comboBox1
// this.comboBox1.FormattingEnabled =
true;
this.comboBox1.Items.AddRange(newobject[] {
"Billy",
"Andy"});
this.comboBox1.Location = new
System.Drawing.Point(52, 42);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new
System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 0;
this.comboBox1.SelectedIndexChanged +=
new System.EventHandler(this.chance);
// //
comboBox2
// this.comboBox2.FormattingEnabled =
true;
this.comboBox2.Items.AddRange(newobject[] {
"Mary",
"Suzy",
"Ann",
"Gerthruda",
"Rose",
"Angela"});
this.comboBox2.Location = new
System.Drawing.Point(280, 42);
this.comboBox2.Name = "comboBox2";
this.comboBox2.Size = new
System.Drawing.Size(119, 21);
this.comboBox2.TabIndex = 1;
this.comboBox2.SelectedIndexChanged +=
new System.EventHandler(this.chance);
// //
label1
// this.label1.AutoSize = true;
this.label1.Location = new
System.Drawing.Point(214, 139);
this.label1.Name = "label1";
this.label1.Size = new
System.Drawing.Size(35, 13);
this.label1.TabIndex = 2;
this.label1.Text = "label1";
// //
Form1
// this.AutoScaleDimensions = new
System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode =
System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new
System.Drawing.Size(480, 383);
this.Controls.Add(this.label1);
this.Controls.Add(this.comboBox2);
this.Controls.Add(this.comboBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregionprivate System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.Label
label1;
}
}
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2010, 8:02 AM EDT
✓ Accepted answer0
Okay well if I understand the problem then I probably have an answer. Put the following in "chance" before calculating "outcome".
if (male == -1 || female == -1) return;
What is happening is that "chance" is executing one or two times before InitializeComponent is called, so you are getting incorrct data. You can find the problem by putting a breakpoint in "chance" and in InitializeComponent. Do you know how to use breakpoints?
0
Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2010, 7:35 AM EDT
0
Welcome to C# Corner.
Note that it really helps if you can use a title for your threads that are more relevant than "newbie question". It helps you since then your question is more likely looked at by people that are the most likely to have the best answers.
I am sorry but I don't understand the question in this thread. I hope someone else understands better.
I know few things about C++ but it isn't my main occupation, I must implement some image processing algoritham (and we do it all in Matlab) and i wanted to do it in C# , it's part of my master's thesis. So I'm learning it on the fly. I understand now where is a problem. At moment when SelectedIndex changes (comboBox1.SelectedIndex=0) call to event handler is made, but at that time no comboBox2.SelectedIndex is setup (it's -1). Thx a million , your explanation made it clear
Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2010, 8:02 AM EDT
What is happening is that "chance" is executing one or two times before InitializeComponent is called, so you are getting incorrct data. You can find the problem by putting a breakpoint in "chance" and in InitializeComponent. Do you know how to use breakpoints?
Sam HobbsRank #107 · 20.5K reputationPosted Oct 22, 2010, 7:35 AM EDT
Note that it really helps if you can use a title for your threads that are more relevant than "newbie question". It helps you since then your question is more likely looked at by people that are the most likely to have the best answers.
I am sorry but I don't understand the question in this thread. I hope someone else understands better.
Mata37 reputationPosted Oct 22, 2010, 8:23 AM EDT