Click to See Complete Forum and Search --> : How to set initial value displayed in combobox


Alicat
June 1st, 2006, 03:37 AM
How do I set the initial value for a combobox list. I tried:

this->comboBox1->SelectedText::set("8599");

which seem to work but I'm no sure where to put code so that it execute before the form is displayed.

Any help appreciated

Lee Cheon-Sin
June 1st, 2006, 02:42 PM
How do I set the initial value for a combobox list. I tried:

this->comboBox1->SelectedText::set("8599");

which seem to work but I'm no sure where to put code so that it execute before the form is displayed.

Any help appreciated

Put it right after the form is created. You can do it even after it starts to display, because the changes are reflected immediately, and the user won't notice.

ThermoSight
June 1st, 2006, 06:12 PM
Hello Lee & Alicat.

Boy, I'm a little confused here ..... Admittedly, I have no experience with the ComboBox, but I am familiar with TextBoxes, ListBoxes and such.

Are you in fact trying to change the text of the selected item .... ***after the user has selected it*** (I see you're using SELECTEDtext)? If so, that'd sure do it although I would think you'd have to give the user time to make a selection - thus I'd choose to do it using an event predicated on selection change or something similar ... not during construction or immediately after the constructor runs.

However, if your interest lies in building the initial ComboBox items, then I would think that you would want to add individual items and their text during the constructor execution. In short, build the box "on the fly", so to speak.

I think you could do that (build the comboBox 'on the fly') using the comboBox->Items, and the ComboBoxCollection:Item constructs, and as Lee suggests, do it while, or immediately after, the contructor runs.

bill

Alicat
June 1st, 2006, 08:05 PM
I really have two issues I'm trying to deal with. In the default setup of the combobox the initial value displayed is blank when you first run the application. I want to be able to have it show a default value, eventually a value I would load from a preference file.

The other issue I have is, I want to be able to display default values in the list (see code below) but also be able to enter a value like say 9467 and press enter to accept it. When I press enter all I get is a beep.

//
// comboBox1
//
this->comboBox1->FormattingEnabled = true;
this->comboBox1->Items->AddRange(gcnew cli::array< System::Object^ >(11) {L"8000", L"8250", L"8500", L"8750", L"9000", L"9250", L"9500", L"9750", L"10000", L"10250", L"10500"});
this->comboBox1->Location = System::Drawing::Point(6, 14);
this->comboBox1->Name = L"comboBox1";
this->comboBox1->Size = System::Drawing::Size(70, 21);
this->comboBox1->TabIndex = 2;
this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);

ThermoSight
June 1st, 2006, 08:47 PM
Hmmmm, yeah, I see what you're getting at.

Perhaps a variation of this ....

void Form1::onTheFly(bool clearExisting){

ComboBox::ObjectCollection *pCollection = comboBox1->Items;
Object *pItem;

if (clearExisting)
pCollection->Clear();

for (int i = 0; i < 3; i++) {
pItem = new Object();
pCollection->Add(pItem);
pCollection->set_Item(i, dynamic_cast <Object *>
(String::Concat(S"Item ", Convert::ToString(i))));
}// for all items

}// end function 'onTheFly'

I think (I HOPE!) the above method would add three items to the combo box. Each item would bear the text "Item x" where 'x' is the zero-based index of the item in the collection (i.e. 0, 1, 2).


Perhaps you could call a method kinda like this when the form constructor runs to build the initial dropdown menu.

Then as required, you could use a similar procedure (Add()) to add the new items bearing the text your user has typed in.

In any case, the above is one example (not necessarily the best method, just A method!) of entering line items into a combo box.


Best wishes,

bill

Alicat
June 1st, 2006, 09:22 PM
OK you lost me a bit. I'm very new to this and don't fully undertand where to place this code to try it in my form. When I try to edit the "Windows Forms Designer generated code" section my forms display is not longer available.

I tried placing in a new form but it wouldn't compile. I know this would make more sense to me later but at the moment I'm still a noobie.

ThermoSight
June 1st, 2006, 09:58 PM
Hi Ali.

Just so that we're both on the same page here, let me say that I'm doing this in C++ .NET. If you're working in something else, this may not be THE solution for you, but it's A solution and is probably easily translated to whatever Windows Forms milieu you're working in.

OK, boilerplate aside, I decided to try this before we go any further. I created a .NET project and declared a function ('onTheFly(bool clearExisting)') and placed a call to that function in the Form constructor as follows ....



#define CLEAR_EXISTING (true)

.....
.....
.....


public __gc class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
onTheFly(CLEAR_EXISTING);
}
.....
.....
.....


So the moment the Form1 constructor is called, the ComboBox will be populated. (Form1 is the form containing the combobox)


Now, off on page Form1a.cpp, I defined the function you saw earlier (permit me to refresh your memory .....)

void Form1::onTheFly(bool clearExisting){

ComboBox::ObjectCollection *pCollection = comboBox1->Items;
Object *pItem;

if (clearExisting)
pCollection->Clear();
for (int i = 0; i < 3; i++) {
pItem = new Object();
pCollection->Add(pItem);
pCollection->set_Item(i, dynamic_cast <Object *>
(String::Concat(S"Item ", Convert::ToString(i))));
}// for all items

}// end function 'onTheFly'


So, the function has been defined, and is called inside the Form1 constructor (the same form that contains the combobox control). Thus, the moment the Form appears on the screen, the combobox is displayed and populated with the three items, ready for the user's selection.


Now, let's say that an event occurs and you want to add a fourth item. I would use a procedure similar to the one above .... use the ComboBoxCollection method Add(System Object *), then use the collection's set_Item(n, Object *) where Object * points to the Text String.

bill

ThermoSight
June 1st, 2006, 10:18 PM
Next Step .....


Now let's assume you want to add an item based on a text string typed into the combobox .....


In the Form1.h [design] page, and with the 'properties' page displayed, create an event handler on, say "Key Up". Immediately thereafter the IDE takes you to page Form1.h in the event handler, where you enter the following.......

private: System::Void comboBox1_KeyUp(System::Object * sender, System::Windows::Forms::KeyEventArgs * e)
{
ComboBox::ObjectCollection *pCollection = comboBox1->Items;
Object *pItem;
int i;

if (e->KeyCode == Keys::Return) {
pItem = new Object();
i = pCollection->Add(pItem);
pCollection->set_Item(i, dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text)));
comboBox1->Text = S"";
}

}


The first line above is the entry to the event handler. The event is a keystroke in the Text portion of the combobox.

Notice the pCollection and pItem declarations. You've seen those before.

Now the IF statement ignores all keystrokes EXCEPT the carriage return key. When the user hits 'RETURN' (OK, 'ENTER' if you ain't old-school like me, an' I'm all about old-school) the Event handler takes over .... it creates an item in the same manner as before, then clears the Text box in case the user wants to enter another item.

variable 'i' is important because the new item may be entered either at the end of the list if comboBox1->Sorted is false, or Who-knows-where if comboBox1->Sorted is true. Variable 'i' gets the index from the Add method and is used in the statement following the Add.

Again, this isn't necessarily the best solution, it's just ** A ** solution; one among many. But it may give you some insight into the relationship between the various controls, properties and methods.

Remember, the HELP section of the IDE is your best friend. Once one becomes acclimated to its somewhat terse descriptions, it becomes a huge source of information.

hope that helps.

Best wishes,

bill

Alicat
June 2nd, 2006, 09:34 AM
Thx Thermosight, I not ignoring you. give me a chance to digest this and I'll post a message back as soon as I can. I'm currently travelling and just hopped on someone's network to post this message.

Alicat
June 2nd, 2006, 01:52 PM
OK I gave that a try but it won't compile. Here's the error messages. Do I need to change a preference setting somewhere? I using C++ Visual Studio Express


1>c:\documents and setting\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(97) : error C3921: Use of S-prefixed strings requires /clr:oldSyntax command line option
1> When compiling with /clr, an implicit conversion exists from string literal type to System::String^. If necessary to avoid ambiguity, cast to System::String^
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(98) : error C3921: Use of S-prefixed strings requires /clr:oldSyntax command line option
1> When compiling with /clr, an implicit conversion exists from string literal type to System::String^. If necessary to avoid ambiguity, cast to System::String^
1>.\testComboBox.cpp(6) : error C2143: syntax error : missing ';' before 'using'
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(25) : error C2059: syntax error : 'inline function header'
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(25) : error C2143: syntax error : missing ';' before '{'
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(25) : error C2447: '{' : missing function header (old-style formal list?)
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(90) : error C3699: '*' : cannot use this indirection on type 'System::Windows::Forms::ComboBox::ObjectCollection'
1> compiler replacing '*' with '^' to continue parsing
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(91) : error C3699: '*' : cannot use this indirection on type 'System::Object'
1> compiler replacing '*' with '^' to continue parsing
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(95) : error C2750: 'System::Object' : cannot use 'new' on the reference type; use 'gcnew' instead
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(95) : error C2440: '=' : cannot convert from 'System::Object *' to 'System::Object ^'
1> No user-defined-conversion operator available, or
1> Cannot convert an unmanaged type to a managed type
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(97) : error C2039: 'set_Item' : is not a member of 'System::Windows::Forms::ComboBox::ObjectCollection'
1> c:\windows\microsoft.net\framework\v2.0.50727\system.windows.forms.dll : see declaration of 'System::Windows::Forms::ComboBox::ObjectCollection'
1>c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(97) : error C3699: '*' : cannot use this indirection on type 'System::Object'
1> compiler replacing '*' with '^' to continue parsing
1>.\testComboBox.cpp(6) : error C2059: syntax error : ';'
1>.\testComboBox.cpp(20) : fatal error C1075: end of file found before the left brace '{' at 'c:\documents and settings\LD\my documents\visual studio 2005\projects\testcombobox\testcombobox\Form1.h(4)' was matched
1>Build log was saved at "file://c:\Documents and Settings\LD\My Documents\Visual Studio 2005\Projects\testComboBox\testComboBox\Debug\BuildLog.htm"
1>testComboBox - 14 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

ThermoSight
June 2nd, 2006, 05:00 PM
Whoooeeee! That's some block of errors, AliCat!

OK, Clearly we're working with different compilers/environments here because the code compiles and executes on my system. I'm using MS C++ .NET - Visual Studio 2003.

I note that the first thing it complains about is the "S" preceding the strings, and refers to "old style" .... another bad sign suggesting an environment or compiler incompatibility.

So, if I were seated at your machine, my next step would be to focus on that first error message and ignore the hundreds of others for now ... clear up the problem with the first bad line and many others are likely to be resolved simultaneously.

It doesn't like the S"...."; OK, how do you enter Strings in your code? Do you just use "..."? Whatever you do to declare a string in your compiler/environment, adapt the new code to do the same.

Oh! There it is right there .... it's TELLING me it's Visual Studio 2005. Regrettably, I'm too poor to pay $500 for what is to me, a toy, so I'm mired in VS 2003. The code I have provided is basically correct, we just need to adapt it to VS2005 standards.

Hmmm, just a question here .... are you writing/compiling in C++ ? As I look thru the list of errors, I'm seeing things that mystify me. Just wanna make sure we're on the same page here .... you writing in C++ .NET?

For instance, one of the errors directs you to use gcnew instead of new .... my environment doesn't recognize gcnew ... has no idea what that is. Further, another error says that set_Item is not a member of ObjectCollection .... my environment says it sure is (although, being a property, one doesn't need to use the set_ portion - one could simply use the expression "Item = xxx" - perhaps your system REQUIRES that usage).

In short, we're screwed .... we need someone to help us with the translation from one milieu (VS2003) to the more recent version (VS2005).

Or we can do it ourselves .... I mean, you couldn't have entered that many lines of code ... let's make'em work, one statement at a time beginning with the first error. How do you declare strings in your environment? Make what I supplied look like that. Then compile the code and see that we've moved on to the next error .... Hey, we got all weekend, how long can this take? (how badly do you want your app to work?).

Perhaps there's some Good Samaritan wandering by that's familiar with both VS2003 AND VS 2005. Help!

Best wishes,

bill

ThermoSight
June 2nd, 2006, 05:13 PM
Can't help but wonder .... are you compiling with C# ? I'm mystified that the compiler apparently wants to see a ^ rather than a * (if I haven't completely misunderstood the compiler message).

So let's do this ...
1) convert the String format I use to whatever format you use to declare Strings.
2) use gcnew in lieu of new

then let's compile that and see where we are. The other errors errors may be resolved once we clear these away.

Perhaps you could provide your code for inspection.

Let's MAKE this work!

bill

Alicat
June 2nd, 2006, 05:31 PM
;) I know it seems like I'm a coding genius. The truth is I'm completely oblivious to the actual C++ code that is generated by C++ Visual Studio Express 2005 which BTW is free for download from Microsoft (Just doesn't have the MFC class support. For that you have to pay).

I and just learning to program in Windows environment and trying to skip ahead of myself in a way. Visual Studios visual way of coded is great but it only takes you so far. Some day I'll understand and be hopefully be able to talk back to you intelligably.

ThermoSight
June 2nd, 2006, 06:23 PM
OK, well look, when you have some free time, let's continue with this.

The code I supplied WORKS in MS C++ VS2003. It compiles and executes just fine. My guess is that all we hafta do is adapt it to comply with VS2005 convention.

You may or may not have the time for that right now, but when ya do, change the strings to whatever format it wants to see and let's remove these errors and get your app on its way, one error at a time (although it's been my experience that resolving one problem frequently clears up several error messages, so things aren't as grim as you might think).

Let's MAKE this **** thing work!

Best wishes,

bill

Alicat
June 2nd, 2006, 09:19 PM
Hi Bill
I know this may be too much to ask but I sem to be having trouble trying to find help on following your code. As I said I'm new to C++ window programming and trying to get a quick under standing.

Could you possibly give me a line by line explaination of what your code is doing.

This is what I have difficulty grasping the concept of:

ComboBox::ObjectCollection *pCollection = comboBox1->Items;
What oes this line mean

Object *pItem;
And this one

if (e->KeyCode == Keys::Return) {
pItem = new Object();
i = pCollection->Add(pItem);
pCollection->set_Item(i, dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text)));
comboBox1->Text = S"";
}

Is this code basically reading every keystroke and adding it to a string?

ThermoSight
June 2nd, 2006, 10:22 PM
"Is this code basically reading every keystroke and adding it to a string?"


You got it, Toyota!

it's looking at each character but the control (the ComboBox) is actually building the string as the user types ... the handler ain't doin' that.

It's reading every character the user types but ignoring all EXCEPT the "ENTER" Key. When that one is hit, the handler knows the user has finished the entry and it's time to do something with what he or she has entered. In this case, it enters the string just typed into the selection list. After the user hits "ENTER", clicking on the arrow in the comboBox will reveal the string just entered as a candidate for selection in the drop down menu.

We should probably also add a statement to ignore empty strings (for instance if the user is in a playful mode, he/she might hit "ENTER" without entering a string. We wanna ignore that case.).

So, line by line....... (hang on while I pick up the code in another window ....)



// I declare that I have a pointer to a comboBox1's Object collection.
// The pointer is called 'pCollection'. Note that ComboBox selection
// candidates are grouped together in an Object Collection. In this
// statement, I assign the address of comboBox1's Collection to
// 'pCollection'.
//
ComboBox::ObjectCollection *pCollection = comboBox1->Items;


//I declare that I have an Object * that will point to an individual
// Object rather than an entire collection of Objects.
//
Object *pItem;


// IF AND ONLY IF the key just struck is the "ENTER" key, Proceed
// (otherwise, ignore this process and return immediately to see the
// the next char ... this'll happen so fast the user will never know)
//
if (e->KeyCode == Keys::Return) {


// Create a NEW (guess you wanna use 'gcnew' here) Object, and set
// the Object pointer, pItem, to point to that new Object.
//
pItem = new Object();


// Add that new and UNINITIALIZED Object to the collection. Add() will add
// the Object to the end of the existing list if comboBox1 has been
// configured with 'SORT' false. Or, if 'SORT' is true, it will be entered in
// alphabetic order (I guess). It remains UNINITIALIZED (i.e. user string
// has not been entered yet).
// Note that Add() returns the item's index to us as 'i'
//
i = pCollection->Add(pItem);



// Concatenate the string "Item " with the string the user entered.
// Then, because the result is a String, but the Collection works
// with Objects, DYNAMIC_CAST the String pointer to an Object pointer
// and set the TEXT property of the ith item in the Collection to that
// Concatenated result.
//
// So if the user had typed "AliCat" (RETURN), opening the dropdown
// List would show the last selection candidate as reading "Item AliCat".
//
// Where did 'i' come from? See 'i' in the preceding Add() statement.
// remember I said we can't be sure where Add() will place it? Well, Add
// informs us where he's placed it by returning 'i'.
//
pCollection->set_Item(i, dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text)));


// Now that we've completed the task, clear the user's entry in case
// he / she wishes to add another entry.
//
comboBox1->Text = S"";





As I mentioned earlier ... I should have added a line which tests for the presence of an empty string in case the user is toying with us. If it's an empty string, we want to avoid performing the process ... just return immediately.


Also, you will note that in the earlier example, I do a very similar procedure, but use "hardwired" items rather than the user text entry, when populating the comboBox initially. That is performed while the form is being constructed. As you can see below ...

Form1(void) // the FORM is being constructed.
{
InitializeComponent();

// populate box with "hard-wired" default values
onTheFly(CLEAR_EXISTING);
}



This code works in C++/VS2003.

You can read about the ComboBox in great detail in the HELP section of your IDE. Just type in ComboBox and you should see "ComboBox class" appear, and a whole buncha comboBox-related subtopics.

Bon chance!

bill

ThermoSight
June 2nd, 2006, 10:36 PM
Whew! Boy, I thought my line-byline explanation was lost .... but it was just on a different page.

While writing the line-by-line explanation in the message preceding this, it occurred to me that a slight alternative would be an improvement.


I'll take the liberty of not doing the line by line here 'cuz almost everything's the same EXCEPT as noted ....

if (e->KeyCode == Keys::Return) {

if (comboBox1->Text->Length > 0) { // test String length > 0 ... NEW

pItem = new Object();


// Do the item initialization HERE so that if the box is configured to
// SORT items as entered, it'll work properly.
//
pItem = dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text)); // .... NEW


// Note that I've retained the 'i' here but it's merely a formality.
// Because we no longer need the set_Item (in the next line)
// we won't need 'i'. But leave it in for giggles.
//
i = pCollection->Add(pItem);


// and we comment the "set_Item" out cuz we've done the assignment
// in the NEW step above. this eliminates one error, anyway.
// pCollection->set_Item(i, dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text))); // ... NEW

comboBox1->Text = S"";

}// end if string length > zero // .... NEW
}// end if KeyCode == ENTER key.

Alicat
June 3rd, 2006, 03:12 AM
Cool!!! Thanks for the explaination. Helped a lot. I need to get a grasp on the * and ^ symbols though and how to use pointers.

This code seems to work for me though. I now have the value stored in int i

int i;
if (e->KeyCode == Keys::Return) {
i = Convert::ToInt32(comboBox1->Text::get());
}

Now how can I make this happen when pressing either return, tab or mouse on the next combobox without writing a seperate event handler for each key?

Alicat
June 3rd, 2006, 03:28 AM
I should have mentioned that I don't realy care about adding the value to the list. I'm more interested in it displaying as the default selection displayed on the screen. I then will use that integer value to set a progress bar for display purposes. Later this value will be saved to a preference file or sent through a serial port.

ThermoSight
June 3rd, 2006, 12:20 PM
Good Morning, AliCat.

I should have mentioned that if you were going to add the items to the list, that the line

pItem = new Object();

is not necessary. You can combine that line with the next line as follows

pItem = dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text));



Regarding making something appear as a default display in the comboBox, that's Easy (note the capital 'E'): the ComboBox has a 'Text' property. If you wanted to display a string there, you simply place the string in the Text property as follows ....

comboBox1->Text = S"this ComboBox courtesy of AliCat";

Note that in the little routine shown earlier, I clear the display box in the same manner after accepting the user's input.


bill

ThermoSight
June 3rd, 2006, 12:56 PM
Note that in the previous message, I failed to address your question regarding the tab key.

I thought that'd be simple .... that one would add a test for e->KeyData == KEYs::Tab, but that wasn't as easy as I thought. It seems that the normal Tabbing function has precedence over the event handler ...... when I hit Tab key, it tabbed to the next control without giving control to the handler.

Not knowing how to intercept the tabbing function, I bit the bullet and installed a separate event handler predicated on Focus - Leaving, as follows

private: System::Void comboBox1_Leave(System::Object * sender, System::EventArgs * e)


Code's pretty much the same, just a different handler executing identical code.


Now, I like to keep the handlers as slim 'n trim as possible, and because both handlers are doing essentially the same thing, I would move the common code off to a separate function and let each handler call that common routine, rather than replicating the same code two or three times to accommodate the different handlers.

Best wishes,

bill

Alicat
June 3rd, 2006, 09:53 PM
Thx, I think you're right about the focus leave event. I'll look into seeing how managed code handles that. I have it working pretty much except for a few more things. Is there a way to limit input to numbers. When I accidently enter junk the program errors.

Secondly, I still can't seem to find the best place to put the initialization code to put the default values into the combobox text field in the C++ managed code environment. What is the contractor code area. I tried putting it there but the Comboboxes doesn't appear to have been created yet.

Here's the code for app.cpp

int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);

// Create the main window and run it
Application::Run(gcnew Form1());
return 0;

}

Would this be where I put it?

ThermoSight
June 3rd, 2006, 11:34 PM
OK! Sounds like you've got a handle on this thing ... great!

Now regarding placement of the call for default values, as I may have mentioned earlier, I'd create a small function which has the requisite code to initialize the comboBox.

Then, I'd place a single call to that initialization function in the Form1 constructor. Functionally, that would be just AFTER the point you suggested in your last note.

Physically, however, that call would be found in the Form1 constructor, i.e. page Form1.h, and would appear as follows .....

// declare and define the class definition .....
//
public __gc class Form1 : public System::Windows::Forms::Form
{
public:
/// define the Form1 Constructor.
Form1(void)
{
InitializeComponent();
onTheFly();// <======INITIALIZE COMBOBOX
}
.......
........
........
}; // end Form1 class definition.


Notice that it is placed AFTER function InitializeComponent().




Regarding the digits-only restriction .... I can't think of a way to prevent a non-digit from being entered, but you can enforce that restriction, after the fact, in your event handler where each char is checked to see if it's the ENTER key. You might consider adding an ELSE clause to that IF.

In the ELSE clause (ELSE it is not the ENTER key) check the character to be certain it falls in the range of 0x30 to 0x39 (ASCII digits '0' thru '9'). If it does, fine. If not, erase it (remove it from the comboBox1->Text) and, optionally, put up a message reminding the user of the digits-only restriction,

for instance .....

private: System::Void comboBox1_KeyUp(System::Object * sender, System::Windows::Forms::KeyEventArgs * e)
{
if ( (e->KeyCode == Keys::Return) ) {
if (comboBox1->Text->Length > 0) {
comboBox1->Items->Add(dynamic_cast <Object *> (String::Concat(S"Item ", comboBox1->Text)));
comboBox1->Text = S"";
}else { /*this block is empty*/ }// avoid confusing the compiler with the next 'ELSE'. <==== SEE THIS !


// this is the part you're interested in. If it isn't a digit,
// remove the last char typed (and maybe raise an error message)
//
}else {// else it wasn't the 'ENTER' key!
if ((e->KeyValue < DIGIT_0) ||
(e->KeyValue > DIGIT_9)) {
comboBox1->Text = comboBox1->Text->Remove( comboBox1->Text->Length-1, 1);
}
}// end else it wasn't the 'ENTER' key.
}



Of course, you can replace the two part test (< DIGIT_0 || > DIGIT_9)
with the IsDigit() method. That'd probably be the preferred form.

Best wishes,

bill

Alicat
June 4th, 2006, 12:52 AM
Thx for your help. I'll give it a try. I'll be away for about a week. Going to Poland on business. Not sure if they have adaquate internet services there. Will post a note here if I'm sucessful

Larry

ThermoSight
June 4th, 2006, 01:07 AM
Hope you have a profitable journey.

See ya.

bill