Hello,
I want to use the Me.Scale function in combination
with a Trackbar.
Does anyone know how to do this?
Thanks
Printable View
Hello,
I want to use the Me.Scale function in combination
with a Trackbar.
Does anyone know how to do this?
Thanks
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:2. The documentation states:Quote:
This method supports the .NET Framework infrastructure and is not intended to be used directly from your code.
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:Quote:
NOTE: This method is now obsolete.
VB Code:
Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged Dim scalingFactor As Double = Me.TrackBar1.Value / 100 Dim width As Integer = Me.MinimumSize.Width + _ Convert.ToInt32(Math.Round(scalingFactor * (Me.MaximumSize.Width - Me.MinimumSize.Width))) Dim height As Integer = Me.MinimumSize.Height + _ Convert.ToInt32(Math.Round(scalingFactor * (Me.MaximumSize.Height - Me.MinimumSize.Height))) Me.Size = New Size(width, height) End Sub
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
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.
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.Quote:
Originally Posted by Spitje
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
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.
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
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.
Thanks for your support
I've got it working:
VB Code:
Public Structure ControlSizePosition Public Name as String Public Left As Single Public Top As Single Public Width As Single Public Height As Single Public FontSize As Single End Structure Private m_ControlSizePosition(150) As ControlSizePosition Private m_FormWidth As Single Private m_FormHeight As Single 'Save the form's and controls Private Sub SaveSizes() Dim i As Integer = 1 For Each Ctrl As Control In Me.Controls If TypeOf Ctrl Is Panel Then m_ControlSizePosition(i).Name = Ctrl.Name m_ControlSizePosition(i).Left = Ctrl.Left m_ControlSizePosition(i).Top = Ctrl.Top m_ControlSizePosition(i).Width = Ctrl.Width m_ControlSizePosition(i).Height = Ctrl.Height On Error Resume Next m_ControlSizePosition(i).FontSize = Ctrl.Font.Size End If For Each Ctl As Control In Ctrl.Controls If TypeOf Ctl Is KB_Btn Then m_ControlSizePosition(i).Name = Ctl.Name m_ControlSizePosition(i).Left = Ctl.Left m_ControlSizePosition(i).Top = Ctl.Top m_ControlSizePosition(i).Width = Ctl.Width m_ControlSizePosition(i).Height = Ctl.Height On Error Resume Next m_ControlSizePosition(i).FontSize = Ctl.Font.Size End If i += 1 Next Ctl i += 1 Next Ctrl ' Save the form's size. m_FormWidth = Me.Width m_FormHeight = Me.Height End Sub Private Sub KeyBoard_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load SaveSizes() End Sub Private Sub KeyBoard_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize ResizeControls() End Sub 'Arrange the controls for the new size. Private Sub ResizeControls() Dim i As Integer = 1 Dim Ctrl As Control Dim x_scale As Single Dim y_scale As Single Dim sFont As Single x_scale = Me.Width / m_FormWidth y_scale = Me.Height / m_FormHeight For Each Ctrl In Me.Controls If TypeOf Ctrl Is Panel AndAlso Ctrl.Name = m_ControlSizePosition(i).Name Then Ctrl.Left = x_scale * m_ControlSizePosition(i).Left Ctrl.Top = y_scale * m_ControlSizePosition(i).Top Ctrl.Width = x_scale * m_ControlSizePosition(i).Width sFont = y_scale * m_ControlSizePosition(i).FontSize With Ctrl .Font = New Font(.Font.Name, sFont, .Font.Style, .Font.Unit) End With End If For Each Ctl As Control In Ctrl.Controls If TypeOf Ctl Is KB_Btn AndAlso Ctl.Name = m_ControlSizePosition(i).Name Then Ctl.Left = x_scale * m_ControlSizePosition(i).Left Ctl.Top = y_scale * m_ControlSizePosition(i).Top Ctl.Width = x_scale * m_ControlSizePosition(i).Width sFont = y_scale * m_ControlSizePosition(i).FontSize With Ctl .Font = New Font(.Font.Name, sFont, .Font.Style, .Font.Unit) End With End If i += 1 Next Ctl i += 1 Next Ctrl End Sub