472,342 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,342 software developers and data experts.

Combobox selectedindex reverting after OnLeave

Hi all,

I have a control that extends the ComboBox object. It
updates the selected item based on what the user enters in
the text area.

In the OnLeave event of the combobox, the selected index
is set to the proper item. However, when accessing its
selectedindex later it has reverted to the previous
value. The selectedindex value is always one behind what
it should currently be when accessed outside of the
combobox.

Any help would be much appreciated.

I would be glad to provide code and further info if that
would help better describe the issue.
Thanks a lot!

ross
Nov 15 '05 #1
2 4424
I would be glad to provide code and further info if that
would help better describe the issue.


I think it probably would. It might be something as simple as remembering
which controls are 0-based indices and which are 1-based ;-)

Cheers
Grant
Nov 15 '05 #2
-----Original Message-----
I would be glad to provide code and further info if that
would help better describe the issue.
I think it probably would. It might be something as

simple as rememberingwhich controls are 0-based indices and which are 1- based ;-)
Cheers
Grant
.

Cool, code to follow.

Just to clarify, when i access the selectedindex property
in the selectedIndexChanged method, i can get the proper
value.
When i access the selectedindex property from outside the
control, i get what should be the previous value.

Any help would be much appreciated!
public class SelectableComboBox :
System.Windows.Forms.ComboBox
{

private System.ComponentModel.Container
components = null;

bool LimitToList = true;
public SelectableComboBox()
{
InitializeComponent();
}

protected override void Dispose( bool
disposing )
{
if( disposing )
{
if( components != null )
components.Dispose
();
}
base.Dispose( disposing );
}

#region Component Designer generated code

private void InitializeComponent()
{

}
#endregion

protected override void OnKeyPress
(KeyPressEventArgs e)
{
/*
* if the user pressed a non-
printing key
* let the default keypress
handler perform the
* default action
* KeyChar values in Hex (why
not :)
* 0x08 == Back Space character,
handled separately
*/
if (e.KeyChar != 0x08 &&
(e.KeyChar < 0x20 ||
e.KeyChar > 0x7E))
{
base.OnKeyPress(e);
return;
}

/*
* if the user pressed a character
or backspace
* then we need to drop down the
combobox and
* select the closest match for
the text and set
* the selected text.
*/
this.DroppedDown = true; // drop
down the combobox
String oldText, newText;
// set oldText to only the text
the user has typed so far
oldText = this.Text.Substring(0,
this.SelectionStart);

if (e.KeyChar == 0x08) //
backspace pressed
{
try // make sure we don't
cause an exception when subtracting 1 from our oldText
length
{
this.Text =
oldText.Substring(0, oldText.Length - 1);
}
catch (System.Exception
e2)
{
this.Text = "";
this.SelectedIndex
= -1;
return;
}
/*
* the following if was
added to resolve an issue i was having where
* the combox box would
select a record and then not remove
* it the next time
backspace was pressed
*/
if (this.Text.Length == 0)
{ this.SelectedIndex = -1; return; }
/*
* the following three
lines were added to resolve
* an issue i was having
where the combobox wouldn't
* reselect the current
item and place it's value
* into the text box.
*/
newText = this.Text; //
save the value of the text
this.SelectedIndex = -
1; // deselect the current item
this.Text = newText; //
restore the value of the text

this.SelectedIndex =
this.FindString(this.Text, 0); // find the nearest match
to our typed text
this.SelectionStart =
oldText.Length - 1; // set the selection to only the text
the user hasn't typed
}
else
{
/*
* the following lines
were added to allow a limit
* to list functionality.
* if the new text isn't
in the list, replace
* with the current that
presumeably is in the list
*/
if ((this.LimitToList ==
true) &&
(this.FindString
(oldText + e.KeyChar.ToString(), 0) == -1))
{
e.Handled = true;
return;
}

// set this.text = the
previously typed text plus the latest char typed
this.Text = oldText +
e.KeyChar.ToString();

/*
* the following three
lines were added to resolve
* an issue i was having
where the combobox wouldn't
* reselect the current
item and place it's value
* into the text box.
*/
newText = this.Text;
this.SelectedIndex = -1;
this.Text = newText;

this.SelectedIndex =
this.FindString(this.Text, 0); // find the nearest match
to our typed text
this.SelectionStart =
oldText.Length + 1; // set the selection to only the text
the user hasn't typed
}

// finally set the length of the
selection so it goes to the end
if (this.Text.Length >
this.SelectionStart)
{
this.SelectionLength =
(this.Text.Length - this.SelectionStart);
}
e.Handled = true; // let the
other handlers know we've taken care of this keypress
}
protected override void OnLeave(EventArgs
e)
{
base.OnLeave (e);
this.Refresh();
//this.SelectedIndex =
this.FindString(this.Text, 0); // find the nearest match
to our typed text
//System.Console.WriteLine
("Selected Index: " + this.SelectedIndex + "\n");
}

protected override void
OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged (e);
System.Console.WriteLine("Selected
index: " + this.SelectedIndex + "\n");
}

}
}
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: jim | last post by:
How do I immediately set the SelectedIndex of a combobox (dropdownlist) upon Form Load? I'm getting out of range errors and I suspect it's due...
3
by: ScottO | last post by:
I would like the user to have to select something in a System.Windows.Forms.ComboBox. private void MyForm_Load(object sender, System.EventArgs e)...
0
by: Dave | last post by:
Greetings, I am using a tabstrip in a user control (ascx) in a web application (aspx) that is used to navigate between the different aspx pages. ...
1
by: mike | last post by:
Hi, I'd like advice from a .NETer. I have a loadcombo routine which sets the selectedindex to -1 if it's an "add record", but then it goes to...
4
by: Strahimir Antoljak | last post by:
Has anyone experienced problems with a combo box SelectIndex property? Is there a limit to the number of Items for a combo box? Namely, when I...
9
by: Atchoum | last post by:
I have a combobox populated with objects from a class derived from FileInfo. When I try to assign a value to selected index, I get a "Argument...
1
by: Marc Robitaille | last post by:
Hello, I fill a ComboBox with a collection on my own. So I set the DisplayMember and the ValueMember properties to some fields of my collection....
3
by: Magnus | last post by:
Im using a set combobox (ComboBox1) to provide a selection of records from a database table. I have a typed dataset (DataSet1) that contains the...
5
by: Rotsey | last post by:
Hi, I have a combobox that when I set the SelectedIndex to -1 it sets to 0. The combobox Items property says there is 20 items in it. ...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.