Results 1 to 4 of 4

Thread: Any idea how to fix this?

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    Bracebridge
    Posts
    20

    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

  2. #2
    Frenzied Member <ABX's Avatar
    Join Date
    Jul 2002
    Location
    Canada eh...
    Posts
    1,622
    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:
    1. Private Enum Answers
    2.         [True] ' Brace's are required because of True being a keyword
    3.         [False]
    4.         MissingItem
    5.     End Enum
    6.  
    7.     Private Results(19) As Answers ' Personally, I would have used a collection
    8.     Private intIndex As Integer = 0 ' Hold place in Results()
    9.     Private blnMouseDown As Boolean ' to know if the mouse is down on a item
    10.  
    11.  
    12.     Private Sub lstScore_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles lstScore.KeyDown
    13.  
    14.         If blnMouseDown Then
    15.             'Exception to fix problem 3
    16.             Dim intItemIndex As Integer = lstScore.SelectedIndex
    17.  
    18.             Select Case e.KeyValue
    19.                 Case Keys.NumPad1
    20.                     'True
    21.                     lstScore.Items.Item(intItemIndex) = "[T]"
    22.                     Results(intItemIndex) = Answers.True
    23.                 Case Keys.NumPad2
    24.                     'False
    25.                     lstScore.Items.Item(intItemIndex) = "[F]"
    26.                     Results(intItemIndex) = Answers.False
    27.                 Case Keys.NumPad3
    28.                     'Missing Item
    29.                     lstScore.Items.Item(intItemIndex) = "Missing Item"
    30.                     Results(intItemIndex) = Answers.MissingItem
    31.                 Case Else
    32.                     'Invaild Key
    33.                     'NoChanged
    34.             End Select
    35.  
    36.             blnMouseDown = True
    37.             Exit Sub '
    38.         End If
    39.  
    40.         If intIndex > 19 Then ' this will fix problem 2
    41.             'We Shouldnt do anything because we dont want more than
    42.             '20 items... so we will exit the sub
    43.             MsgBox("Already have 20 items... bailing!")
    44.             Exit Sub
    45.         End If
    46.  
    47.         Select Case e.KeyValue
    48.             Case Keys.NumPad1
    49.                 'True
    50.                 lstScore.Items.Add("[T]")
    51.                 Results(intIndex) = Answers.True
    52.             Case Keys.NumPad2
    53.                 'False
    54.                 lstScore.Items.Add("[F]")
    55.                 Results(intIndex) = Answers.False
    56.             Case Keys.NumPad3
    57.                 'Missing Item
    58.                 lstScore.Items.Add("Missing Item")
    59.                 Results(intIndex) = Answers.MissingItem
    60.             Case Else
    61.                 'Invaild Key Pressed....
    62.                 Exit Sub ' We dont want to increase
    63.                 '    intIndex because we didnt do anything
    64.         End Select
    65.  
    66.         intIndex += 1
    67.  
    68.         'Also set the listbox so it looks at the last item
    69.  
    70.         lstScore.SelectedIndex = lstScore.Items.Count - 1
    71.     End Sub
    72.  
    73.     Private Sub lstScore_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lstScore.MouseDown
    74.         If Not lstScore.SelectedIndex = -1 Then
    75.             'An Item Is Selected
    76.             If e.Button = MouseButtons.Left Then
    77.                 blnMouseDown = True
    78.             End If
    79.         End If
    80.     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

  3. #3
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Can you elaborate upon Problem 1? I didn't quite understand what you're trying to get at there.

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2004
    Location
    Bracebridge
    Posts
    20

    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
  •  



Click Here to Expand Forum to Full Width