Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,085 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,808 people online right now. Registration is fast and FREE... Join Now!




Temperatur Converting using Two Scroll Bars. (SOLVED)

 
Reply to this topicStart new topic

Temperatur Converting using Two Scroll Bars. (SOLVED)

Slayer6234
22 Apr, 2008 - 08:47 AM
Post #1

New D.I.C Head
*

Joined: 22 Apr, 2008
Posts: 3

Hey guys,

I'm trying to create an app. /w a function that allows a user to click on either of two scrollbars on screen and see the other one move with the right conversion.

For example, one bar would be labeled as Fahrenheit and the other as Celsius. If as user clicks and scrolls one of them. The other will change with the appropriate conversion taking place.
So if someone scrolled the Fahrenheit bar to 32 then the Celsius bar would change in real time to 0.
These numbers would be displayed in labels of course.

However, I know this code can be done with a simple function returning the right number for both scrollbar values depending on whats clicked.

This is what I have so far, however it does nothing except show the ValueChanged in the labels.

CODE


Public Class Form1

    Private Function GetNumber(ByVal intNum As Integer, ByVal scaleStr As String) As Integer
        Dim intAnswer As Integer
        If scaleStr = "F" Then
            intAnswer = (5 / 9) * (intNum - 32)
        Else
            intAnswer = (9 / 5) * intNum + 32
        End If
        Return intAnswer
    End Function

    Private Sub FarenheitHScrollBar_ValueChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles FarenheitHScrollBar.ValueChanged
        Dim farenInt As Integer
        farenInt = GetNumber(FarenheitHScrollBar.Value, "F")
        FarenLabel.Text = farenInt.ToString
    End Sub

    Private Sub CelsiusHScrollBar_ValueChanged1(ByVal sender As Object, ByVal e As System.EventArgs) Handles CelsiusHScrollBar.ValueChanged
        Dim celsInt As Integer
        celsInt = GetNumber(CelsiusHScrollBar.Value, "C")
        CelsLabel.Text = celsInt.ToString
    End Sub
End Class



Any help to correct this would be greatly appreciated.

This post has been edited by Slayer6234: 22 Apr, 2008 - 10:21 AM
User is offlineProfile CardPM
+Quote Post

Sonic88
RE: Temperatur Converting Using Two Scroll Bars. (SOLVED)
22 Apr, 2008 - 09:56 AM
Post #2

D.I.C Head
**

Joined: 19 Feb, 2008
Posts: 166



Thanked: 2 times
My Contributions
CODE

Public Class Form1

    Private Sub CelsiusHScrollBar_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CelsiusHScrollBar.ValueChanged
        Dim celInt As Integer
        celInt = GetNumber(Me.CelsiusHScrollBar.Value, "C")
        Me.lblCel.Text = Me.CelsiusHScrollBar.Value.ToString + " Degrees Celsius"
        Me.lblFar.Text = celInt.ToString + " Degrees Fahrenheight"
        Me.FarenheitHScrollBar.Value = celInt
    End Sub


    Private Sub FarenheitHScrollBar_Scroll(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ScrollEventArgs) Handles FarenheitHScrollBar.Scroll
        Dim farInt As Integer
        farInt = GetNumber(Me.FarenheitHScrollBar.Value, "F")
        Me.lblCel.Text = farInt.ToString + " Degrees Celsius"
        Me.lblFar.Text = Me.FarenheitHScrollBar.Value.ToString + " Degrees Fahrenheight"
        Me.CelsiusHScrollBar.Value = farInt
    End Sub


    'conversion
    Private Function GetNumber(ByVal intNum As Integer, ByVal scaleStr As String) As Integer
        Dim intAnswer As Integer
        If scaleStr = "F" Then
            intAnswer = (5 / 9) * (intNum - 32)
        Else
            intAnswer = (9 / 5) * intNum + 32
        End If
        Return intAnswer
    End Function


End Class




There ya go. Give that a try. It should do what you are asking. However, there is logical problem as you need to set the min and max values for your scroll bars or you get an out of range exception. If you want both bars to move and change the other you will have to put a check in there to make sure the other bar does not exceed the max. For teh code above i just set the min and max to extreme so that it would not occur for testing, but you will need to address that issue. Now, you could make it so that you could only move, say, the celsius bar and have that locked to 0 - 100 and then you could set your Fahrenheit bar to the possible max and mins. Then you could have a radio to toggle between moving celsius or Fahrenheit and reset your mins and max dynamically to account for the change. The last option, which may be a little advanced, would be to write an alogrithm to dynamically set you mins and max as needed.

User is offlineProfile CardPM
+Quote Post

Slayer6234
RE: Temperatur Converting Using Two Scroll Bars. (SOLVED)
22 Apr, 2008 - 10:20 AM
Post #3

New D.I.C Head
*

Joined: 22 Apr, 2008
Posts: 3

Sonic88 Thanks for the help!

That worked perfectly.

For the logic problem, I just set the f's min max between -20 and 120, and c's between -29 and 49.
Everything worked great.

I really appreciate the assistance!
User is offlineProfile CardPM
+Quote Post

skyhawk133
RE: Temperatur Converting Using Two Scroll Bars. (SOLVED)
22 Apr, 2008 - 10:22 AM
Post #4

Head DIC Head
Group Icon

Joined: 17 Mar, 2001
Posts: 15,277



Thanked: 61 times
Dream Kudos: 1650
Expert In: Web Development

My Contributions
QUOTE
I really appreciate the assistance!


Tell your friends smile.gif


Welcome to DIC!
User is offlineProfile CardPM
+Quote Post

Slayer6234
RE: Temperatur Converting Using Two Scroll Bars. (SOLVED)
22 Apr, 2008 - 10:25 AM
Post #5

New D.I.C Head
*

Joined: 22 Apr, 2008
Posts: 3

It will be told!

Thanks for the welcome. biggrin.gif
User is offlineProfile CardPM
+Quote Post

Sonic88
RE: Temperatur Converting Using Two Scroll Bars. (SOLVED)
22 Apr, 2008 - 12:42 PM
Post #6

D.I.C Head
**

Joined: 19 Feb, 2008
Posts: 166



Thanked: 2 times
My Contributions
No Problem. Glad I could help!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 11:39PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month