|
-
Feb 13th, 2008, 01:07 PM
#1
Thread Starter
Addicted Member
Arrrrgh...List Box Irritation
Why when I start my application does the list box come up with the default first value clearly visible, black text on a white back ground.
But when I try to convert this clearly visible string to a single value, it errors out ? But when I click on the list box, it turns blue back ground, and then the csng() func works fine ?
Is this some focus related issue ? Why does it work fine with a text box ? Why don't I have to click a text box ?
I do want the user to have a limited choices on input, but I don't want to use a text box...Arrrrgh !
-
Feb 13th, 2008, 01:13 PM
#2
Re: Arrrrgh...List Box Irritation
I wish you could explain that a little better for I am unable to get a clear picture in my mind what your are talking about. Or, maybe it's just me, I don't know.
-
Feb 13th, 2008, 01:19 PM
#3
Re: Arrrrgh...List Box Irritation
No, it isn't just you. I have no idea what the issue is either.
 Originally Posted by prginocx
Why when I start my application does the list box come up with the default first value clearly visible
I don't know, does it?.
 Originally Posted by prginocx
But when I try to convert this clearly visible string to a single value, it errors out ?
What is the error? How are you trying to convert it?.
 Originally Posted by prginocx
But when I click on the list box, it turns blue back ground, and then the csng() func works fine ?
Well, the blue line indicates that something has been selected. Perhaps you are trying to convert something that doesn't exist when you try to convert it..
 Originally Posted by prginocx
Is this some focus related issue ? Why does it work fine with a text box ? Why don't I have to click a text box ?
Without more details I can't answer that question.
-
Feb 13th, 2008, 02:48 PM
#4
Re: Arrrrgh...List Box Irritation
"Why when I start my application does the list box come up with the default first value clearly visible, black text on a white back ground.
But when I try to convert this clearly visible string to a single value, it errors out ? ..."
----------------
Tell us what the "clearly visible string" and "default first value" is/are and we might be able to answer your question.
-
Feb 13th, 2008, 02:52 PM
#5
Re: Arrrrgh...List Box Irritation
does the list box come up with the default first value clearly visible
No. By default an item in the list is not selected even if the control has the input focus. The ListBox.Text property is empty and the ListBox.ListIndex property is -1. Note: if the MultiSelect property is set to 1 or 2, there is a default item.
After loading the ListBox set its ListIndex property to 0 to select the first item. The Text property will then be automatically populated with that value. Or use something like
If List1.ListIndex <> -1 Then
Debug.print CSng(List1.Text)
End If
-
Feb 13th, 2008, 02:57 PM
#6
Thread Starter
Addicted Member
Re: Arrrrgh...List Box Irritation
Ok, let me try to elaborate:
1. execute the application, main form opens.
2. On that form is a list box, with the 30 clearly visible ( first item in list )
3. Press cmd button, procedure tries to get that 30 string visible with:
4. intSpcSize = cint(lstbxSpectrumsize.text)
If instead of pressing the cmd button in step 3 above, you click on the text window of the list box, it turns blue background. Then press the cmd button for step 3 and everything works fine.
If you don't, Cint gives an error for type mismatch. I feel foolish asking this...Why does no one else have these problems ? I used to be a C programmer, am I cursed because of that ?
-
Feb 13th, 2008, 03:06 PM
#7
Re: Arrrrgh...List Box Irritation
Like I said, the Items may be visible but one of them must first be "Selected" before the Text property is populated.
-
Feb 13th, 2008, 03:28 PM
#8
Re: Arrrrgh...List Box Irritation
Bruce is correct. The reason its giving you an error is because, you can't convert an empty string to an integer.. theres no equivalent..
chem
Last edited by MartinLiss; Feb 13th, 2008 at 07:45 PM.
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Feb 13th, 2008, 03:40 PM
#9
Thread Starter
Addicted Member
Re: Arrrrgh...List Box Irritation
I know why the Cint() gives an error, I just don't know why the dump list box doesn't put those values in when the form / program executes...Why show the user values that don't " programmatically exist ?
I thought about trying to zero in on the index value, instead of using cint(). Is there a way to trap the cint() error, then go to the listindex business ?
or at least have a message for the user saying something stupid like you must select ( blue background ) the list parameter first, etc....
-
Feb 13th, 2008, 04:00 PM
#10
Re: Arrrrgh...List Box Irritation
Oh man!
For the third time...
If you want a default item then select it in code using the ListIndex property.
Code:
If lstbxSpectrumsize.ListIndex = -1 Then
lstbxSpectrumsize.ListIndex = 0
End If
intSpcSize = cint(lstbxSpectrumsize.text)
Or even
intSpcSize = cint("0" & lstbxSpectrumsize.text)
How simple is that!
-
Feb 13th, 2008, 05:20 PM
#11
Thread Starter
Addicted Member
Re: Arrrrgh...List Box Irritation
Ok, using the lstbox.index seems the easiest way to avoid the Cint() of -1 problem.
It is still kind of annoying, though, because my box has up/down scroll controls, but again, when you scroll to the value you want, you must still select it ( blue background ), or else the program " sees " the prior value.
I think this issue is resolved, though...What I didn't understand is that when the form is first executed, the list index is -1, no matter what value is displayed to the user.
Call me crazy, but I think the value displayed should match the list index the program sees...or else display a blank box. That would clue the developer ( and the user) in that there is no " real " list value right now....
-
Feb 13th, 2008, 05:38 PM
#12
Re: Arrrrgh...List Box Irritation
 Originally Posted by prginocx
Ok, using the lstbox.index seems the easiest way to avoid the Cint() of -1 problem.
It is still kind of annoying, though, because my box has up/down scroll controls, but again, when you scroll to the value you want, you must still select it ( blue background ), or else the program " sees " the prior value.
I think this issue is resolved, though...What I didn't understand is that when the form is first executed, the list index is -1, no matter what value is displayed to the user.
Call me crazy, but I think the value displayed should match the list index the program sees...or else display a blank box. That would clue the developer ( and the user) in that there is no " real " list value right now....
How does that make sense?
A listbox is for a number of items. In your world.. if a listbox had 3 items with the names "John" "Amanda" and "Gary", then this:
Code:
MsgBox ListBox.Text
..would show "John Amanda Gary".
.. doesn't seem correct does it.. ?
chem
Last edited by MartinLiss; Feb 13th, 2008 at 07:46 PM.
Visual Studio 6, Visual Studio.NET 2005, MASM
-
Feb 13th, 2008, 06:04 PM
#13
Re: Arrrrgh...List Box Irritation
I think this issue is resolved, though...What I didn't understand is that when the form is first executed, the list index is -1, no matter what value is displayed to the user.
When form is first executed there is no value displayed to the user. The user sees everything in the listbox that is within the height of the listbox and no item is selected by default.
You cannot do this
intSpcSize = CInt(List1.Text)
if you have not selected which item in the listbox you want. List1.Text is null if you do not select an item and you will get the type mismatch.
All you have to do is to accept this as is.
-
Feb 13th, 2008, 06:41 PM
#14
Thread Starter
Addicted Member
Re: Arrrrgh...List Box Irritation
Ah, I see...My box is tiny, so only the one value shows..
You are saying if it was long I'd see that no value is selected...
BTW, the reason I'm using this box is I am scared to allow users to just put in any value they want, I've kinda learned not to trust them...
Is there some other better way to present a user with a choice of 20+ numerical values ?
-
Feb 13th, 2008, 07:47 PM
#15
Re: Arrrrgh...List Box Irritation
You are saying if it was long I'd see that no value is selected...
Exactly, but not long; high or taller.
And BTW....
Why when I start my application does the list box come up with the default first value clearly visible, black text on a white back ground.
It isn't a default first value visible it was the only value (or item) that was visible because you had the listbox narrow so only one item was showing.
But when I try to convert this clearly visible string to a single value, it errors out ? But when I click on the list box, it turns blue back ground, and then the csng() func works fine ?
That is called highlighted. It means Selected.
[B]Is this some focus related issue ? Why does it work fine with a text box ? Why don't I have to click a text box ?
It is not a focus issue it is a Selected issue
BTW, the reason I'm using this box is I am scared to allow users to just put in any value they want, I've kinda learned not to trust them...
When the listbox style is set to 0 users cannot put items in the listbox, it must be loaded either at design time or through program code.
Last edited by jmsrickland; Feb 13th, 2008 at 07:54 PM.
-
Feb 13th, 2008, 10:01 PM
#16
Re: Arrrrgh...List Box Irritation
 Originally Posted by prginocx
Ah, I see...My box is tiny, so only the one value shows..
You are saying if it was long I'd see that no value is selected...
BTW, the reason I'm using this box is I am scared to allow users to just put in any value they want, I've kinda learned not to trust them...
Is there some other better way to present a user with a choice of 20+ numerical values ?
Try using a combobox instead of a listbox rather than forcing listbox to perform what it wasn't designed for (square peg, round hole analogy).
Code:
Option Explicit
Private Sub cmdTest_Click()
If cboNums.ListIndex <= 0 Then 'if non-numeric text selected or no selection
MsgBox "Please select a value!"
Else
MsgBox CInt(cboNums.Text)
End If
End Sub
Private Sub Form_Load()
cboNums.AddItem "Select a value" 'test later that this isn't selected. Can be zero length string rather than message
cboNums.AddItem "1"
cboNums.AddItem "2"
cboNums.AddItem "3"
cboNums.AddItem "4"
cboNums.AddItem "5"
cboNums.ListIndex = 0
End Sub
Last edited by leinad31; Feb 13th, 2008 at 10:08 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|