Is there a Tag for an Item in the CheckedListBox? Or something similar? I would like to be able to store and ID associated with the item that I'm displaying.
You don't need a Tag property. The control accepts any object, that means you don't have to put just strings in it. Make a class that has a string (and overrridden ToString()) and any other data members you need.
Public Class Form1
Protected Overrides Sub OnLoad(ByVal e As System.EventArgs)
MyBase.OnLoad(e)
CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "One", .ExtraData = "extra 1"})
CheckedListBox1.Items.Add(New MyListBoxItem() With {.Name = "Two", .ExtraData = "extra 2"})
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For Each obj As Object In CheckedListBox1.CheckedItems
Dim item As MyListBoxItem = CType(obj, MyListBoxItem)
MessageBox.Show(String.Format("{0}/{1} is checked.", item.Name, item.ExtraData))
Next
End Sub
End Class
Public Class MyListBoxItem
Private _name As String
Private _extraData As String
Public Property Name As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property ExtraData As String
Get
Return _extraData
End Get
Set(ByVal value As String)
_extraData = value
End Set
End Property
Public Overrides Function ToString() As String
Return Name
End Function
End Class
(The overridden ToString() dictates what will be displayed in the box.)
You can inherit your own control from CheckedListBox and create a property, in C# it would be like this, the rest of the functionality remains the same as it is inherited so no further additional code required:
public class MyCheckedListbox : System.Windows.Forms.CheckedListBox{
private object thisObj;
public object Tag{
get{ return this.thisObj; }
set{ this.thisObj = value; }
}
}
Edit: Decided to include the VB.NET version for everyone's benefit also...
Public Class MyCheckedListBox Inherits System.Windows.Forms.CheckedListBox
Private thisObj As Object
Public Property Tag As Object
Get
Tag = thisObj
End Get
Set (objParam As Object)
thisObj = objParam
End Set
End Property
End Class
Of course, this is plain and uses boxing but works nicely...
Hope this helps
Translation of tommieb75 answer to VB.NET:
Public Class MyCheckedListbox
Inherits System.Windows.Forms.CheckedListBox
Private thisObj As Object
Public Property Tag() As Object
Get
Return Me.thisObj
End Get
Set(ByVal value As Object)
Me.thisObj = value
End Set
End Property
End Class
I use the translator at www.developerfusion.com/tools
Related
I have a Simple Property called Customer as string
I want to bind this property to a Textbox.Text Databinding
I use the INotifyPropertyChanged Interface.
If I want to add the Databindings with
TextBox1.DataBindings.Add("Text", Customer, "Text")
I get an Error with:
You cannot bind text to the property or column for the DataSource.
Parameter name: dataMember
Public Class Form1
Implements INotifyPropertyChanged
Private _Customer As String = "DEFAULT"
Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.DataBindings.Add("Text", Customer, "Text")
End Sub
Public Property Customer As String
Get
Return _Customer
End Get
Set
_Customer = Value
NotifyPropertyChanged()
End Set
End Property
Private Sub NotifyPropertyChanged(<CallerMemberName()> Optional ByVal propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
The reason is that you can't bind to a property directly, you need to bind to an object which contains the property. Also you can't use a property that is inside the Form1 class. You need to set up an instance of an object.
I've created a sample that uses a class named Customer with a single property called Name. I've also created a base class. This is not required, but is useful if you create multiple classes.
Public Class BaseNotify
Implements INotifyPropertyChanged
Friend Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
Friend Sub NotifyPropertyChanged(<CallerMemberName()> Optional propertyName As String = Nothing)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
End Class
Public Class Customer
Inherits BaseNotify
Private _name As String = "DEFAULT"
Public Property Name As String
Get
Return _name
End Get
Set
If (_name = Value) Then Return
_name = Value
NotifyPropertyChanged()
End Set
End Property
End Class
Finally set up the form (I also renamed the textbox to something more meaningful.
Public Class Form1
Private _customer As Customer
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
BindProperties()
End Sub
Private Sub BindProperties()
_customer = New Customer()
Me.tbName.DataBindings.Add("Text", _customer, NameOf(Customer.Name))
End Sub
End Class
Using NameOf is recommended, as it won't break the code if you decide to change the property name at a later stage.
I have written my on editor for a property in a propertyGrid. Everything works as expected, but for best user experience i want to hide the expand button on the propertygrid for this property. Can someone tell me how to do that?
I am using a custom typeconverter
Public Class PropertyLanguageSetupEditor
Inherits UITypeEditor
Public Overrides Function GetEditStyle(context As ITypeDescriptorContext) As UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function
Public Overrides Function EditValue(context As ITypeDescriptorContext, provider As IServiceProvider, value As Object) As Object
Try
Dim svc As IWindowsFormsEditorService = CType(provider.GetService(GetType(IWindowsFormsEditorService)), IWindowsFormsEditorService)
Dim settings As LanguageSettings = value
Dim oldSettings As LanguageSettings = CType(value, LanguageSettings).Clone
If Not svc Is Nothing And Not settings Is Nothing Then
Dim form As New PropertyLanguageSetupWindow(settings)
If svc.ShowDialog(form) = DialogResult.OK Then
value = form.Data
Else
value = oldSettings
End If
End If
Catch ex As Exception
Debug.WriteLine(ex.Message)
End Try
Return MyBase.EditValue(context, provider, value)
End Function
End Class
And this is de property which uses the typeConverter
<DisplayName("Properties Translation Settings"), Editor(GetType(PropertyLanguageSetupEditor), GetType(System.Drawing.Design.UITypeEditor)), TypeConverter(GetType(BrowsableTypeConverter)),
Description("Settings for the output text"), BrowsableTypeConverter.BrowsableLabelStyleAttribute(BrowsableTypeConverter.LabelStyle.lsEllipsis),
Category("10 - DXF Export"), Browsable(True)>
Public Property TranslationSettings As LanguageSettings = New LanguageSettings
My BrowsAbleTypeConverter
Public Class BrowsableTypeConverter
Inherits ExpandableObjectConverter
Public Enum LabelStyle
lsNormal
lsTypeName
lsEllipsis
lsText
End Enum
Public Class BrowsableLabelStyleAttribute
Inherits System.Attribute
Private eLabelStyle As LabelStyle = LabelStyle.lsEllipsis
Public Sub New(ByVal LabelStyle As LabelStyle)
eLabelStyle = LabelStyle
End Sub
Public Property LabelStyle() As LabelStyle
Get
Return eLabelStyle
End Get
Set(ByVal value As LabelStyle)
eLabelStyle = value
End Set
End Property
End Class
Public Class BrowsableLabelTextAttribute
Inherits System.Attribute
Private strText As String = ""
Public Sub New(ByVal value As String)
strText = value
End Sub
Public Property Text() As String
Get
Return strText
End Get
Set(ByVal value As String)
strText = value
End Set
End Property
End Class
Public Overrides Function CanConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal destinationType As System.Type) As Boolean
Return True
End Function
Public Overrides Function ConvertTo(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal culture As System.Globalization.CultureInfo, ByVal value As Object, ByVal destinationType As System.Type) As Object
Dim Style As BrowsableLabelStyleAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelStyleAttribute))
If Not Style Is Nothing Then
Select Case Style.LabelStyle
Case LabelStyle.lsNormal
Return MyBase.ConvertTo(context, culture, value, destinationType)
Case LabelStyle.lsTypeName
Return "(" & value.GetType.Name & ")"
Case LabelStyle.lsEllipsis
Return "(...)"
Case LabelStyle.lsText
Dim text As BrowsableLabelTextAttribute = context.PropertyDescriptor.Attributes(GetType(BrowsableLabelTextAttribute))
If text IsNot Nothing Then
Return text.Text
End If
End Select
End If
Return MyBase.ConvertTo(context, culture, value, destinationType)
End Function
End Class
Your TypeConverter derives from ExpandableObjectConverter, which means subproperties will be visible because of its GetPropertiesSupported method returning true.
In your TypeConverter, you simply need to override GetPropertiesSupported and return false.
Not an experienced programmer, so probably not a hard question.
Developing a small application in VB.net in WPF.
I made 3 classes, EngineeringObject<==Inherits==PartOfInstallation<==Inherits==SensorActor
In the class SensorActor I'm trying to get a property of PartOfInstallation with the function MyBase.Name. But this goes directly to EngineeringObject. How do I solve this?
Public Class EngineeringObject
''Private declarations, alleen objecten die erven kunnen hieraan, of dmv van getters en setters
'Name of part
Private sName As String = "Naam"
'81346 Id's
Private sSystemId As String = "Functie" 'VentilationSystem, Pumpsystem
Private sLocationId As String = "Locatie" 'Room 0.0
Private sObjectId As String = "Object" 'Fan, Pump
'General
Private sPartNumber As String
Private sLinkToDatasheet As String
'Property's
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal value As String)
sName = value
End Set
End Property
Public Property SystemId() As String
Get
Return sSystemId
End Get
Set(ByVal value As String)
sSystemId = value
End Set
End Property
Public Property PartNumber() As String
Get
Return sPartNumber
End Get
Set(ByVal value As String)
sPartNumber = value
End Set
End Property
Public Property LinkToDatasheet() As String
Get
Return sLinkToDatasheet
End Get
Set(ByVal value As String)
sLinkToDatasheet = value
End Set
End Property
Public Sub New()
End Sub
End Class
Public Class PartOfInstallation
Inherits EngineeringObject
'src: https://stackoverflow.com/questions/21308881/parent-creating-child-object
'src: https://stackoverflow.com/questions/16244548/how-to-create-a-list-of-parent-objects-where-each-parent-can-have-a-list-of-chil
Private lSensorActor As New List(Of SensorActor)
Public Function GetSensorActor()
Return Me.lSensorActor
End Function
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor)
End Sub
End Class
Public Class SensorActor
Inherits PartOfInstallation
Dim sMyPartOfInstallation As String
Public Property MyPartOfInstallation As String
Get
Return sMyPartOfInstallation
End Get
Set(value As String)
sMyPartOfInstallation = MyBase.Name
End Set
End Property
End Class
If I understand it correctly, based on your comments, you want every SensorActor instantiated within a PartOfInstallation instance to get the same name as that instance.
If so, then just add a second constructor to your SensorActor class allowing you to pass a name for it as well:
Public Class SensorActor
Inherits PartOfInstallation
...your code...
Public Sub New() 'Empty constructor, for if/when you don't want to set the name immediately.
End Sub
Public Sub New(ByVal Name As String)
Me.Name = Name
End Sub
End Class
Now in your PartOfInstallation class you can do:
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me.Name)) 'Here, "Me" refers to the current PartOfInstallation instance.
End Sub
Alternatively you can make the SensorActor constructor take a PartOfInstallation instance instead, allowing you to copy any properties you like:
Public Class SensorActor
Inherits PartOfInstallation
...your code...
Public Sub New()
End Sub
Public Sub New(ByVal BasedOnPOI As PartOfInstallation)
Me.Name = BasedOnPOI.Name
End Sub
End Class
Thus making the code in the PartOfInstallation class:
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me))
End Sub
Read more about constructors: Object Lifetime: How Objects Are Created and Destroyed (Visual Basic) | Microsoft Docs
The result below, if there's room for improvement... always welcome.
SensorActor
Public Class SensorActor
Inherits PartOfInstallation
Dim sTemp As String
Public Overloads Property SystemId() As String
Get
Return Me.sSystemId
End Get
Set(ByVal value As String)
Me.sSystemId = sTemp + "." + value
End Set
End Property
Public Sub New(ByVal BasedOnPOI As PartOfInstallation)
sTemp = BasedOnPOI.SystemId
End Sub
End Class
PartOfInstallation
Public Class PartOfInstallation
Inherits EngineeringObject
'src: https://stackoverflow.com/questions/21308881/parent-creating-child-object
'src: https://stackoverflow.com/questions/16244548/how-to-create-a-list-of-parent-objects-where-each-parent-can-have-a-list-of-chil
Private lSensorActor As New List(Of SensorActor)
Public Function GetSensorActor()
Return Me.lSensorActor
End Function
Public Sub CreateSensorActor()
lSensorActor.Add(New SensorActor(Me))
End Sub
End Class
EngineeringObject
Public Class EngineeringObject
''Private declarations, alleen objecten die erven kunnen hieraan, of dmv van getters en setters
'Name of part
Private sName As String = "Naam"
'81346 Id's
Friend sSystemId As String = "Functie" 'VentilationSystem, Pumpsystem
Private sLocationId As String = "Locatie" 'Room 0.0
Private sObjectId As String = "Object" 'Fan, Pump
'General
Private sPartNumber As String
Private sLinkToDatasheet As String
'Property's
Public Property Name() As String
Get
Return sName
End Get
Set(ByVal value As String)
sName = value
End Set
End Property
Public Property SystemId() As String
Get
Return sSystemId
End Get
Set(ByVal value As String)
sSystemId = "=" + value
End Set
End Property
Public Property PartNumber() As String
Get
Return sPartNumber
End Get
Set(ByVal value As String)
sPartNumber = value
End Set
End Property
Public Property LinkToDatasheet() As String
Get
Return sLinkToDatasheet
End Get
Set(ByVal value As String)
sLinkToDatasheet = value
End Set
End Property
Public Sub New()
End Sub
End Class
I'm not able to remove an object from my List (of contact)
Here are my fields:
Public Class Contact
'Things to remember
Private m_firstName As String = String.Empty
Private m_lastName As String = String.Empty
Private m_address As Address
My list:
Public Class ContactManager
Private m_contactRegistry As List(Of Contact)
Public Sub New()
m_contactRegistry = New List(Of Contact)()
End Sub
My method in ContactManger Class. Here I'm getting error "Value of type 'Integer' cannot be converted to Assignment.Contact" on the index
Public Function DeleteContact(index As Integer) As Boolean
m_contactRegistry.Remove(index)
Return True
End Function
My delete button method on my Main class:
Private Sub btnRemove_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
'listResults is my listbox
Dim list = listResults.SelectedIndex
'm_contact is an object of the Contact class
m_contacts.DeleteContact(list)
UpdateGUI()
End Sub
The problem is that I don't know how to do the method DeleteContact(index As Integer) without getting an error. Do you guys have a suggestion?
When using an index, you need RemoveAt() rather than Remove()
I have a class that add extra information about a column for linq2sql (see code below)
right now, I have to explicitly tell what column I want that info on, how would you put that code with a loop on every column in every table from a dbml file?
I was doing my test on a very very small DB, now I have to implement it on a much more bigger database and I really don't want to do it manually for every tables/columns
it will take hours.
how it's being used:
Partial Class Contact ''contact is a table inside a dbml file.
Private _ContactIDColumn As ExtraColumnInfo
Private _ContactNameColumn As ExtraColumnInfo
Private _ContactEmailColumn As ExtraColumnInfo
Private _ContactTypeIDColumn As ExtraColumnInfo
Private Sub OnCreated()
initColumnInfo()
End Sub
Private Sub initColumnInfo()
Dim prop As PropertyInfo
prop = Me.GetType.GetProperty("ContactID")
_ContactIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactName")
_ContactNameColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactEmail")
_ContactEmailColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Me.GetType.GetProperty("ContactTypeID")
_ContactTypeIDColumn = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
prop = Nothing
End Sub
Public ReadOnly Property ContactIDColumn() As ExtraColumnInfo
Get
Return _ContactIDColumn
End Get
End Property
Public ReadOnly Property ContactNameColumn() As ExtraColumnInfo
Get
Return _ContactNameColumn
End Get
End Property
Public ReadOnly Property ContactEmailColumn() As ExtraColumnInfo
Get
Return _ContactEmailColumn
End Get
End Property
Public ReadOnly Property ContactTypeIDColumn() As ExtraColumnInfo
Get
Return _ContactTypeIDColumn
End Get
End Property
End Class
what is ExtraColumnInfo:
Public Class ExtraColumnInfo
Private _column As ColumnAttribute
Private _MaxLength As Nullable(Of Integer)
Private _Precision As Nullable(Of Integer)
Private _Scale As Nullable(Of Integer)
Private _IsIdentity As Boolean
Private _IsUniqueIdentifier As Boolean
Sub New(ByVal column As ColumnAttribute)
Dim match As Match
_column = column
Match = Regex.Match(DBType, "^.*\((\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_MaxLength = If(match.Success, Convert.ToInt32(match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Precision = If(Match.Success, Convert.ToInt32(Match.Groups(1).Value), Nothing)
match = Regex.Match(DBType, "^.*\((\d+),(\d+)\).*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_Scale = If(match.Success, Convert.ToInt32(match.Groups(2).Value), Nothing)
match = Regex.Match(DBType, "^.*\bIDENTITY\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsIdentity = match.Success
match = Regex.Match(DBType, "^.*\bUniqueIdentifier\b.*$", RegexOptions.Compiled Or RegexOptions.IgnoreCase)
_IsUniqueIdentifier = match.Success
End Sub
'others available information
'AutoSync
'Expression
'IsVersion
'Name
'Storage
'TypeId
'UpdateCheck
'maybe more
Public ReadOnly Property CanBeNull() As Boolean
Get
Return _column.CanBeNull
End Get
End Property
Public ReadOnly Property IsDbGenerated() As Boolean
Get
Return _column.IsDbGenerated
End Get
End Property
Public ReadOnly Property IsPrimaryKey() As Boolean
Get
Return _column.IsPrimaryKey
End Get
End Property
Public ReadOnly Property DBType() As String
Get
Return _column.DbType
End Get
End Property
Public ReadOnly Property MaxLength() As System.Nullable(Of Integer)
Get
Return _MaxLength
End Get
End Property
Public ReadOnly Property Precision() As System.Nullable(Of Integer)
Get
Return _Precision
End Get
End Property
Public ReadOnly Property Scale() As System.Nullable(Of Integer)
Get
Return _Scale
End Get
End Property
Public ReadOnly Property IsIdentity() As Boolean
Get
Return _IsIdentity
End Get
End Property
Public ReadOnly Property IsUniqueIdentifier() As Boolean
Get
Return _IsUniqueIdentifier
End Get
End Property
End Class
The only thing that comes to mind, and it's a far from optimal solution, would be to include this functionality into a base class and declare partials for all of the generated classes so that they inherit from your base class.
I'm sure there are also other ways to do this, but none that I know of that are native to the designer.
I took the T4 template and modified it a little to automatically put inside the generated class the column information,
in the import I added
Imports System.Reflection
Imports System.Text.RegularExpressions
at line 228, added these line:
<# foreach(Column column in class1.Columns) {#>
Private _<#=column.Member#>Column As ExtraColumnInfo
Public ReadOnly Property <#=column.Member#>Column() As ExtraColumnInfo
Get
Return _<#=column.Member#>Column
End Get
End Property
<# }#>
at line 298 I added
Dim prop As PropertyInfo
<# foreach(Column column in class1.Columns) {#>
prop = Me.GetType.GetProperty("<#=column.Member#>")
_<#=column.Member#>Column = New ExtraColumnInfo(DirectCast(prop.GetCustomAttributes(GetType(ColumnAttribute), False)(0), ColumnAttribute))
<# }#>
at the end of the file, I added my own class