473,884 Members | 2,415 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 4539
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
3012
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
16280
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
1280
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
2419
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
2620
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
4654
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
1605
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
6738
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
6895
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
9799
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
11168
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...
1
10869
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10426
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
7137
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
5808
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4623
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4231
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3242
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.