Results 1 to 11 of 11

Thread: [RESOLVED] VB 2005 Scale Trackbar

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Resolved [RESOLVED] VB 2005 Scale Trackbar

    Hello,

    I want to use the Me.Scale function in combination
    with a Trackbar.

    Does anyone know how to do this?

    Thanks

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB 2005 Scale Trackbar

    By "Me.Scale" I assume that you mean the Scale method of a form. I also assume you mean that you want to set a value on the TrackBar and then use that as a scaling factor. This is the sort of thing that you should be specifying though, not making us assume.

    Also, there are two good reasons that you shouldn't be calling the Scale method.

    1. The documentation states:
    This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
    2. The documentation states:
    NOTE: This method is now obsolete.
    If you want to use a TrackBar to set the size of a form between a maximum and minimum then you can do it without the Scale method. Set the MinimumSize and MaximumSize properties of the form in the designer. Also set the Minimum and Maximum properties of the TrackBar to 0 and 100 respectively. You can then use code like this:
    VB Code:
    1. Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged
    2.     Dim scalingFactor As Double = Me.TrackBar1.Value / 100
    3.     Dim width As Integer = Me.MinimumSize.Width + _
    4.                            Convert.ToInt32(Math.Round(scalingFactor * (Me.MaximumSize.Width - Me.MinimumSize.Width)))
    5.     Dim height As Integer = Me.MinimumSize.Height + _
    6.                             Convert.ToInt32(Math.Round(scalingFactor * (Me.MaximumSize.Height - Me.MinimumSize.Height)))
    7.  
    8.     Me.Size = New Size(width, height)
    9. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Exclamation Re: VB 2005 Scale Trackbar

    In the MSDN documentation 2005:

    .NET Framework Class Library
    Control.Scale Method (SizeF)
    Note: This method is new in the .NET Framework version 2.0.

    Scales the control and all child controls by the specified scaling factor.

    Namespace: System.Windows.Forms
    Assembly: System.Windows.Forms (in system.windows.forms.dll)

    Visual Basic (Usage)
    Dim instance As Control
    Dim factor As SizeF

    instance.Scale(factor)

    I have a usercontrol that's on a panel.
    I want to scale the form, panel and usercontrol

    Is there another way ?


    Thanks

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB 2005 Scale Trackbar

    Hmmm... two overloads of scale deprecated and one new one added. It appears that that overload is OK to use, but I wouldn't use it for what you're doing anyway. When you call it you are resizing the form based on its current size. If you want a TrackBar to work consistently then it would take a bit of work. I'd suggest using a TableLayoutPanel to handle resizing the controls when the form resizes. if you set up a TLP appropriately its cells will scale with the form. The docked or anchored controls in those cells will thus also scale with the form.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Exclamation Re: VB 2005 Scale Trackbar

    Using a TableLayoutPanel is not an option , it's a keyboard layout.


    Thanks

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB 2005 Scale Trackbar

    Quote Originally Posted by Spitje
    Using a TableLayoutPanel is not an option , it's a keyboard layout.


    Thanks
    I don't see how that precludes the use of a TLP. Sounds like just the ticket to me.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Exclamation Re: VB 2005 Scale Trackbar

    If i use the Percent sizetype of the TLP then the resizing works,
    but the fontsize doesn't change ?

    I also have a problem with the different keysize ( Tab, Caps Lock, Shift etc)

    Thanks

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB 2005 Scale Trackbar

    Perhaps Scale is the way to go then. I think that your best bet to use it with a TrackBar would be to not use a linear scale. Make each step on the TrackBar a 10% increase in size relative to the current size. That would mean that the minimum value on the TrackBar would correspond to your minimum size. The next value would be 1.1 times the minimum; the next value would 1.1 times that, so 1.1 times 1.1 times the minimum; etc. That way there's no need to perform complex translations each time that may be inconsistent due to round-off errors.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Exclamation Re: VB 2005 Scale Trackbar

    Also the scale function doesn't change the fontsize ?

    If i don't use the trackbar but the form resize event, what is the best way to store the old control size, fontsize ,location, etc.


    Thanks
    Last edited by Spitje; Nov 12th, 2006 at 04:12 PM.

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: VB 2005 Scale Trackbar

    I thought you were saying that it did. So basically what I said before about setting the current size relative to a minimum and maximum is still the best way to go. You just do the same thing with the fonts too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Oct 2005
    Posts
    56

    Exclamation Re: VB 2005 Scale Trackbar

    Thanks for your support

    I've got it working:

    VB Code:
    1. Public Structure ControlSizePosition
    2.         Public Name as String
    3.         Public Left As Single
    4.         Public Top As Single
    5.         Public Width As Single
    6.         Public Height As Single
    7.         Public FontSize As Single
    8.     End Structure
    9.  
    10.     Private m_ControlSizePosition(150) As ControlSizePosition
    11.     Private m_FormWidth As Single
    12.     Private m_FormHeight As Single
    13.  
    14.     'Save the form's and controls
    15.     Private Sub SaveSizes()
    16.  
    17.         Dim i As Integer = 1
    18.  
    19.         For Each Ctrl As Control In Me.Controls
    20.             If TypeOf Ctrl Is Panel Then
    21.                 m_ControlSizePosition(i).Name = Ctrl.Name
    22.                 m_ControlSizePosition(i).Left = Ctrl.Left
    23.                 m_ControlSizePosition(i).Top = Ctrl.Top
    24.                 m_ControlSizePosition(i).Width = Ctrl.Width
    25.                 m_ControlSizePosition(i).Height = Ctrl.Height
    26.                 On Error Resume Next
    27.                 m_ControlSizePosition(i).FontSize = Ctrl.Font.Size
    28.             End If
    29.  
    30.             For Each Ctl As Control In Ctrl.Controls
    31.                 If TypeOf Ctl Is KB_Btn Then
    32.                     m_ControlSizePosition(i).Name = Ctl.Name
    33.                     m_ControlSizePosition(i).Left = Ctl.Left
    34.                     m_ControlSizePosition(i).Top = Ctl.Top
    35.                     m_ControlSizePosition(i).Width = Ctl.Width
    36.                     m_ControlSizePosition(i).Height = Ctl.Height
    37.                     On Error Resume Next
    38.                     m_ControlSizePosition(i).FontSize = Ctl.Font.Size
    39.                 End If
    40.                 i += 1
    41.             Next Ctl
    42.             i += 1
    43.         Next Ctrl
    44.  
    45.         ' Save the form's size.
    46.         m_FormWidth = Me.Width
    47.         m_FormHeight = Me.Height
    48.  
    49.     End Sub
    50.  
    51.     Private Sub KeyBoard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    52.         SaveSizes()
    53.     End Sub
    54.  
    55.     Private Sub KeyBoard_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
    56.         ResizeControls()
    57.     End Sub
    58.  
    59.     'Arrange the controls for the new size.
    60.     Private Sub ResizeControls()
    61.  
    62.         Dim i As Integer = 1
    63.         Dim Ctrl As Control
    64.         Dim x_scale As Single
    65.         Dim y_scale As Single
    66.         Dim sFont As Single
    67.  
    68.         x_scale = Me.Width / m_FormWidth
    69.         y_scale = Me.Height / m_FormHeight
    70.  
    71.  
    72.         For Each Ctrl In Me.Controls
    73.  
    74.             If TypeOf Ctrl Is Panel AndAlso Ctrl.Name = m_ControlSizePosition(i).Name Then
    75.                 Ctrl.Left = x_scale * m_ControlSizePosition(i).Left
    76.                 Ctrl.Top = y_scale * m_ControlSizePosition(i).Top
    77.                 Ctrl.Width = x_scale * m_ControlSizePosition(i).Width
    78.                  sFont = y_scale * m_ControlSizePosition(i).FontSize
    79.                 With Ctrl
    80.                     .Font = New Font(.Font.Name, sFont, .Font.Style, .Font.Unit)
    81.                 End With
    82.  
    83.             End If
    84.  
    85.  
    86.             For Each Ctl As Control In Ctrl.Controls
    87.  
    88.                 If TypeOf Ctl Is KB_Btn AndAlso Ctl.Name = m_ControlSizePosition(i).Name Then
    89.                     Ctl.Left = x_scale * m_ControlSizePosition(i).Left
    90.                     Ctl.Top = y_scale * m_ControlSizePosition(i).Top
    91.                     Ctl.Width = x_scale * m_ControlSizePosition(i).Width
    92.                      sFont = y_scale * m_ControlSizePosition(i).FontSize
    93.                     With Ctl
    94.                         .Font = New Font(.Font.Name, sFont, .Font.Style, .Font.Unit)
    95.                     End With
    96.                    
    97.                 End If
    98.                 i += 1
    99.             Next Ctl
    100.  
    101.             i += 1
    102.         Next Ctrl
    103.  
    104.     End Sub

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