By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,978 Members | 1,389 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,978 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Feb 22 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Feb 22, 12:34 pm, "Ronald S. Cook" <r...@westinis.comwrote:
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

Feb 22 '07 #2

P: n/a
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


"jayeldee" <ja******@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Feb 22, 12:34 pm, "Ronald S. Cook" <r...@westinis.comwrote:
>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

Mar 19 '07 #3

P: n/a
On Mar 19, 2:06 pm, "Ronald S. Cook" <r...@westinis.comwrote:
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.

Mar 21 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.