Welcome Guest!
 VB.NET Helper
 Previous Message All Messages Next Message 
VB .NET Helper Newsletter  Rod Stephens
 Jul 12, 2011 15:30 PDT 

I've decided that I'm not going to have time for reasonably big
newsletters so I'm just going to try to get out something small whenever
I have a chance.
-----
Rumors about Windows 8 and the future of Microsoft's development tools
continue. This article is not hopeful about Microsoft's future unless
there's a shakeup:

    10 Reasons Microsoft Needs An HP-Like Management Shakeup
    http://tinyurl.com/5rzfxv9

This article is more hopeful for us developers:

    Windows 8 for software developers: the Longhorn dream reborn?
    http://tinyurl.com/64jf399

(Although people have already pointed out that the animal pictured at
the top of the article is an example of Scottish Highland Cattle, not a
Longhorn. It does have long horns, however.)

<tr><td align="center">6/25/2011</td><td align="center">148</td>
    <td>
    In Figure 11-2, the bottom check box has the wrong caption. It
should read:
    <p>
    <pre><tt>    !((A && B) || (!A && !B))</tt></pre>
    <p>
    </td>
-----
Off topic: A very cool concept for wrist computers:

    http://tinyurl.com/2fzcpr5
-----
Things to do this week:

- View screencasts from "Visual Basic 24-Hour Trainer":

    Lesson 3, Part 1: http://www.youtube.com/watch?v=7jxlebRMuu0
    Lesson 3, Part 2: http://www.youtube.com/watch?v=GLGIFmvZx7E
    Lesson 5:         http://www.youtube.com/watch?v=xeJPTBTmNzA

- Post book reviews

- Finish work on next book

- Write printing article
-----
Have a great week and thanks for subscribing!

Rod
RodSte-@vb-helper.com

Twitter feeds:
    VBHelper
    CSharpHelper
==========
==========

    VB.NET Contents:
1. New HowTo: Use a PictureBox to make a slider with a needle in Visual
Basic .NET
2. New HowTo: Use a PictureBox to make a slider with a value bar in
Visual Basic .NET
==========
++++++++++
      <VB.NET>
++++++++++
==========
1. New HowTo: Use a PictureBox to make a slider with a needle in Visual
Basic .NET
http://www.vb-helper.com/howto_net_picturebox_slider.html
http://www.vb-helper.com/HowTo/howto_net_picturebox_slider.zip

This example draws a vertical "needle" in a PictureBox to let the user
select a value much as a TrackBar does. The code does all of the
drawing, however, so you have control over the slider's appearance. The
example also displays the current value in a tooltip above the slider.

The program uses a variable and two constants to track the slider's
value and its minimum and maximum allowed values.

' The current value.
Private SliderValue As Single = 0.3

' The minimum and maximum allowed values.
Private Const MinimumValue As Single = 0.0
Private Const MaximumValue As Single = 1.0

The program uses two methods to convert between X coordinates in the
PictureBox and values between the minimum and maximum allowed values.

' Convert an X coordinate to a value.
Private Function XtoValue(ByVal x As Integer) As Single
    Return MinimumValue + (MaximumValue - MinimumValue) * _
        x / CSng(picSlider.ClientSize.Width - 1)
End Function

' Convert value to an X coordinate.
Private Function ValueToX(ByVal value As Single) As Single
    Return (picSlider.ClientSize.Width - 1) * _
        (value - MinimumValue) / CSng(MaximumValue - MinimumValue)
End Function

This is just math. The only trick here is that the code subtracts 1 from
the PictureBox's client width to map the rightmost visible pixel in the
PictureBox to the largest allowed value. If you don't do this, then when
the user selects the large allowed value the needle isn't visible
because it is one pixel to the right of the edge of the control's client
area.

When the user presses the mouse down on the PictureBox or moves the
pressed mouse over the PictureBox, the MouseDown or MouseMove event
handlers call the SetValue method to update the slider's value.

' Move the needle to this position.
Private MouseIsDown As Boolean = False
Private Sub picSlider_MouseDown(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles picSlider.MouseDown
    MouseIsDown = True
    SetValue(XtoValue(e.X))
End Sub

Private Sub picSlider_MouseMove(ByVal sender As System.Object, ByVal e
As System.Windows.Forms.MouseEventArgs) Handles picSlider.MouseMove
    If (Not MouseIsDown) Then Return
    SetValue(XtoValue(e.X))
End Sub

When the user releases the mouse, the following event handler sets
MouseIsDown so future MouseMove events don't do anything.

Private Sub picSlider_MouseUp(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.MouseEventArgs) Handles picSlider.MouseUp
    MouseIsDown = False
    tipValue.Hide(Me)

    ' Take action here if desired.
    lblResult.Text = SliderValue.ToString("0.00")
End Sub

The SetValue method updates the slider's value.

' Set the slider's value. If the value has changed,
' display the value tooltip.
Private Sub SetValue(ByVal value As Single)
    ' Make sure the new value is within bounds.
    If (value < MinimumValue) Then value = MinimumValue
    If (value > MaximumValue) Then value = MaximumValue

    ' See if the value has changed.
    If (SliderValue = value) Then Return

    ' Save the new value.
    SliderValue = value

    ' Redraw to show the new value.
    picSlider.Refresh()

    ' Display the value tooltip.
    Dim tip_x As Integer = picSlider.Left + CInt(ValueToX(SliderValue))
    Dim tip_y As Integer = picSlider.Top
    tipValue.Show(SliderValue.ToString("0.00"), Me, tip_x, tip_y, 3000)

    ' Take action here if desired.
    lblResult.Text = SliderValue.ToString("0.00")
End Sub

The code first ensures that the new value is within range. If the user
presses the mouse down over the control and then drags it to the right
or left of the PictureBox, the value could be outside of the allowed
range so this code fixes it.

Next if the new value and the old value are the same, the method exits.

If the method doesn't exit, it saves the new value and refreshes the
PictureBox so it shows the new needle position. It then displays the new
value in a tooltip above the PictureBox. Finally in this example the
code displays the new value in a label.

The last piece of interesting code is the PictureBox's Paint event
handler.

' Draw the needle.
Private Sub picSlider_Paint(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picSlider.Paint
    ' Calculate the needle's X coordinate.
    Dim x As Single = ValueToX(SliderValue)

    ' Draw it.
    e.Graphics.DrawLine(Pens.Blue, _
        x, 0, _
        x, picSlider.ClientSize.Height)
End Sub

This code simply draws the needle at the corrent position.

Using a SetValue method may seem overly elaborate. You could simply set
the new value directly and then refresh the PictureBox in the MouseDown
and MouseMove event handlers. The program uses SetValue to avoid doing
anything if the value doesn't change. That's important to avoid flicker
caused by unnecessary redraws and unnecessarily redisplaying the
tooltip.
==========
2. New HowTo: Use a PictureBox to make a slider with a value bar in
Visual Basic .NET
http://www.vb-helper.com/howto_net_picturebox_slider2.html
http://www.vb-helper.com/HowTo/howto_net_picturebox_slider2.zip

The example <a href="howto_net_picturebox_slider.html">Use a PictureBox
to make a slider with a needle in Visual Basic .NET</a> shows how to use
a PictureBox to make a simple slider. This example is similar except it
draws the slider in a different style. See the previous example for
information about how to make the slider.

The following code shows the only difference between this example and
the previous one: the way it draws.

' Draw the needle.
Private Sub picSlider_Paint(ByVal sender As System.Object, ByVal e As
System.Windows.Forms.PaintEventArgs) Handles picSlider.Paint
    ' Calculate the needle's X coordinate.
    Dim x As Single = ValueToX(SliderValue)
    Dim y As Integer = CInt(picSlider.ClientSize.Height * 0.25)
    Dim hgt As Integer = picSlider.ClientSize.Height - 2 * y

    ' Draw it.
    e.Graphics.FillRectangle(Brushes.Blue, _
        0, y, x, hgt)
    using pen as new Pen(Color.Blue, 3)
        e.Graphics.DrawLine(pen, _
            x, 0, _
            x, picSlider.ClientSize.Height)
    End Using
End Sub

This code draws a thin bar horizontally inside the PictureBox. It then
draws a vertical line at the selected value.

You can modify the Paint event handler further to produce other effects.
For example, you could draw the area to the left of the value in a solid
color, fill it with a gradient brush, or tile the area with a picture.
==========
Archives:
    http://www.topica.com/lists/VBHelper
    http://www.topica.com/lists/VB6Helper
    http://www.topica.com/lists/VBNetHelper

Twitter feeds:
    VBHelper
    CSharpHelper

Post questions at:
    http://www.topica.com/lists/VBHelperQA
	
 Previous Message All Messages Next Message 
  Check It Out!

  Topica Channels
 Best of Topica
 Art & Design
 Books, Movies & TV
 Developers
 Food & Drink
 Health & Fitness
 Internet
 Music
 News & Information
 Personal Finance
 Personal Technology
 Small Business
 Software
 Sports
 Travel & Leisure
 Women & Family

  Start Your Own List!
Email lists are great for debating issues or publishing your views.
Start a List Today!

© 2001 Topica Inc. TFMB
Concerned about privacy? Topica is TrustE certified.
See our Privacy Policy.