Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB Listbox, loops, ifs?

Options
  • 20-11-2002 12:05pm
    #1
    Registered Users Posts: 446 ✭✭


    Help!
    I'm going to explain exactly what I'm trying to do:

    Imagine a form with a list box with "Cat, Dog" I then have a button at the bottom that will put up a message box that tells the person how many people have clicked on the cat...and dog I.e. "3 people chose Dog".

    Ive tried loops and If statement but they're not doing anything.

    Also I'm setting the number to 0 and so it just keeps reseting, but when I dont set the number nothing happens


    Hope you can Help
    Kate


Comments

  • Registered Users Posts: 2,781 ✭✭✭amen


    right this does what you want.
    Its a bit crude but it works

    save the text below into a file form1.frm

    Open a new vbproject and add the form1.frm
    and way you go

    What course are you studying, year and where ?


    VERSION 5.00
    Begin VB.Form Form1
    Caption = "Form1"
    ClientHeight = 3195
    ClientLeft = 60
    ClientTop = 345
    ClientWidth = 4680
    LinkTopic = "Form1"
    ScaleHeight = 3195
    ScaleWidth = 4680
    StartUpPosition = 3 'Windows Default
    Begin VB.CommandButton cmdReset
    Caption = "Reset"
    Height = 465
    Left = 2700
    TabIndex = 2
    Top = 1560
    Width = 1485
    End
    Begin VB.CommandButton cmdDisplayClicks
    Caption = "Display Clicks"
    Height = 435
    Left = 420
    TabIndex = 1
    Top = 1530
    Width = 2025
    End
    Begin VB.ListBox List1
    Height = 645
    Left = 240
    TabIndex = 0
    Top = 240
    Width = 3045
    End
    End
    Attribute VB_Name = "Form1"
    Attribute VB_GlobalNameSpace = False
    Attribute VB_Creatable = False
    Attribute VB_PredeclaredId = True
    Attribute VB_Exposed = False
    Option Explicit

    Public iNumCatClicks As Integer
    Public iNumDogClicks As Integer
    Public iNumHorseClicks As Integer

    Private Sub cmdDisplayClicks_Click()
    MsgBox "Number Of Cats: " & iNumCatClicks & vbNewLine & "Number Of Dogs: " & iNumDogClicks & vbNewLine & "Number Of Horses: " & iNumHorseClicks
    End Sub

    Private Sub cmdReset_Click()
    'init variables
    iNumCatClicks = 0
    iNumDogClicks = 0
    iNumHorseClicks = 0
    End Sub

    Private Sub Form_Load()
    'init variables
    iNumCatClicks = 0
    iNumDogClicks = 0
    iNumHorseClicks = 0
    'set up list box
    List1.AddItem "Cat"
    List1.AddItem "Dog"
    List1.AddItem "Horse"
    End Sub



    Private Sub List1_DblClick()
    Select Case List1.ListIndex
    Case 0 'cat
    iNumCatClicks = iNumCatClicks + 1
    Case 1 'dog
    iNumDogClicks = iNumDogClicks + 1
    Case 2 'horse
    iNumHorseClicks = iNumHorseClicks + 1
    End Select
    End Sub


  • Closed Accounts Posts: 9,314 ✭✭✭Talliesin


    If you're going to do someone's homework for them could you at least explain the code so that they might actually learn something from it.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Post some code Kegan5 - it always helps to post your code so people can point out whats wrong and make suggestions.


Advertisement