Combobox selectedindex reverting after OnLeave

R

ross kerr

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
 
G

Grant Robinson

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
 
R

ross kerr

-----Original Message-----


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


.
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");
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top