473,503 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

propertygrid listbox and setting the selectedvalue, help

Hi,
I am strugling with the propertygrid and a listbox. I am using the
universaldropdowneditor from the codeproject (code below). However I am
populating the listbox via a datasource. The problem I am having is that when
I have a value in the propertygird and edit that, I want the listbox to have
the selectvalue equal to the value that is being edited. Just to make it
clearer:

PropgridVal = Germany
Datasource=
Belgium
France
Italie
Germany

Currently when I edit a value it defaults directly to Belgium, instead of
Germany

I do not understand, when I use listbox.selectedvalue="Germany" the
selectedvalue remains Nothing

please help

Thankks

David J

#Region " UniversalDropdownEditor class "

Public Class UniversalDropdownEditor
Inherits UITypeEditor
Private edSvc As IWindowsFormsEditorService
Private valMemb As ValueMemberAttribute

Public Overloads Overrides Function GetEditStyle(ByVal context As _
ITypeDescriptorContext) As UITypeEditorEditStyle
If Not context Is Nothing AndAlso Not context.Instance Is Nothing Then
Return UITypeEditorEditStyle.DropDown
End If
Return UITypeEditorEditStyle.None
End Function

<RefreshProperties(RefreshProperties.All)> _
Public Overloads Overrides Function EditValue( _
ByVal context As ITypeDescriptorContext, _
ByVal provider As System.IServiceProvider, _
ByVal value As [Object]) As [Object]
If context Is Nothing OrElse provider Is Nothing _
OrElse context.Instance Is Nothing Then
Return MyBase.EditValue(provider, value)
End If
' Dim att As SourceCollectionAttribute = _
' context.PropertyDescriptor.Attributes( _
' GetType(SourceCollectionAttribute))
'If att Is Nothing Then
' nothing we can do here
'Return MyBase.EditValue(provider, value)
'End If
Me.edSvc = provider.GetService(GetType(IWindowsFormsEditorSer vice))
'If Me.edSvc Is Nothing Then
' nothing we can do here either
'Return MyBase.EditValue(provider, value)
'End If

'prepare the listbox
Dim lst As New ListBox()
Me.PrepareListBox(lst, context)
If Me.valMemb Is Nothing Then
lst.SelectedItem = value

Else
Me.valMemb.SelectByValue(lst, value)
End If
Me.switchloaded = False

Me.edSvc.DropDownControl(lst)
Me.switchloaded = True
' we're back
If lst.SelectedItem Is Nothing Then
value = Nothing
Else
value = lst.Text
'Else
' value = Me.valMemb.GetValue(lst)
End If
Return value
End Function

Private Sub PrepareListBox(ByVal lst As ListBox, _
ByVal context As ITypeDescriptorContext)
Dim SysSetTypeAttr As PropertySysSetTypeAttribute =
context.PropertyDescriptor.Attributes(GetType(Prop ertySysSetTypeAttribute))
Dim syssetType As Short = SysSetTypeAttr.PropertySysSetTypeAttribute
lst.IntegralHeight = True ' resize to avoid partial items
Dim coll As DataTable =
funGetPropVal(context.PropertyDescriptor.Name, SysSetType)
If lst.ItemHeight > 0 Then
Dim adjHei As Integer = (coll.Rows.Count + 1) * lst.ItemHeight
If Not coll Is Nothing AndAlso _
lst.Height / lst.ItemHeight < coll.Rows.Count Then
' try to keep the listbox small but sufficient
If adjHei > 200 Then adjHei = 200
End If
lst.Height = adjHei

Else ' safeguard, although it shouldn't happen
lst.Height = lst.ItemHeight
End If
lst.Sorted = True ' present in alphabetical order
FillListBoxFromCollection(lst, coll)
Me.AssignValueMember(lst, context.PropertyDescriptor)
Me.AssignDisplayMember(lst, context.PropertyDescriptor)
' attach event handler
AddHandler lst.SelectedIndexChanged, AddressOf Me.handleSelection
AddHandler lst.DoubleClick, AddressOf Me.handledoubleclick
End Sub

Public Shared Sub FillListBoxFromCollection(ByVal lb As ListBox, ByVal
coll As DataTable)
Dim DataRowX As DataRow
' prevent flickers and slow downs by entering the mass update mode
lb.BeginUpdate()
lb.Items.Clear()
lb.MultiColumn = False
Dim intCount As Integer
lb.DataSource = coll
lb.Invalidate()
End Sub

Private Sub AssignValueMember(ByVal lc As ListControl, ByVal pd As
PropertyDescriptor)
Dim Attr As ValueMemberAttribute = New ValueMemberAttribute("Name")
Me.valMemb = Attr 'pd.Attributes(GetType(ValueMemberAttribute))
lc.ValueMember = Me.valMemb.ValuePropertyName
End Sub

Private Sub AssignDisplayMember(ByVal lc As ListControl, ByVal pd As
PropertyDescriptor)
Dim att As DisplayMemberAttribute = New DisplayMemberAttribute("Name")
If att Is Nothing Then Return
lc.DisplayMember = att.DisplayPropertyName
End Sub

Private Sub handleSelection(ByVal sender As Object, ByVal e As EventArgs)
If Me.edSvc Is Nothing Then Return
If Me.switchloaded = False Then Return
Me.edSvc.CloseDropDown()
End Sub
Private Sub handledoubleclick(ByVal sender As Object, ByVal e As
EventArgs)
If Me.edSvc Is Nothing Then Return
Me.edSvc.CloseDropDown()
End Sub
End Class

#End Region

#Region " Attributes "

#Region " Auxiliary attribute that fetches the specified source collection "

<Description("Service attribute to point to the source collection."), _
AttributeUsage(AttributeTargets.All)> _
Public Class SourceCollectionAttribute
Inherits Attribute
Private srcCollName As String

Public ReadOnly Property CollectionName() As String
Get
Return Me.srcCollName
End Get
End Property

Public ReadOnly Property Collection(ByVal instance As Object) As
ICollection
Get
Dim pdc As PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(instance)
Dim pd As PropertyDescriptor
For Each pd In pdc
If pd.Name = Me.srcCollName Then
Return pd.GetValue(instance)
End If
Next
Return Nothing
End Get
End Property

Public Sub New(ByVal sourceCollectionPropertyName As String)
Me.srcCollName = sourceCollectionPropertyName
End Sub
End Class

#End Region

#Region " 'Value member' attribute "
<AttributeUsage(AttributeTargets.All)> _
Public Class ValueMemberAttribute
Inherits Attribute
Private valMemb As String

<Description("The name of the property used as value member by the
dynamic combo type editor.")> _
Public ReadOnly Property ValuePropertyName() As String
Get
Return Me.valMemb
End Get
End Property

Public Sub SelectByValue(ByVal lb As ListBox, ByVal val As Object)
lb.SelectedItem = Nothing
Dim item As Object
If Not val Is Nothing Then
For Each item In lb.Items
If Me.GetValue(item) = val Then
lb.SelectedItem = item
Exit Sub
End If
Next
End If
End Sub

Public Function GetValue(ByVal obj As Object) As Object
If Me.valMemb = String.Empty Then Return obj
' Dim pi As System.Reflection.PropertyInfo =
obj.GetType().GetProperty("Row")
Dim pi As String = obj.text
If pi Is Nothing Then Return obj
Return pi
End Function

Public Sub New(ByVal valueMemberPropertyName As String)
Me.valMemb = valueMemberPropertyName
End Sub
End Class
#End Region

#Region " 'Display member' attribute "

<AttributeUsage(AttributeTargets.All)> _
Public Class DisplayMemberAttribute
Inherits Attribute
Private dispMemb As String

<Description("The property displayed in the list control used by the
dynamic combo editor.")> _
Public ReadOnly Property DisplayPropertyName() As String
Get
Return Me.dispMemb
End Get
End Property

Public Sub New(ByVal displayMemberPropertyName As String)
Me.dispMemb = displayMemberPropertyName
End Sub
End Class

Nov 21 '05 #1
0 2630

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1959
by: Alpha | last post by:
I have a window application. On one of the form, there is a listbox and a few combox. The lstSchItem has a dataview as a datasource. The comboxes are bind to its selected value but through the...
4
2425
by: Moe Sizlak | last post by:
Hi There, I am trying to return the value of a listbox control that is included as a user control, I can return the name of the control but I can't access the integer value of the selected item,...
4
4189
by: dtblankenship | last post by:
Hello everyone, I know this question has been asked many times in the forums, and after spending a few days reading, I am still confused as to the answer. I have a ListBox (lstBox),...
8
1465
by: gv | last post by:
Ok, Hi all, new to asp.net. Simple question with web forms I have a Dropdownlist and a listbox, I want to click on and item in the dropdownlist and show items in listbox. that simple I...
5
2273
by: Doug Bell | last post by:
Hi, I thought that this would be a simple exercise but I am struggling to get a result. I need a listbox that will display a list of items from a DataTable, dtUnits. The displayed list comes from...
1
2754
by: A. Spiehler | last post by:
I'm trying to fill a listBox control with string members from an array of objects. I think using data binding is supposed to be the easiest way to do this. I've never used data binding before and...
2
9786
by: Pat | last post by:
I'm a newbie to c# and could use some help - been banging my head on the keyboard for 3 days now. I have an unbound listbox that I'm populating this way: 1. loop through a datatable and load...
2
5520
by: =?Utf-8?B?U3RlcGhlbiBSaXRjaGll?= | last post by:
Hi NET1.1 / Winforms I have a listbox that I am binding to a table via the DataSource property. However I want to be able to programmatically select values in this listbox so I am using the...
1
2218
by: csharpula csharp | last post by:
Hello, I have the folloing problem: A PropertyGrid which suppose to be binded to listbox is not binded well. I am doing such thing in the form constructor: ...
1
6998
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7464
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5586
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5018
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4680
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3171
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3162
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1516
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
391
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.