Override Textbox Properties In VB.NET - vb.net

It is possible to override textbox properties like selectionstart property

Yes you can by creating your own textbox class, such as
Class MyTextBox
Inherits TextBox
Public Shadows Property SelectionStart As Integer
Get
' Get Code
End Get
Set(ByVal value As Integer)
' Set code
End Set
End Property
End Class

You can create your own custom control that inherits from the textbox control and override whatever events you need to.
Here is some guidance from MSDN.

SelectionStart property is not marked as virtual, so you can not override it. Other properties that are virtual or overridable can be overridden.

Related

Force Inherited ComboBox' DropDownStyle Property to DropDown

In VB.NET (WinForms), how to force a control that inherits ComboBox to only allow DropDown for its DropDownStyle property?
The DropDownStyle property is not Overridable, that limits the amount of forcing your can do. The approach that .NET Framework takes is to just hide the property. Similar to:
Private Const DefaultStyle As ComboBoxStyle = ComboBoxStyle.DropDown
Public Sub New()
MyBase.DropDownStyle = DefaultStyle
End Sub
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)>
<DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)>
Public Overloads Property DropDownStyle As ComboBoxStyle
Get
Return MyBase.DropDownStyle
End Get
Set(value As ComboBoxStyle)
MyBase.DropDownStyle = DefaultStyle
End Set
End Property
The Browsable attribute hides the property from the designer, the EditorBrowsable attribute hides it from the text editor, the DesignerSerializationVisibility ensures it is not written to the InitializeComponent() method.
But that doesn't otherwise stop client code, if it really wants to then it can cast to ComboBox and bypass your property setter, still assigning the MyBase.DropDownStyle property to an arbitrary value. The liability of Overloads only hiding the base member and not overriding it. Technically you can still defeat that by overriding CreateParams but the property is used too often in the ComboBox code to make that truly effective. This is as good as it gets. Good enough for the framework.

How to change default property of a custom Control

The default property of my custom button is Text.I want to change to a custom property named MaximumClicks
Public Property MaximumClicks As Integer
Get
Return maxClick
End Get
Set(value As Integer)
maxClick = value
End Set
End Property
You can apply the DefaultPropertyAttribute to your class:
<DefaultProperty("MaximumClicks")> _
Public Class CustomButton
Inherits Button
Public Property MaximumClicks As Integer
Get
' etc

Change the defaultValue of A custom control property

I created a number of custom controls but still struggling mastering the interface.
For uniformity besides creating custom proerties I also want to change some base properties of the custom control I tried following code
Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
Me.AutoCompleteMode = AutoCompleteMode.Suggest
Me.AutoCompleteSource = AutoCompleteSource.ListItems
MyBase.OnControlAdded(e)
End Sub
That however does not work when I drop the custom control on a form, I suppose the solution lies with adding the attribute and overriding the property.
I found an answer to this for C# but did not succeed to understand/translate it for vb.net
Since those properties are not overridable, try using the Shadows modifier instead:
Public Class MyComboBox
Inherits ComboBox
Public Sub New()
Me.AutoCompleteMode = AutoCompleteMode.Suggest
Me.AutoCompleteSource = AutoCompleteSource.ListItems
End Sub
<DefaultValue(AutoCompleteMode.Suggest)> _
Public Shadows Property AutoCompleteMode As AutoCompleteMode
Get
Return MyBase.AutoCompleteMode
End Get
Set(value As AutoCompleteMode)
MyBase.AutoCompleteMode = value
End Set
End Property
<DefaultValue(AutoCompleteSource.ListItems)> _
Public Shadows Property AutoCompleteSource As AutoCompleteSource
Get
Return MyBase.AutoCompleteSource
End Get
Set(value As AutoCompleteSource)
MyBase.AutoCompleteSource = value
End Set
End Property
End Class
Please note though, the DefaultValue attribute probably doesn't do what you think it does: it doesn't actually set a default value of that property. All it is used for is to tell the PropertyGrid what the default value for the property is, and if it matches it, it won't make it bold in the PropertyGrid view and it won't serialize the value in the designer file.

borderless button in visual basic.net?

HEY there I was asking about how to make a borderless button in vb.net I can always set the style to flat and make the background color as transparent but always I always get the button focused and shows in a shape of a button which ruins my button style that I want it
here is a class I used earlier but it didn't work
Public Class ButtonEx
Inherits Button
Private _ShouldShowFocus As Boolean = False
Public Property ShouldShowFocus() As Boolean
Get
Return _ShouldShowFocus
End Get
Set(ByVal value As Boolean)
_ShouldShowFocus = value
End Set
End Property
Protected Overrides ReadOnly Property ShowFocusCues() As Boolean
Get
Return _ShouldShowFocus
End Get
End Property
End Class
You could subclass the standard button and override the OnPaint method to achieve a borderless button.
Class BorderlessButton
Inherits Button
Protected Overrides Sub OnPaint(ByVal pe As PaintEventArgs)
MyBase.OnPaint(pe)
pe.Graphics.DrawRectangle(New Pen(BackColor, 5), ClientRectangle)
End Sub
End Class
I'm assuming you've tried out the Button.FlatAppearance properties and none of these help to solve your problem.
Public Class ButtonNoFocusCues
Inherits System.Windows.Forms.Button
Protected Overrides ReadOnly Property ShowFocusCues As Boolean
Get
Return False
End Get
End Property
End Class
You can use label that is clickable so it will be no border just add hover code.....
Also you can use bootstrap or css if your using asp.net

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.