Results 1 to 10 of 10

Thread: Measurment Converstions

  1. #1

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Measurment Converstions

    i have an 08 project that has 50 forms and 28 modules that outputs hundreds if not thousands of metric measurements with labels showing "lm" for lineal meters or "m2" for square meters.

    I am wanting to also have the project output imperial measurements as in "ft" feet and "sqft" square feet.

    I know i can write hundreds of lines of code like below to do this:

    Code:
     if checkbox_imperial.checked 
    xxlbl.text = sqfeet
    end if
    But am hoping there will be a easier way before i begin.

    What is the typical way to handle both metric and imperial measurements for different eviroments?

    thx in advance...

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Measurment Converstions

    feet = ((metres * 100) / 2.25 / 12)

    You'll need to show some code for a useful answer, but i'm not sure how you're hoping to shoehorn imperial conversions in there...

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Measurment Converstions

    Do you have hundreds or thousands of output labels?

  4. #4

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Measurment Converstions

    at minimum hundreds on the 50 odd forms.

  5. #5

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Measurment Converstions

    i can do the conversion was more after the correct way its usually handled.

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Measurment Converstions

    How about this:

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Label1.TextChanged, AddressOf ClassHandlers.Labels_TextChanged
        End Sub
    
    End Class
    Code:
    Public Class Form2
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            AddHandler Label1.TextChanged, AddressOf ClassHandlers.Labels_TextChanged
        End Sub
    
    End Class
    Code:
    Public Class ClassHandlers
        'you need to set this on checkbox_imperial.checkedchanged
        Public Shared convert As Boolean
    
        Public Shared Sub Labels_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
            If convert Then
                Dim lbl As Label = DirectCast(sender, Label)
                If lbl.Text.EndsWith("lm") Then
                    Dim d As Decimal
                    If Decimal.TryParse(lbl.Text.Replace("lm", ""), d) Then
                        'conversion code here and output back to the same label
                    End If
                End If
            End If
        End Sub
    End Class

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: Measurment Converstions

    There might be a more efficient answer, but I really can't think how...

  8. #8

    Thread Starter
    Frenzied Member toecutter's Avatar
    Join Date
    Apr 2006
    Location
    Brisbane, Australia
    Posts
    1,160

    Re: Measurment Converstions

    Quote Originally Posted by .paul. View Post
    There might be a more efficient answer, but I really can't think how...
    thx heaps for that .paul i will check it thoroughly

  9. #9
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Measurment Converstions

    As far as your form will contains such huge amount of labels, it is better to create your own Label and encapsulate required conversions.

    Here is a working code you can start with

    VB.NET Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class MyLabel
    5.     Inherits System.Windows.Forms.Label
    6.  
    7.     Public Enum Units
    8.         Centimetre
    9.         Meter
    10.         '
    11.         Inch
    12.         Foot
    13.     End Enum
    14.  
    15.     Private _Unit As Units
    16.     Public Property Unit() As Units
    17.         Get
    18.             Return _Unit
    19.         End Get
    20.         Set(ByVal value As Units)
    21.             _Unit = value
    22.             Select Case value
    23.                 Case Units.Meter ' it is the default, so no conversion needed
    24.                     Me.Text = CStr(Me.Value) & " m"
    25.                 Case Units.Centimetre
    26.                     Me.Text = CStr(Me.Value * 100) & " cm"
    27.                 Case Units.Foot
    28.                     Me.Text = CStr(Me.Value / 0.3048) & " ft"
    29.                 Case Units.Inch
    30.                     Me.Text = CStr(Me.Value / 0.0254) & " in"
    31.             End Select
    32.         End Set
    33.     End Property
    34.  
    35.     Public Sub New()
    36.         Me.Unit = Units.Meter ' set default unit.
    37.     End Sub
    38.  
    39.     Private _Value As Decimal
    40.     Public Property Value() As Decimal
    41.         Get
    42.             Return _Value
    43.         End Get
    44.         Set(ByVal value As Decimal)
    45.             _Value = value
    46.             Me.Text = CStr(value) & " m"
    47.         End Set
    48.     End Property
    49.  
    50. End Class

    Usage

    VB.NET Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    6.         MyLabel1.Value = 10 ' meters
    7.         RadioButton3.Checked = True ' convert to feet
    8.     End Sub
    9.  
    10.     Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
    11.         MyLabel1.Unit = MyLabel.Units.Meter
    12.     End Sub
    13.  
    14.     Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
    15.         MyLabel1.Unit = MyLabel.Units.Centimetre
    16.     End Sub
    17.  
    18.     Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
    19.         MyLabel1.Unit = MyLabel.Units.Foot
    20.     End Sub
    21.  
    22.     Private Sub RadioButton4_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged
    23.         MyLabel1.Unit = MyLabel.Units.Inch
    24.     End Sub
    25. End Class



  10. #10
    PowerPoster ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    3,130

    Re: Measurment Converstions

    Hi,

    why not in diffrent Datagridviews?

    regards
    Chris
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width