PC Review


Reply
Thread Tools Rate Thread

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

 
 
Ronald S. Cook
Guest
Posts: n/a
 
      22nd Feb 2007
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



 
Reply With Quote
 
 
 
 
jayeldee
Guest
Posts: n/a
 
      22nd Feb 2007
On Feb 22, 12:34 pm, "Ronald S. Cook" <r...@westinis.com> wrote:
> 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

 
Reply With Quote
 
Ronald S. Cook
Guest
Posts: n/a
 
      19th Mar 2007
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On Feb 22, 12:34 pm, "Ronald S. Cook" <r...@westinis.com> wrote:
>> 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
>



 
Reply With Quote
 
jayeldee
Guest
Posts: n/a
 
      21st Mar 2007
On Mar 19, 2:06 pm, "Ronald S. Cook" <r...@westinis.com> wrote:
> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How load user control into panel on split container in Win app? Ronald S. Cook Microsoft VB .NET 2 16th Feb 2007 12:22 PM
Panel based holder for User Controls and Scrolling Adam Goetz Microsoft Dot NET Framework 0 13th Jun 2006 02:51 AM
Location error placing dynamic user controls on a Panel =?Utf-8?B?RG9uSA==?= Microsoft Dot NET Framework Forms 1 27th May 2004 02:01 AM
Re: dynamically load web user controls Craig Buchanan Microsoft ASP .NET 1 16th Oct 2003 04:23 AM
Dynamically load Web User Controls Jack David Microsoft Dot NET 1 25th Sep 2003 12:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 03:20 AM.