How to Select a Specific Bar on a 2D Graph with the Mouse
ID: Q123840
|
The information in this article applies to:
-
Microsoft Visual Basic Professional Edition for Windows, version 3.0
SUMMARY
This article explains how to detect which one of the Slices or Bars on a 2D
Bar Graph was selected with the mouse.
MORE INFORMATION
The graph control does not have a built-in method that you can use to
discover which bar or slice of a 2D Bar Graph your mouse pointer is
currently over. However, the graph control's ColorData property does
correspond to Visual Basic's QBColor() function. Therefore, if you display
the Graph in a picture box by assigning the Graph's picture property to a
picture box, you can use the picture box's Point(x,y) method to compare the
color of the bar at the position of your mouse. You can use this method to
differentiate up to 14 different points.
Step-by-Step Example
- Start a new project in Visual Basic (ALT, F, N). Form1 is created by
default.
- Add a Label and Graph control. Then add a Picture control on top of the
graph control; give both controls identical Top, Left, Width, and Height
property values. Set the GraphType to Pie or Bar chart.
- Add the following code to the Form_Load event:
Sub Form_Load ()
Picture1.Move Graph1.Left, Graph1.Top, Graph1.Width, Graph1.Height
Graph1.Visible = False
' Set the color to between 1 and the number of slices or Bars you
' will have (up to 14).
For I = 1 to Graph1.NumPoints
Graph1.ThisPoint = I
Graph1.ColorData = I
Next I
Graph1.DrawMode = 2 'Draw the graph
Picture1.Picture = Graph1.Picture 'Display in picture box
End Sub
- Add the following code to the Picture1_MouseDown event:
' Place the following two lines on one, single line:
Sub Picture1_MouseDown (Button As Integer, Shift As Integer,
X As Single, Y As Single)
Select Case Picture1.Point(X, Y)
Case QBColor(1)
label1.Caption = "1st"
Case QBColor(2)
label1.Caption = "2nd"
Case QBColor(3)
label1.Caption = "3rd"
Case QBColor(4)
label1.Caption = "4th"
Case QBColor(5)
label1.Caption = "5th"
Case QBColor(6)
label1.Caption = "6th"
Case QBColor(7)
label1.Caption = "7th"
Case QBColor(8)
label1.Caption = "8th"
Case QBColor(9)
label1.Caption = "9th"
Case QBColor(10)
label1.Caption = "10th"
Case QBColor(11)
label1.Caption = "11th"
Case QBColor(12)
label1.Caption = "12th"
Case QBColor(13)
label1.Caption = "13th"
Case QBColor(14)
label1.Caption = "14th"
End Select
End Sub
- Save the project (ALT, F, S).
- Run the program by pressing the F5 key. When you click any of the bars
or Slices, the label will tell you which one you selected.
Notes
Additional query words:
3.00
Keywords :
Version : WINDOWS:3.0
Platform : WINDOWS
Issue type :
|