3
Answers

Selected.Index problem

Ask a question
Mata

Mata

13y
7.2k
1

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
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

#region
Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

Answers (3)