Better way to load user controls into panel in Windows app?

R

Ronald S. Cook

I lost my last post, but I'm looking for a better way to manage loading user
controls (that are essentially forms) in a Windows app.

In my current design, I have a ListBar in which the user selects an item. I
then call a sub to loop through and remove any user control that may exist
in the panel. But then I have a wordy select statement to load up the user
control selected.

Isn't there a better way?

Thanks,
Ron
Private Sub ulbNav_ItemSelected(ByVal sender As System.Object, ByVal e As
Infragistics.Win.UltraWinListBar.ItemEventArgs) Handles ulbNav.ItemSelected

RemoveExitingUserControls()

Select Case e.Item.Key

Case "keyTesting_ViewCountryList"

If _uctTesting_ViewCountryList Is Nothing Then

_uctTesting_ViewCountryList = New uctTesting_ViewCountryList

_uctTesting_ViewCountryList.Parent = splContainer.Panel2

End If

Case "keyTesting_SQLReportViewer"

If _uctTesting_SQLReportViewer Is Nothing Then

_uctTesting_SQLReportViewer = New uctTesting_SQLReportViewer

_uctTesting_SQLReportViewer.Parent = splContainer.Panel2

End If

Case "keyTesting_CalculateDrivingDistance"

If _uctTesting_CalculateDrivingDistance Is Nothing Then

_uctTesting_CalculateDrivingDistance = New
uctTesting_CalculateDrivingDistance

_uctTesting_CalculateDrivingDistance.Parent = splContainer.Panel2

End If

End Select

End Sub
 
J

jayeldee

I lost my last post, but I'm looking for a better way to manage loading user
controls (that are essentially forms) in a Windows app.

In my current design, I have a ListBar in which the user selects an item. I
then call a sub to loop through and remove any user control that may exist
in the panel. But then I have a wordy select statement to load up the user
control selected.

Isn't there a better way?

In your RemoveExitingUserControls() sub, you could just do a
myPanel.Controls.Clear to remove the existing User Controls on the
panel, then do the myPanel.Controls.Add() for the next user control.

To avoid the messy select statement, you could use a custom class to
house each item in the ListView and initialize them during the form
load.

I'm not familiar with the type of List Control you're using, but I'm
assuming it behaves a bit like a ListBox and you can add custom items
to it and that it will call the ToString of your custom class to
figure out what text to display.

Class would look like:

Public Class MyListItem
Public Name As String
Public Control As Control

Public Overrides Function ToString() As String
Return Name
End Function
End Class

Form Code would look like:
Public Class Form1

Dim viewCountryList As New MyListItem
Dim sqlReportViewer As New MyListItem
Dim CalculateDrivingDistance As New MyListItem

Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load


' Initialize ViewCountryList
_uctTesting_ViewCountryList = New uctTesting_ViewCountryList
viewCountryList.Control = _uctTesting_ViewCountryList
viewCountryList.Name = "Country List"

' Initialize SQLReportViewer
sqlReportViewer.Control = _uctTesting_SQLReportViewer
_uctTesting_SQLReportViewer = New uctTesting_SQLReportViewer
sqlReportViewer.Name = "Report Viewer"

' Initialize CalculateDrivingDistance
CalculateDrivingDistance.Control = New
uctTesting_CalculateDrivingDistance
_uctTesting_CalculateDrivingDistance = New
uctTesting_CalculateDrivingDistance
CalculateDrivingDistance.Name = "Calculate Driving Distance"

' Add each MyListItem to the List control
ulbNav.Items.Add(viewCountryList)
ulbNav.Items.Add(sqlReportViewer)
ulbNav.Items.Add(CalculateDrivingDistance)

End Sub

Private Sub ulbNav_ItemSelected(ByVal sender As System.Object,
ByVal e As
Infragistics.Win.UltraWinListBar.ItemEventArgs) Handles
ulbNav.ItemSelected

Dim selectedListItem As MyListItem

RemoveExitingUserControls()

' Convert the generic Object to the MyListItem type
selectedListItem = CType(e.Item, MyListItem)

' Add the control
splContainer.Panel2.Controls.Add(selectedListItem.Control)

End Sub

Private Sub RemoveExitingUserControls()
splContainer.Panel2.Controls.Clear()
End Sub

End Class
 
R

Ronald S. Cook

Jayeldee,

Thanks for the response. But, when I run this I get:

Unable to cast object of type 'Infragistics.Win.UltraWinListBar.Item' to
type 'FRC.COW.Feedyard.MyListItem'.

... at run-time when it hits the line:

selectedListItem = CType(e.Item, MyListItem)

Any thoughts?

Thanks,
Ron
 
J

jayeldee

Jayeldee,

Thanks for the response. But, when I run this I get:

Unable to cast object of type 'Infragistics.Win.UltraWinListBar.Item' to
type 'FRC.COW.Feedyard.MyListItem'.

.. at run-time when it hits the line:

selectedListItem = CType(e.Item, MyListItem)

Any thoughts?

Thanks,
Ron

Ron,

Sorry about that Exception. It's more than likely from me not being
familiar with the types of controls you're using.

Do the items in the ListBar have a Tag property where you could put
the custom object? It seems they don't use the same method that a
standard ListBox does when adding custom objects to it.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top