User control inherited from Label and Autosize

Wild Bill

Member
Joined
Oct 30, 2009
Messages
18
Location
Russia, Komsomolsk-on-Amur
Programming Experience
3-5
Hi.
I made User Control
VB.NET:
Imports System.Drawing
Public Class ZoomingLabel
    Private _zoom As Integer
    Private _minFontHeight As Integer
    Public Sub New()

        InitializeComponent()


        Me.AutoSize = False
        Me.Zoom = 6
        _minFontHeight = 8
        SetFont()
    End Sub
    Public Property Zoom() As Integer
        Get
            Return _zoom
        End Get
        Set(ByVal value As Integer)
            If value > 0 And value < 11 Then
                _zoom = value
                SetFont()
            End If
        End Set
    End Property
    Private Sub SetFont()
        Dim FontHeight As Integer
        FontHeight = CInt(Me.Height * _zoom / 10)
        If FontHeight < _minFontHeight Then
            FontHeight = _minFontHeight
        End If
        Dim fnt As Font = New Font(Me.Font.Name, FontHeight, Me.Font.Style)
        Me.Font = fnt
    End Sub
    Private Sub ZoomingLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SetFont()
    End Sub
End Class

In the constructor i wrote Me.AutoSize = False
But when i drop that ZoomingLabel on form AutoSize=True!
 
Wondering if this would do it:
VB.NET:
<DefaultValue(False)> Public Shadows Property Autosize() As Boolean
    Get

    End Get
    Set(ByVal value As Boolean)

    End Set
  End Property
 
VB.NET:
Imports System.Drawing
Imports System.ComponentModel
Public Class ZoomingLabel
    Private _zoom As Integer
    Private _minFontHeight As Integer
    Public Sub New()

        InitializeComponent()

        [B]MyBase.AutoSize = False
        Me.Autosize = False[/B]
        Me.Zoom = 6
        _minFontHeight = 8
        SetFont()
    End Sub
    [B]<DefaultValue(False)> Public Shadows Property Autosize() As Boolean
        Get
        End Get
        Set(ByVal value As Boolean)
        End Set
    End Property[/B]
    Public Property Zoom() As Integer
        Get
            Return _zoom
        End Get
        Set(ByVal value As Integer)
            If value > 0 And value < 11 Then
                _zoom = value
                SetFont()
            End If
        End Set
    End Property
    Private Sub SetFont()
        Dim FontHeight As Integer
        FontHeight = CInt(Me.Height * _zoom / 10)
        If FontHeight < _minFontHeight Then
            FontHeight = _minFontHeight
        End If
        Dim fnt As Font = New Font(Me.Font.Name, FontHeight, Me.Font.Style)
        Me.Font = fnt
    End Sub
    Private Sub ZoomingLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SetFont()
    End Sub
End Class

Result the same :(
BTW
Newguy, are you the same newguy that one from IGX?
 
Changed "Shadows" to "Overrides" and all became ok!
Thanks Newguy!

One problem. Now i can't change AutoSize property :)
It not a big problem, because this control always must with AutoSize=False

But it interesting in any case.
 
Last edited:
Well it was one or the other. Did you make a private variable to hold the AutoSize variable and put it in the Get/Set statements? And what is IGX?:confused:
 
Did you make a private variable to hold the AutoSize variable and put it in the Get/Set statements?
No. I don't understamd how to do it. When i doing this
VB.NET:
Imports System.Drawing
Imports System.ComponentModel
Public Class ZoomingLabel
    Private _zoom As Integer
    Private _minFontHeight As Integer
    Private _autoSize As Boolean
    Public Sub New()

        InitializeComponent()


        Me.Zoom = 6
        Me._minFontHeight = 8
[B]        Me._autoSize = False
        Me.Autosize = False
        MyBase.AutoSize = False
[/B]        SetFont()
    End Sub
[B]    <DefaultValue(False)> Public Overrides Property Autosize() As Boolean
        Get
            Return _autoSize
        End Get
        Set(ByVal value As Boolean)
            _autoSize = value
        End Set
    End Property
[/B]    Public Property Zoom() As Integer
        Get
            Return _zoom
        End Get
        Set(ByVal value As Integer)
            If value > 0 And value < 11 Then
                _zoom = value
                SetFont()
            End If
        End Set
    End Property
    Private Sub SetFont()
        Dim FontHeight As Integer
        FontHeight = CInt(Me.Height * _zoom / 10)
        If FontHeight < _minFontHeight Then
            FontHeight = _minFontHeight
        End If
        Dim fnt As Font = New Font(Me.Font.Name, FontHeight, Me.Font.Style)
        Me.Font = fnt
    End Sub
    Private Sub ZoomingLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SetFont()
    End Sub
End Class
Result became previous. I mean when dropping component on form AutoSize=True

I did this
VB.NET:
Imports System.Drawing
Imports System.ComponentModel
Public Class ZoomingLabel
    Private _zoom As Integer
    Private _minFontHeight As Integer
    Private _autoSize As Boolean
    Public Sub New()

        InitializeComponent()

        Me.Zoom = 6
        Me._minFontHeight = 8
        SetFont()
    End Sub
[B]    <DefaultValue(False), Browsable(False)> Public Overrides Property Autosize() As Boolean
        Get
        End Get
        Set(ByVal value As Boolean)
        End Set
    End Property
[/B]    Public Property Zoom() As Integer
        Get
            Return _zoom
        End Get
        Set(ByVal value As Integer)
            If value > 0 And value < 11 Then
                _zoom = value
                SetFont()
            End If
        End Set
    End Property
    Private Sub SetFont()
        Dim FontHeight As Integer
        FontHeight = CInt(Me.Height * _zoom / 10)
        If FontHeight < _minFontHeight Then
            FontHeight = _minFontHeight
        End If
        Dim fnt As Font = New Font(Me.Font.Name, FontHeight, Me.Font.Style)
        Me.Font = fnt
    End Sub
    Private Sub ZoomingLabel_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
        SetFont()
    End Sub
End Class
After that AutoSize=False, and not bothering me in the object inspector.
May be you can say what i doing wrong?

And what is IGX?:confused:
It one sport forum. There also one guy "Newguy" :)
 
Apparently not me if I did not even recognize it, lol. Well this is a weird behavior, usually when you set the default it is just that. I guess leaving the variable out is fine and it get you want you want. Let me guess, you are needing the AutoEllipsis on this control - which does not work if Autosize = True.
 
Also if your getting into custom properties and want to set them in the designer use the
<Browsable(True)> attribute.
 
Let me guess, you are needing the AutoEllipsis on this control - which does not work if Autosize = True.
No, but thanks for one more good hint :) AutoEllipsis will be usefull for this component.

I just wanted label, in wich text will resize togeather with component.
I mean when i zoom program window, text in ZoomingLabel will also zooming and vice versa.
1.png

2.png
 
Back
Top