Observed:
System.ArgumentOutOfRangeException was unhandled
Message="InvalidArgument=Value of '0' is not valid for
'SelectedIndex'.\r\nParameter name: SelectedIndex"
t2.c1 contains legal value.
Why this error occurs ?
How to fix ?
Andrus.
using System.Windows.Forms;
using System.Data;
using System;
class testForm : Form {
testForm() {
DataTable t = new DataTable();
t.Columns.Add("displaymember");
t.Columns.Add("valuemember");
t.Rows.Add("lower", "l");
ComboBox comboBox1 = new ComboBox();
comboBox1.DisplayMember = "displaymember";
comboBox1.ValueMember = "valuemember";
comboBox1.DataSource = t;
comboBox1.AutoCompleteSource = AutoCompleteSource.ListItems;
comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
DataTable t2 = new DataTable();
t2.Columns.Add("c1");
t2.Rows.Add("l");
comboBox1.DataBindings.Add("SelectedValue", t2, "c1");
Controls.AddRange(new Control[] { comboBox1 });
}
[STAThread]
static void Main() {
Application.Run(new testForm());
}
}
--
HTH,
Kevin Spencer
Microsoft MVP
Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net
"Andrus" <kobru...@hot.ee> wrote in message
news:eMNs7PNt...@TK2MSFTNGP05.phx.gbl...
thank you.
The code does not set SelectedIndex as all.
Why .NET tries to set SelectedIndex to invalid value ?
Andrus.
> It isn't legal if it's a string.
>>
You can't select a specific item from a combobox that doesn't contain
any items at all.
You haven't databound the combobox, so it doesn't contain any items.
--
Göran Andersson
_____
http://www.guffa.com
You are setting the SelectedValue property. That will basically look for
the value among the items, and set the SelectedIndex property for the
matching item.
Hi,
This is *totally* a guess, because I haven't tried your code, but try moving
this line:
comboBox1.DataSource = t;
So it immediately follows this line:
ComboBox comboBox1 = new ComboBox();
--
Tom Spink
University of Edinburgh
InvalidArgument=Value of '41' is not valid for 'SelectedIndex'.
Parameter name: SelectedIndex
System.ArgumentOutOfRangeException
Stack Trace:
at System.Windows.Forms.ComboBox.set_SelectedIndex(Int32 value)
at SpeedConnect7VB.SpeedConnect.Display_Defaults()
at SpeedConnect7VB.SpeedConnect.SpeedConnect_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.SetVisibleCore(Boolean value)
at System.Windows.Forms.Form.SetVisibleCore(Boolean value)
at System.Windows.Forms.Control.Show()
at SpeedConnect.FormOptimizer.ReloadOptimizer()
at SpeedConnect.FormOptimizer.FormOptimizer_VisibleChanged(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.ScrollableControl.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Form.OnVisibleChanged(EventArgs e)
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
Posted via DevelopmentNow.com Groups
http://www.developmentnow.com/g/
I'm not sure why you responded to a post from several years ago...
At the line where you get the error, how many items does the debugger
say are in the control? If there are 41 items, you must use
SelectedIndex in the range 0 to 40.
--
Mike