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
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;
namespace test
{
public partial class
Form1 : Form
{
private double[,] table
={
{.5,.8,.6,.4,.2,.3},
{.8,.5,.4,.2,.6,.8}};
double outcome;
public Form1()
{
InitializeComponent();
comboBox1.SelectedIndex = 0; //male
comboBox2.SelectedIndex = 0; //female
}
public void chance(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:
namespace test
{
partial class Form1
{
///
///
Required designer variable.
///
private System.ComponentModel.IContainer components = null;
///
///
Clean up any resources being used.
///
///
true if managed resources should be disposed; otherwise,
false.
protected override
void Dispose(bool disposing)
{
if (disposing && (components !=
null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
///
///
Required method for Designer support - do not modify
///
the contents of this method with the code editor.
///
private void
InitializeComponent()
{
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(new
object[] {
"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(new
object[] {
"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();
}
#endregion
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.ComboBox comboBox2;
private System.Windows.Forms.Label
label1;
}
}
|
Sam HobbsPosted Oct 22, 2010, 12:02 PM
Okay well if I understand the problem then I probably have an answer. Put the following in "chance" before calculating "outcome".
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?
MataPosted Oct 22, 2010, 12:23 PM
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 HobbsPosted Oct 22, 2010, 11:35 AM
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.