|
-
Jun 26th, 2004, 09:45 PM
#1
Thread Starter
Junior Member
Any idea how to fix this?
OK - I've got a listbox that displays "True", "False", or "Missing Item" to numberpad keys 1, 2, and 3. I'm trying to make a scoring utility. I want to have 20 items. I've got three problems with the following code, and I don't know how to fix them. Can anyone help?
Problem 1: The variables Items(X) don't seem to contain the values I'm trying to assign to them.
Problem 2: I can enter more than 20 items.
Problem 3: I'd like to edit the items by scolling up with the cursor keys and/or clicking on it, then entering the correct answer.
Here's the code:
Public Item(19) as char 'Each variable to hold a T, F, or space
Dim x as integer 'This will act as a counter
Private Sub lstViewScore_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstViewScore.KeyDown
For X = 1 To 20
If e.KeyValue = Keys.NumPad1 Then
lstViewScore.Items.Add("[T]")
Item(X) = "T"
ElseIf e.KeyValue = Keys.NumPad2 Then
lstViewScore.Items.Add(" [F]")
Item(X) = "F"
ElseIf e.KeyValue = Keys.NumPad3 Then
lstViewScore.Items.Add("Missing Item")
Item(X) = " "
End If
Next
lstViewScore.SelectedIndex = lstViewScore.Items.Count - 1
End Sub
This will probably be easy for someone, but it's driving me up a wall!
Ian
-
Jun 27th, 2004, 06:47 PM
#2
It seems to me like you are trying make some kind of test application or an application that holds answers....
Problem 1:
When the keyDown Event fires... you loop through every ordinal and set it to the key that is being pressed... (not only that but you are using a trying to assign values from 1-20 but you should be assigning values from 0-19)
Problem 2:
Related to the for... next that was causing problem 1
you need to check to see if you have already added 20 item and then bail if you have...
Problem 3:
This is an interesting problem... to keep by example simple i will implement it so that if you click on the item and press 1-3 on the number pad it will change.... (could also be done will keys but I am going to keep it simple)
As of right now... im too lazy to clean up the KeyDown code so there is only 1 Select Case but what i posted works...
Also Attaching working VS 2003 project... if you want to see how i did it.
VB Code:
Private Enum Answers
[True] ' Brace's are required because of True being a keyword
[False]
MissingItem
End Enum
Private Results(19) As Answers ' Personally, I would have used a collection
Private intIndex As Integer = 0 ' Hold place in Results()
Private blnMouseDown As Boolean ' to know if the mouse is down on a item
Private Sub lstScore_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstScore.KeyDown
If blnMouseDown Then
'Exception to fix problem 3
Dim intItemIndex As Integer = lstScore.SelectedIndex
Select Case e.KeyValue
Case Keys.NumPad1
'True
lstScore.Items.Item(intItemIndex) = "[T]"
Results(intItemIndex) = Answers.True
Case Keys.NumPad2
'False
lstScore.Items.Item(intItemIndex) = "[F]"
Results(intItemIndex) = Answers.False
Case Keys.NumPad3
'Missing Item
lstScore.Items.Item(intItemIndex) = "Missing Item"
Results(intItemIndex) = Answers.MissingItem
Case Else
'Invaild Key
'NoChanged
End Select
blnMouseDown = True
Exit Sub '
End If
If intIndex > 19 Then ' this will fix problem 2
'We Shouldnt do anything because we dont want more than
'20 items... so we will exit the sub
MsgBox("Already have 20 items... bailing!")
Exit Sub
End If
Select Case e.KeyValue
Case Keys.NumPad1
'True
lstScore.Items.Add("[T]")
Results(intIndex) = Answers.True
Case Keys.NumPad2
'False
lstScore.Items.Add("[F]")
Results(intIndex) = Answers.False
Case Keys.NumPad3
'Missing Item
lstScore.Items.Add("Missing Item")
Results(intIndex) = Answers.MissingItem
Case Else
'Invaild Key Pressed....
Exit Sub ' We dont want to increase
' intIndex because we didnt do anything
End Select
intIndex += 1
'Also set the listbox so it looks at the last item
lstScore.SelectedIndex = lstScore.Items.Count - 1
End Sub
Private Sub lstScore_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstScore.MouseDown
If Not lstScore.SelectedIndex = -1 Then
'An Item Is Selected
If e.Button = MouseButtons.Left Then
blnMouseDown = True
End If
End If
End Sub
Tips:
- Google is your friend! Search before posting!
- Name your thread appropriately... "I Need Help" doesn't cut it!
- Always post your code!!!! We can't read your mind!!! (well, at least most of us!)
- Allways Include the Name and Line of the Exception (if one is occuring!)
- If it is relevant state the version of Visual Studio/.Net Framwork you are using (2002/2003/2005)
If you think I was helpful, rate my post  IRC Contact: Rizon/xous ChakraNET/xous Freenode/xous
-
Jun 28th, 2004, 12:15 AM
#3
Can you elaborate upon Problem 1? I didn't quite understand what you're trying to get at there.
-
Jun 28th, 2004, 07:00 PM
#4
Thread Starter
Junior Member
It's OK!
It's OK - I think it was an indexing problem. I couldn't get my code to assign a "T", "F", or "" to Item(0) to Item(19).
Ian
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
|