473,549 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

propertygrid listbox and setting the selectedvalue, help

I am strugling with the propertygrid and a listbox. I am using the
universaldropdo wneditor 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

PropgridVal = Germany

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

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

please help


David J

#Region " UniversalDropdo wnEditor class "

Public Class UniversalDropdo wnEditor
Inherits UITypeEditor
Private edSvc As IWindowsFormsEd itorService
Private valMemb As ValueMemberAttr ibute

Public Overloads Overrides Function GetEditStyle(By Val context As _
ITypeDescriptor Context) As UITypeEditorEdi tStyle
If Not context Is Nothing AndAlso Not context.Instanc e Is Nothing Then
Return UITypeEditorEdi tStyle.DropDown
End If
Return UITypeEditorEdi tStyle.None
End Function

<RefreshPropert ies(RefreshProp erties.All)> _
Public Overloads Overrides Function EditValue( _
ByVal context As ITypeDescriptor Context, _
ByVal provider As System.IService Provider, _
ByVal value As [Object]) As [Object]
If context Is Nothing OrElse provider Is Nothing _
OrElse context.Instanc e Is Nothing Then
Return MyBase.EditValu e(provider, value)
End If
' Dim att As SourceCollectio nAttribute = _
' context.Propert yDescriptor.Att ributes( _
' GetType(SourceC ollectionAttrib ute))
'If att Is Nothing Then
' nothing we can do here
'Return MyBase.EditValu e(provider, value)
'End If
Me.edSvc = provider.GetSer vice(GetType(IW indowsFormsEdit orService))
'If Me.edSvc Is Nothing Then
' nothing we can do here either
'Return MyBase.EditValu e(provider, value)
'End If

'prepare the listbox
Dim lst As New ListBox()
Me.PrepareListB ox(lst, context)
If Me.valMemb Is Nothing Then
lst.SelectedIte m = value

Me.valMemb.Sele ctByValue(lst, value)
End If
Me.switchloaded = False

Me.edSvc.DropDo wnControl(lst)
Me.switchloaded = True
' we're back
If lst.SelectedIte m Is Nothing Then
value = Nothing
value = lst.Text
' value = Me.valMemb.GetV alue(lst)
End If
Return value
End Function

Private Sub PrepareListBox( ByVal lst As ListBox, _
ByVal context As ITypeDescriptor Context)
Dim SysSetTypeAttr As PropertySysSetT ypeAttribute =
context.Propert yDescriptor.Att ributes(GetType (PropertySysSet TypeAttribute))
Dim syssetType As Short = SysSetTypeAttr. PropertySysSetT ypeAttribute
lst.IntegralHei ght = True ' resize to avoid partial items
Dim coll As DataTable =
funGetPropVal(c ontext.Property Descriptor.Name , SysSetType)
If lst.ItemHeight > 0 Then
Dim adjHei As Integer = (coll.Rows.Coun t + 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
FillListBoxFrom Collection(lst, coll)
Me.AssignValueM ember(lst, context.Propert yDescriptor)
Me.AssignDispla yMember(lst, context.Propert yDescriptor)
' attach event handler
AddHandler lst.SelectedInd exChanged, AddressOf Me.handleSelect ion
AddHandler lst.DoubleClick , AddressOf Me.handledouble click
End Sub

Public Shared Sub FillListBoxFrom Collection(ByVa l 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
End Sub

Private Sub AssignValueMemb er(ByVal lc As ListControl, ByVal pd As
PropertyDescrip tor)
Dim Attr As ValueMemberAttr ibute = New ValueMemberAttr ibute("Name")
Me.valMemb = Attr 'pd.Attributes( GetType(ValueMe mberAttribute))
lc.ValueMember = Me.valMemb.Valu ePropertyName
End Sub

Private Sub AssignDisplayMe mber(ByVal lc As ListControl, ByVal pd As
PropertyDescrip tor)
Dim att As DisplayMemberAt tribute = New DisplayMemberAt tribute("Name")
If att Is Nothing Then Return
lc.DisplayMembe r = att.DisplayProp ertyName
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.CloseD ropDown()
End Sub
Private Sub handledoublecli ck(ByVal sender As Object, ByVal e As
If Me.edSvc Is Nothing Then Return
Me.edSvc.CloseD ropDown()
End Sub
End Class

#End Region

#Region " Attributes "

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

<Description("S ervice attribute to point to the source collection."), _
AttributeUsage( AttributeTarget s.All)> _
Public Class SourceCollectio nAttribute
Inherits Attribute
Private srcCollName As String

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

Public ReadOnly Property Collection(ByVa l instance As Object) As
Dim pdc As PropertyDescrip torCollection = _
TypeDescriptor. GetProperties(i nstance)
Dim pd As PropertyDescrip tor
For Each pd In pdc
If pd.Name = Me.srcCollName Then
Return pd.GetValue(ins tance)
End If
Return Nothing
End Get
End Property

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

#End Region

#Region " 'Value member' attribute "
<AttributeUsage (AttributeTarge ts.All)> _
Public Class ValueMemberAttr ibute
Inherits Attribute
Private valMemb As String

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

Public Sub SelectByValue(B yVal 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(ite m) = val Then
lb.SelectedItem = item
Exit Sub
End If
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.Reflecti on.PropertyInfo =
obj.GetType().G etProperty("Row ")
Dim pi As String = obj.text
If pi Is Nothing Then Return obj
Return pi
End Function

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

#Region " 'Display member' attribute "

<AttributeUsage (AttributeTarge ts.All)> _
Public Class DisplayMemberAt tribute
Inherits Attribute
Private dispMemb As String

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

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

Nov 21 '05 #1
0 2635

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

Similar topics

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 dataset table. Now when I change the selection in the lstSchItem the comboxes are staying the same and not reflecting the changes. I tried...
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, what do I need to do in order to return the "option value" of the control? Moe !--- returned value of the control
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), SqlConnection (sqlConnection), SqlDataAdapter (daLookupData), SqlDataAdapter (daData), DataSet (dsLookupData), and DataSet (dsData), all created via the IDE...
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 have this: I have two items in Dropdownlist. The Reportlist doesn't show
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 Column . Selecting an item in this list needs to return a value from Column . Data in dtUnits is UnitsProdDescn UnitsConvn Carton ...
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 am having trouble getting it to do anything. The relevant code is below, followed by a better explanation of what I'm trying to do. // pseudocode...
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 row into row object 2. Check one of the row columns against a dictionary key 3. If it's found, add it to the listbox. Then I want to pass the...
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 code below... With lstJobTypes .DataSource = BusinessRules.Common.JobTypes.All(True)
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: propGrid.SelectedObject=lbSelectedItems.SelectedValue; and this is not really binded ,cause when I select different value in listbox,there is no change on PropertyGrid. How can I...
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.