473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4523
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 selectedIndexCh anged 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 SelectableCombo Box :
System.Windows. Forms.ComboBox
{

private System.Componen tModel.Containe r
components = null;

bool LimitToList = true;
public SelectableCombo Box()
{
InitializeCompo nent();
}

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

#region Component Designer generated code

private void InitializeCompo nent()
{

}
#endregion

protected override void OnKeyPress
(KeyPressEventA rgs 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.DroppedDow n = true; // drop
down the combobox
String oldText, newText;
// set oldText to only the text
the user has typed so far
oldText = this.Text.Subst ring(0,
this.SelectionS tart);

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.Substri ng(0, oldText.Length - 1);
}
catch (System.Excepti on
e2)
{
this.Text = "";
this.SelectedIn dex
= -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.Leng th == 0)
{ this.SelectedIn dex = -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.SelectedIn dex = -
1; // deselect the current item
this.Text = newText; //
restore the value of the text

this.SelectedIn dex =
this.FindString (this.Text, 0); // find the nearest match
to our typed text
this.SelectionS tart =
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.LimitToL ist ==
true) &&
(this.FindStrin g
(oldText + e.KeyChar.ToStr ing(), 0) == -1))
{
e.Handled = true;
return;
}

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

/*
* 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.SelectedIn dex = -1;
this.Text = newText;

this.SelectedIn dex =
this.FindString (this.Text, 0); // find the nearest match
to our typed text
this.SelectionS tart =
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.Leng th >
this.SelectionS tart)
{
this.SelectionL ength =
(this.Text.Leng th - this.SelectionS tart);
}
e.Handled = true; // let the
other handlers know we've taken care of this keypress
}
protected override void OnLeave(EventAr gs
e)
{
base.OnLeave (e);
this.Refresh();
//this.SelectedIn dex =
this.FindString (this.Text, 0); // find the nearest match
to our typed text
//System.Console. WriteLine
("Selected Index: " + this.SelectedIn dex + "\n");
}

protected override void
OnSelectedIndex Changed(EventAr gs e)
{
base.OnSelected IndexChanged (e);
System.Console. WriteLine("Sele cted
index: " + this.SelectedIn dex + "\n");
}

}
}
Nov 15 '05 #3

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

Similar topics

1
3004
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 that the Form has not been displayed yet. Please help.. thanks.
3
16214
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) { ... comboBox.DataSource = data; comboBox.SelectedIndex = -1; ... }
0
1271
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. Whenever I click on one of them, the response.redirect command works fine, but the page reloads with the first tab selected every time. If I try to change the selectedindex, it runs my SelectedIndexChanged routine so that doesn't work either. Is there a way I can change the selectedindex of...
1
2403
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 a security routine which, based on permissions sets the visible property. When the visible property is set.. 'Petition Type lblPetitionType.Visible = False
4
2608
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 set programmatically ComboBox.SelectIndex property with 2000 Items listed in the ComboBox, SelectIndex property accepts assigned value. However, when I try to use SelectIndex
9
4639
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 length must be greater of equal to 0". What is this about? For i = 0 To cboCalc.Items.Count - 1 Dim Item As FileInfoExt Item = cboCalc.Items(i) If Item.TruncatedFileName = DefaultName Then cboCalc.SelectedIndex = i Exit For
1
1577
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. It works perfectly. The problem is when my form showed up, even if I set the SelectedIndex of my ComboBox to -1, the first value in my collection is selected. I check everywhere in my code to be sure that nothing change the SelectedIndex and find nothing. Maybe it is a know problem or it is...
3
6712
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 typed datatable (DataTable1) that the combobox is bound to. The datatable in the dataset is filled using the typed tableadapter (TableAdapter1). DataTable consists of the typical primary key and value fields. Here is the designer generated code for ComboBox1: this.ComboBox1.DataSource =...
5
6888
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. Anyone know why this could be please? rotsey
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8482
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.