Changing a Controls Property - vb.net

There is something not going out of my head because i spent now several days to get an understandable answer with reading books and searching the web.
In my VS 2012 VB.net project I have a usercontrol that contains a toolstrip. The toolstrip has a toolstripdropdownbutton with a toolstripmenuitem in it. I drag this usercontrol onto a windows form. My aim is to change the text of the menuitem when I'am in the forms designer mode.
I tried to create some properties in the usercontrol for example
public property TS as Toolstrip
get
return Toosltrip1
end get
set (value as Toolstrip)
Toolstrip1 = value
end set
Doing so I can open the toolbar item collection in the property grid of the usercontrol on the windows form using the new usercontrols property TS and I am able to change the menuitems text
but when I run the windows form the old text of the menuitem is displayed and my property change is ignored.
What should I do so that the property change is not ignored.
Please help me to clear my head with this problem .

Since you are only changing the text, use this property instead :
Public Property MenuText As String
Get
Return ToolStripMenuItem1.Text
End Get
Set(value As String)
ToolStripMenuItem1.Text = value
End Set
End Property

Related

Pass value from form to button text vb.net

I am learning vb.net and I'm having issues searching for what I need. I want to create a button that is "re-usable" throughout my application without needing to write code for each instance. So, what I would like to start with is take a variable in a form, example, public integer value and when this value changes I want to write to the text of a button. I know I can easily do this by writing code in the form btn_xxx.text = variable, but what if I have several buttons and each button looks at the same variable? Currently what I do is create a component which inherits a button and have a timer that on tick will look at the variable and write to the text. I'm sure there is a better way. Can anyone point me in the right direction? I know part of my problem is I don't know the nomenclature on what things are called, so hopefully I asked my question without too much confusion.
I saw this, https://www.daniweb.com/programming/software-development/threads/124842/detect-variable-change, but I don't see how to adapt that to my situation.
Here is what I have:
Private WithEvents Active_Alarm As New Nav_Active_Alarm
Then inside of a sub that calculates the count:
Active_Alarm.Count = CInt(dt_Active_Alarms.Rows.Count)
The user control:
Public Class Nav_Active_Alarm
Private mActive_Alarm_Count As Integer
Public Event Active_Alarm_Count_Changed(ByVal mvalue As Integer)
Public Property Count() As Integer
Get
Count = mActive_Alarm_Count
End Get
Set(ByVal value As Integer)
mActive_Alarm_Count = value
If Not Me.DesignMode Then
RaiseEvent Active_Alarm_Count_Changed(mActive_Alarm_Count)
test()
End If
End Set
End Property
Private Sub test()
If Not Me.DesignMode Then
If mActive_Alarm_Count = 0 Then
Me.btn_Goto_Active_Alarm.Image = My.Resources.Alarm_Clock_Static
'console or msgbox will work but updating the image will not
Else
Me.btn_Goto_Active_Alarm.Image = My.Resources.Alarm_Clock_Animation
'console or msgbox will work but updating the image will not
End If
End If
End Sub
End Class
If I write to console or add a msgbox I will see the event working. But, the image will not change. If I call the test sub from a timer it will work. Why won't the button update (by the way, I did try refresh and update in the code)?
Observer pattern is what you probably looking for.
This is quick and dirty.
Create a class to hold the variable value. Add a method that adds a button instance to a list.
Then a button that needs to know about the variable calls the register method.
When the value of the variable changes, it iterates through the list of buttons and sets the Text property of each one.
You might have jumped in a bit too deep too quick here. Google Custom data binding in .net, there's loads of built in stuff you can use. Though do it yourself is a good exercise.
A simple method to do this might be:
Create a form level list to hold the buttons you are interested in
Add the buttons you are interested in, into the list (maybe in form load or some other place where you have initialization code)
Create a private property in your form with a backing variable to hold the value you want to have applied to the buttons. In the setter portion spin through the list and set each buttons text.
Dim lstButtons As New List(Of Button)
Sub SetupButtons()
'call from form load or other init code
lstButtons.Add(btnPopulate)
lstButtons.Add(btnPopulate)
End Sub
Private _buttonText As String
Private Property ButtonText As String
Get
Return _buttonText
End Get
Set(value As String)
_buttonText = value
For Each b As Button In lstButtons
b.Text = value
Next
End Set
End Property
When you set the property - which now acts as your variable - it will update all of your textboxes for you.
I realize you mentioned without having to write code - but something has to tie things together. Even if you used the observer pattern (which is an elegant solution for this - so props to those who suggested it) you'd probably end up creating a class to hold the property and have that class implement the INotifyPropertyChanged from System.ComponentModel, and then you'd also have to have each button have a databinding for its text property to the property in the object of your class. There isn't really a way (that I can think of) to get around having to write some code for each form you do this in (though the class part you'd only have to write once of course).

Font property issue in usercontrol

I am working on a visual basic project builded using Visual Studio 2015. I have make a custom usercontrol that contains a RectangleShape and a textbox. Into usercontrol code I have added font properties...
Public Overrides Property Font() As Font
Get
Return TextBox.Font
End Get
Set(ByVal value As Font)
TextBox.Font = value
End Set
End Property
So I load my custom usercontrol to my project and for example, I change font's size.
My problem is that I can see the changes into design mode but not into run mode where shows the default font size!!!
Any idea?
HansPassant gave the answer to this question!!! So, To help other people who may have the same question, this is how my custom font property should look...
Public Overrides Property Font() As Font
Get
Return MyBase.Font
End Get
Set(ByVal value As Font)
MyBase.Font = value
TextBox.Font = value
End Set
End Property

VB.NET Adding list of property in Designer to choose from

First excuse my my english, I'm french.
I have made a control (a class in fact) inherited from textbox.
The purpose of this class is simply to keep the textbox aligned above a column of a datagridview.
All is working correctly. But I have a question :
I made a datagridview property, and in design time, I can simply select it in a combobox listing the datagridview of my form.
But I have also a columnName property as a string. I would like that, once the datagridview is selected, the designer propose me the list of the datagridviewcolumn name in this property.
I have absolutly no idea how to achieve that. I'm a very beginner in code, and have made no studies on it.
I tried making the string property into a datagridviewcolumn property, but not only did the designer do not propose me the ones available, but also it crashes and I have to remove all about this textbox in the file.Designer.vb.
Thank you for your help.
EDIT : Here is the code :
Public Class TextBoxFilter
Inherits TextBox
Private WithEvents _DGV As DataGridView = Nothing
Public Property DGV As DataGridView
Get
Return _DGV
End Get
Set(value As DataGridView)
_DGV = value
End Set
End Property
Private _ColumnName As String = String.Empty
Public Property ColumnName As String
Get
Return _ColumnName
End Get
Set(value As String)
_ColumnName = value
End Set
End Property
The rest of the code is irrelevant. Just know that it features event (when datagridviewcolumns are move, not displayed, not visible) to adjust the textbox.
To answer Plutonix, I would like to be able to give the designer a list of column, even if there is several datagridview in the form, and even if it can't find them automatically. I suppose I could do that in the set of the datagridview ? But I don't know how.

Remove Properties and Events from UserControl vb.net

I´m devoloment my own userControl with vb.net. I´m new with this task.
I want to remove default properties.
After google, I found several topics, like this:
Removing certain properties in a user control, i.e. forcing one value and not editable in Design mode
So, I´m trying to use it, but doesn´t works for me. I don´t know what I missing or doing wrong.
Public Class MyControlDesigner
Inherits System.Windows.Forms.Design.ControlDesigner
Protected Overrides Sub PreFilterProperties(ByVal properties As System.Collections.IDictionary)
MyBase.PreFilterProperties(properties)
properties.Remove("BackColor")
properties.Remove("ForeColor")
properties.Remove("Font")
End Sub
End Class
<DesignerAttribute(GetType(MyControlDesigner))> _
Public Class MyUserControl
' ...
End Class
To hide overwrite properties I follow this topic Hiding inherited properties and this works fine, for some of them.
<Browsable(False), EditorBrowsable(EditorBrowsableState.Never)> _
Public Shadows Property AutoScroll() As Boolean
Get
Return m_AutoScroll
End Get
Set(ByVal value As Boolean)
m_AutoScroll = value
End Set
End Property
But still, I have other properties that I don´t know how to hide or remove. Like Font, ForeColor, Margin etc...
Thanks advanced
Edit: Once I finish my control, I don´t want to see, all the properties like the picture, Only I want to show mine´s.
Edit: Add code from #Plutonix
I do not have access to that control/tool/property editor, but you can try to use a TypeConverter. This works with a control that inherits from UserControl to hide properties from a Property Grid, but it wont hide them from the VS IDE property editor.
The VS IDE uses reflection to get the property list and apparently ignores the TypeConverter. If your tool does something similar, this wont work - again, I dont have the tool to test it, but it is simple and worth a try.
I created an actual UserControl with a few controls on it. Then:
Imports System.ComponentModel
Public Class YControlConverter
Inherits TypeConverter
Public Overrides Function GetPropertiesSupported(context As ITypeDescriptorContext) As Boolean
Return True
End Function
Public Overrides Function GetProperties(context As ITypeDescriptorContext,
value As Object,
attributes() As Attribute) As PropertyDescriptorCollection
Dim propNames() As String = {"backcolor", "forecolor",
"autoscroll", "autoscrollminsize",
"autoscrollmargin", "autoscrolloffset",
"autoscrollposition"}
Dim pdc As PropertyDescriptorCollection = TypeDescriptor.GetProperties(context.Instance)
' collection to store the ones we want:
Dim myPDCList As New List(Of PropertyDescriptor)
For Each pd As PropertyDescriptor In pdc
If propNames.Contains(pd.Name.ToLowerInvariant) = False Then
myPDCList.Add(pd)
End If
Next
Return New PropertyDescriptorCollection(myPDCList.ToArray())
End Function
End Class
Then decorate your usercontrol with the TypeConverter:
<TypeConverter(GetType(YControlConverter))>
Public Class YControl
This basically runs thru the PropertyDescriptorCollection for the control and filters out the unwanted properties before returning the new collection. If it works, just add the names to the propNames array that you want to hide. View in a PropertyGrid:
As you can see, all the AutoScroll... properties are removed as well as BackColor. The others are gone as well. If the editor will use your TypeConverter instead of reflection, it should work.
--
How to test your TypeConverter using a PropertyGrid. Using a form with a property grid and a button, in the button click:
Dim yp As New YControl
PropertyGrid1.SelectedObject = yp
If the AutoScroll... properties are missing from the prop grid, your TypeConverter works! If they still show in the other tool, it is using reflection like VS.

Overriding a Custom Control's Text Property to use a Textbox's Text causes the Textbox to display the Control's name by default

Using VB.net (2005), I'm creating a custom control (a folder picker) that contains a textbox and button. Recently I've added the following:
<Browsable(True), DefaultValue(""), DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)> _
Public Overrides Property Text() As String
Get
Return Me.uiPath.Text
End Get
Set(ByVal value As String)
Me.uiPath.Text = value
End Set
End Property
The problem I'm having is that now when the control is created, the textbox always defaults to showing the name of the control (instead of being blank). It seems the Text property is being changed after Sub New is called, however, I've checked the designer, and I cannot see where it is doing this.
I know that changing the name of the property would resolve the issue, but I would like to know why this is happening (I'm guessing it has something to do with the standard designer?), and if there is a way to resolve (or work around) it, while still using the Text property?
I don't know if it makes any difference, but I am using a custom designer:
Public Class FolderPickerDesigner
Inherits Windows.Forms.Design.ControlDesigner
Public Overrides ReadOnly Property SnapLines() As IList
Get
Dim SnapLinesList As ArrayList = TryCast(MyBase.SnapLines, ArrayList)
If (Me.Control IsNot Nothing) AndAlso (Me.Control IsNot Nothing) Then
Dim FP As FolderPicker = CType(Me.Control, FolderPicker)
SnapLinesList.Add(New SnapLine(SnapLineType.Baseline, FP.uiPath.Bottom - 5, SnapLinePriority.Medium))
End If
Return SnapLinesList
End Get
End Property
End Class
Any help / ideas would be appreciated. If this doesn't / can't be resolved, I'll mark the most helpful post as the answer...
Set(ByVal value As String)
If Me.DesignMode And (Environment.StackTrace.Contains("System.Windows.Forms.Design.ControlDesigner.InitializeNewComponent")) Then Return
BaseT.Text = value
End Set