Category: usercontrols

Jan 27 2011

How do you create Winform user control that supports multiple items like a listbox.

Hello,

I think I understand the basics of create winforms user control. I can do Add New and select user control and that will give me a design surface and designer file and code-behind file etc. And then I could change it to inherit from something other than UserControl (like listbox or text box).

The thing that is different for me this time is the user control needs to be a list control (like a list box) that can be data-bound and with two data-bindable controls in it (two combo boxes).

I am not sure where to start? Can anybody reference a document or some obvious bit in the tooling that I am missing that will make it quick and easy? Any approaches I can take like extending an existing control. Any gotchas I need to be aware of?

Thanks.

Seth

Jan 27 2011

How do you create Winform user control that supports multiple items like a listbox

I think I understand the basics of create winforms user control. I can do Add New and select user control and that will give me a design surface and designer file and code-behind file etc. And then I could change it to inherit from something other than UserControl (like listbox or text box).

The thing that is different for me this time is the user control needs to be a list control (like a list box) that can be data-bound and with two data-bindable controls in it (two combo boxes).

I am not sure where to start? Can anybody reference a document or some obvious bit in the tooling that I am missing that will make it quick and easy? Any approaches I can take like extending an existing control. Any gotchas I need to be aware of?

Thanks.

Seth

EDIT
I have decided to take a different approach. I am leaving the question up, though. This is clearly difficulty than I had guessed. WPF is not an option for me unfortunately. Just gonna use a data grid.

Sep 04 2009

Listbox ClearSelected method leaves items selected

Hello,

I have several winforms listbox controls I am hosting in a user control. The behavior I want in this user control is that I only want ONE of the listboxes to have a selected item at a time.

So if I have an item in Listbox1 selected and I click an item in listbox2 listbox one should be automatically have all items deselected.

I have tried to accomplish this with the following code:

   dim listBox as listbox
   For Each listName As String In _listboxes.Keys
        If listName <> listboxName Then
            listbox = me.controls(listName)
            listbox.ClearSelected()
        End If
    Next

I am storing the names of the listboxes in a dictionary. (The number of listboxes is dynamic). listBoxName in this example is the listbox that was just click so this code avoids clearing the selection in that box.

The behaviour I get when I run this code is very unexpected. Lets say I have 3 listboxes...listbox1, listbox2, and listbox3. Let's say that item3 in listbox2 is currently selected. If I click item 4 of listbox1 then the end result of this is that item four in listbox1 is selected (as expected) but listbox2 has item 1 selected.

In short, the listBox that previously had an item select still has item 1 selected rather than being cleared of selections.

I emitted a debug.print listBox.SelectedItems.Count immediately after the ClearSelection method call and sure enough it says that 1 items is selected.

Any thoughts on how to do this or fix my code?

Seth

EDIT: BTW, I am binding the listboxes to business object collections.

Sep 04 2009

Listbox ClearSelected method leaves items selected

I have several winforms listbox controls I am hosting in a user control. The behavior I want in this user control is that I only want ONE of the listboxes to have a selected item at a time.

So if I have an item in Listbox1 selected and I click an item in listbox2 listbox one should be automatically have all items deselected.

I have tried to accomplish this with the following code:

   dim listBox as listbox
   For Each listName As String In _listboxes.Keys
        If listName <> listboxName Then
            listbox = me.controls(listName)
            listbox.ClearSelected()
        End If
    Next

I am storing the names of the listboxes in a dictionary. (The number of listboxes is dynamic). listBoxName in this example is the listbox that was just click so this code avoids clearing the selection in that box.

The behaviour I get when I run this code is very unexpected. Lets say I have 3 listboxes...listbox1, listbox2, and listbox3. Let's say that item3 in listbox2 is currently selected. If I click item 4 of listbox1 then the end result of this is that item four in listbox1 is selected (as expected) but listbox2 has item 1 selected.

In short, the listBox that previously had an item select still has item 1 selected rather than being cleared of selections.

I emitted a debug.print listBox.SelectedItems.Count immediately after the ClearSelection method call and sure enough it says that 1 items is selected.

Any thoughts on how to do this or fix my code?

Seth

EDIT: BTW, I am binding the listboxes to business object collections.

Jul 13 2009

Is it OK to change a winforms designer file?

I have created a class that is simply THIS

Class UserControlBase
     Inherits UserControl
End Class

Then I changed the Inherits clause in each of my UserControls designer file to

   Inherits UserControlBase

I know that generally you shouldn't manually mod the designer file. But in cases like this what else can you do? Is this OK? Is there a best practice I don't know about? Is there some other way to accomplish the same end (extending UserControl) ?

Jul 03 2009

How to expose and raise custom events for a vb.net winforms user control

Hello,

Please read THIS post. I have the same problem as described in this post but I am trying to do in in VB.net rather than c#.

I am pretty sure to do this I have to use a custom event. (I used a code conversion site to get to learn about custom events.) So in the IDE when I type the following:

Public Custom Event AddRemoveAttendees As EventHandler

It expands to the following code snippet.

Public Custom Event AddRemoveAttendees As EventHandler
    AddHandler(ByVal value As EventHandler)

    End AddHandler

    RemoveHandler(ByVal value As EventHandler)

    End RemoveHandler

    RaiseEvent(ByVal sender As Object, ByVal e As System.EventArgs)

    End RaiseEvent
End Event

But I can't figure out what to do with it. Until today I had never heard of custom events.

The bottom line of what I want is to have the click event of a button bubble up to the container of the user control. I know that I could wrap my own event but I would at least like to understand custom events before I go farther down that road.

Seth