.NET inherited label doesn't autosize - vb.net

I'd like to inherit from Windows.Forms.Label, so I've made something like that:
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Protected Property DefaultText as
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, MyBase.Text)
End Get
Set(value As String)
MyBase.Text = value
End Set
End Property
End Class
The issue is using this, even if AutoSize property is still true, the label keep it's original size whatever the value set to CustomText.
So I thought that Mybase.Text property isn't updated when CustomText is set to I've changed to :
Public Class CustomLabel
Inherits Label
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
MyBase.Text = value
End Set
End Property
Protected _txt As String
Protected Property DefaultText As String
Public Overrides Property Text As String
Get
Return If(CustomText <> "", CustomText, DefaultText)
End Get
Set(value As String)
DefaultText = value
End Set
End Property
End Class
But still the same issue.
And, on the other side, when I set the Text property on a standard Label in the same place in the code where I set my CustomLabel, the autosize works.
Any idea ? Thanks

After some other tries, I've check how the TextChanged event is raised. It appears that the control is firstly initialized by the designer, and the CustomText is set to nothing, and as expected the TextChanged event is raised.
However, later in the code when CustomText property is set to the wanted value, I don't know why, but the event is not raised, even if the string is strictly different.
So to get a working code I have to write:
Public Property CustomText As String
Get
Return _txt
End Get
Set(value As String)
_txt = value
OnTextChanged(EventArgs.Empty) 'instead of Mybase.Text = value
End Set
End Property
To force the event and then trigger the label resize.
So it looks as solved, but if anyone has an explanation, I'd be happy to know it.

Related

Set default value to an image "Public Property"

I try to set default value to an image Public Property of a UserControl. I tried to do that with a variable but I get an error Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class.
Private Image_ As Image = My.Resources.MyImage
<Category("Appearance")> <DisplayName("Image")> <DefaultValue(Image_)> <Description("...")>
Public Property Image As Image
Get
Return NestedControl.Image
End Get
Set(ByVal value As Image)
NestedControl.Image = value
End Set
End Property
I also tried to set default value like this <DefaultValue(GetType(Image), "My.Resources.MyImage")> but when I do reset to UserControl's property it turns to "None"!!!
Any idea?
While the System.ComponentModel.DefaultValueAttribute does not support this, you can use the old-style ResetPropertyName and ShouldSerializePropertyName methods to achieve the same function.
This is documented in Defining Default Values with the ShouldSerialize and Reset Methods.
Imports System.ComponentModel
Public Class MyUserControl
Private Image_ As Image = My.Resources.MyImage
Public Sub New()
InitializeComponent()
ResetImage() ' set default
End Sub
<Category("Appearance")> <DisplayName("Image")> <Description("...")>
Public Property Image As Image
Get
Return NestedControl.Image
End Get
Set(ByVal value As Image)
NestedControl.Image = value
End Set
End Property
Public Sub ResetImage()
If Image IsNot Nothing Then Image.Dispose()
Image = Image_
End Sub
Public Function ShouldSerializeImage() As Boolean
Return (Image IsNot Image_)
End Function
End Class

VB.NET Forms ListBox doesn't display DataSource

I'm trying to link my ListBox to an ObservableCollection.
Here's my class for defining mods:
Public Class TroveMod
Private m_FileName As String
Private m_Enabled As Boolean
Public Property FileName() As String
Get
Return m_FileName
End Get
Set(value As String)
m_FileName = value
End Set
End Property
Public Property Enabled() As Boolean
Get
Return m_Enabled
End Get
Set(value As Boolean)
m_Enabled = value
End Set
End Property
Public ReadOnly Property ModName()
Get
Return Path.GetFileNameWithoutExtension(FileName)
End Get
End Property
End Class
And this is the actual Property ModList:
Public Property ModList() As ObservableCollection(Of TroveMod)
Get
Return m_ModList
End Get
Set(value As ObservableCollection(Of TroveMod))
m_ModList = value
End Set
End Property
I add items using:
Private Sub AddMod(file__1 As String, enabled As Boolean)
If File.Exists(file__1) Then
ModList.Add(New TroveMod() With { _
.FileName = file__1, _
.Enabled = enabled _
})
End If
End Sub
Everytime I want to add something to this Collection using AddMod, it won't show off in my listbox :/ I added an ModListBindingSource to the ListBox and set the DisplayMember and ValueMember to ModName but it still won't work. I also got a status label, which says, that it successful added the mods to the collection but it simply won't show them in the ListBox. Did I miss something?
An ObservableCollection is not quite what you want - this would allow your code to see changes to the collection via events. For instance, if there were several actors adding items to the collection and you wanted to know that. In this case, since 'local' code is adding items, it is not needed.
It is also not clear how the collection is mapped to the ListBox as the DataSource. Try this:
Public Class Form...
Private myList As New BindingList(Of TroveMod)
Sub Form_Load(....
theListBox.DataSource = myList
theListBox.DisplayMember = "ModName"
Now, as you add things to the BindingList they will appear in theListBox. If an item changes though (e.g. the Name), that change will not show up without a little more work, but as these are file names that seems unlikely.
The form property is not needed unless an external class also needs access to the collection/BindingList; in that case, you probably do not want need a setter.

Why does the text property overridden in user control is not showing at design time

I have a usercontrol which overrides the property Text. But this property is not shown at design time.
If I rename it to caption or value it is shown in properties at design time but Text is not shown.
public Class SomeControl
Inherits System.Windows.Forms.UserControl
Public Overrides Property Text() As String
Get
Return lblText.Text
End Get
Set(ByVal value As String)
lblText.Text = value
End Set
End Property
End Class
What to do?
Added following attributes and the problem is solved.
<EditorBrowsable(EditorBrowsableState.Always)> _
<Browsable(True)> _
<DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
<Bindable(True)> _
Public Overrides Property Text() As String
Get
Return lblText.Text
End Get
Set(ByVal value As String)
lblText.Text = value
End Set
End Property
The Text property is defined as:
[Bindable(false), EditorBrowsable(EditorBrowsableState.Never), Browsable(false),
DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
Meaning, you can't browse it in the property window; you need to override the property attributes defined here (which I don't know if that will work as expected) or just set the property name to something else.
HTH.

Pass string to Dialog in VB.net

I have a form and from this I call
dialogPrintDiet.ShowDialog()
which launchs my dialog. I need to pass a string value and need the easiest way to do this in VB.NET .
Try properties, for example setting some text boxes in your dialog:
Property FirstName() As String
Get
Return txtFirstName.Text
End Get
Set(ByVal Value As String)
txtFirstName.Text = Value
End Set
End Property
Property LastName() As String
Get
Return txtLastName.Text
End Get
Set(ByVal Value As String)
txtLastName.Text = Value
End Set
End Property
You can either add a property to the form or you can add a parameter to your form's constructor.
An example of the first method would look like (where Message is the name of the property)
frm.Message = "Some text"
An example of the second method would look like
Dim frm As New SampleForm ( "Some text" )
Your form code would be something like
Public Class SampleForm
Private someMessage As String
Public Sub New(ByVal msg As String)
InitializeComponent()
If Not (String.IsNullOrEmpty(msg)) Then
someMessage = msg
End If
End Sub
Property Message() As String
Get
Return someMessage
End Get
Set(ByVal Value As String)
someMessage = Value
End Set
End Property
End Class

Create Custom Control with reusable properties

This is similar to my last post but with a different purpose.
I have built a custom control, but when I set the properties for it... ALL instances of that control on my page grab the exact same property. I need to be able to set the property to "abc" for one instance of the control on my page, and then set the exact same proprty to "xyz" for a different instance of the control on the same page.
Can anyone shed any light?
Namespace CustomControl
Public Class mycontrol : Inherits Literal
Protected Overrides Sub CreateChildControls()
Dim value As String = "test"
If _doubleit = True Then
value = value & " test"
End If
MyBase.Text = value
MyBase.CreateChildControls()
End Sub
Private Shared _doubleit As Boolean = True
Public Shared Property doubleit() As Boolean
Get
Return _doubleit
End Get
Set(ByVal value As Boolean)
_doubleit = value
End Set
End Property
End Class
End Namespace
Remove the Shared from your variable and from your property declaration. Shared means exactly that what you don't want: All instances share the same value.
So, your code should look like this:
Private _doubleit As Boolean = True
Public Property doubleit() As Boolean
Get
Return _doubleit
End Get
Set(ByVal value As Boolean)
_doubleit = value
End Set
End Property