473,395 Members | 1,972 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Counting clicks in a listbox....

Have a counter in place, however, it isn't correctly counting clicks in a listbox...help....
Jul 22 '07 #1
3 1478
Killer42
8,435 Expert 8TB
Have a counter in place, however, it isn't correctly counting clicks in a listbox...help....
I think you need to provide us more details about the situation. For instance, what version of VB are you using? And what have you done so far, which isn't working?

One possible problem is variable "scope" (check your VB doco). If you create a variable in the Click event procedure, for example, then it only exists until you exit from that procedure. Next time you execute the procedure, it will be created from scratch again, and won't have the value you left there before.

That's just one possibility, of course. Without knowing more about what you're doing, I can't say whether it actually is the problem.
Jul 22 '07 #2
I am using VB 5....here is the code I have in place so far...counter isn't showing correct number of times someone has selected something in the ListBox via a msgbox...the number does change, however, it is never correct.

Expand|Select|Wrap|Line Numbers
  1. Option Explicit On
  2. Option Strict On
  3. Imports System.Data
  4. Public Class Form1
  5.     Dim dt As New DataTable()   'First step in filling data table with data from database.
  6.     Dim dt1 As New DataTable()
  7.     Dim counter As Integer = 0
  8.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e _
  9.         As System.EventArgs) Handles Button2.Click
  10.         MsgBox(counter & " cities were looked at.", 0, "City Count")
  11.         End
  12.     End Sub
  13.     Private Sub Form1_Load(ByVal sender As Object, ByVal e _
  14.     As System.EventArgs) Handles Me.Load
  15.         'Open a connection to the database
  16.         Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  17.         "Data Source=LOSSPROPERTIES.MDB"
  18.         Dim sqlStr As String = "SELECT * FROM Properties"
  19.         Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
  20.         dataAdapter.Fill(dt)
  21.         dataAdapter.Dispose()
  22.         ListBox2.DataSource = dt
  23.         ListBox2.DisplayMember = "property"
  24.         TextBox1.Clear()
  25.         TextBox2.Clear()
  26.         TextBox3.Clear()
  27.     End Sub
  28.     Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, _
  29.     ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
  30.        'Load data from the Countries table into second data table
  31.         Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
  32.                 "Data Source=LOSSPROPERTIES.MDB"
  33.         Dim sqlStr As String = "SELECT * FROM Counties"
  34.         Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
  35.         dataAdapter.Fill(dt1)
  36.         dataAdapter.Dispose()
  37.         TextBox1.Text = CStr(dt.Rows(ListBox2.SelectedIndex)("sales"))
  38.         TextBox1.Text = TextBox1.Text
  39.         TextBox1.Text = FormatNumber(TextBox1.Text, 2)
  40.         For i As Integer = 0 To dt.Rows.Count - 1
  41.             Do While CStr(dt1.Rows(counter)("County")) <> _
  42.             CStr(dt.Rows(ListBox2.SelectedIndex)("county"))
  43.                 counter = counter + 1
  44.             Loop
  45.             TextBox2.Text = CStr(dt1.Rows(counter)("sales"))
  46.             TextBox2.Text = TextBox2.Text
  47.             TextBox2.Text = FormatNumber(TextBox2.Text, 2)
  48.             Dim popPercent As Double
  49.             Dim devPop, ctyPop As Double
  50.             devPop = CDbl(TextBox1.Text)
  51.             ctyPop = CDbl(TextBox2.Text)
  52.             popPercent = CDbl(devPop / ctyPop)
  53.             TextBox3.Text = ("The percentage of the population of " & _
  54.                                 CStr(dt.Rows(ListBox2.SelectedIndex)("properties")) & " to the population of " & _
  55.                                 CStr(dt.Rows(ListBox2.SelectedIndex)("county")) & " is: " & FormatPercent(popPercent))
  56.         Next
  57.     End Sub
  58. End Class
Jul 22 '07 #3
Killer42
8,435 Expert 8TB
I can guarantee you, that is not VB5 code. Presumably you must mean VB 2005.

Anyway, I'll have a look through it as soon as I have a chance.
Jul 22 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Antoni | last post by:
Hello, I was trying to write a simple shopping example, which allows the user to select two poducts and we kept a counter of each item. In this code, the sessions are created, but the files...
17
by: amber | last post by:
Hello. Can someone tell me what I may be doing wrong here? I'm using the code (lboxRP is a listbox): Dim newRPindex As Integer newRPindex = Me.lboxRP.FindString(RP)...
14
by: Keith | last post by:
Just looking for some ideas here. What is the best way to count how many times an image has been clicked? I have an image gallery on a site and want to count how many times each image has been...
13
by: Regnab | last post by:
The problem I'm having is the rsa.RecordCount always returns a '1' when I know there should be about 1600 records returned. I read somewhere that one should use '.MoveLast' before recordcount but...
8
by: Oddball | last post by:
Ok - I have a ListBox control and I'm ready to write my own DrawItem event handler. What I want to draw as the item is another control. I have created a user control that I would like to list in...
6
by: Chris Leuty | last post by:
I am populating a multiselect Listbox from a dataset, with the content of the listbox filled by one table, and the selections determined from another table. So far, I have been keeping the dataset...
11
by: Al | last post by:
Can anyone tell me if there's a way to return the amount of times a button is "clicked" I need to change text in a text box and that text changes each time the button is clicked-up to 6 times...
4
by: aaronfude | last post by:
Hi, Please consider the following class (it's not really my class, but it's a good example for my question): class Vector { int myN; double *myX; Vector(int n) : myN(n), myX(new double) { }...
3
by: Ali Chambers | last post by:
Hi, I have created a listbox called "dtlist1" on my VB.NET form. I call a procedure as follows: Private Sub openfile(flname As String) dtlist1.Items.Clear() etc..
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.