Parameter for Winpart controls

Parameter for Winpart controls

Old forum URL: forums.lhotka.net/forums/t/4826.aspx


Warren posted on Thursday, May 08, 2008

My application is designed on Ptracker and uses WinPart. My mainform is using the following routines to load the winpart control after the menu is item is selected:

 Private Sub chosenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim currentAssembly As Assembly = Assembly.GetAssembly(GetType(MainForm))
      Dim mi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
      Dim classToInstantiate As String = "MyAppWin." + DirectCast(mi.Tag, String)
      Dim selectedForm As System.Object = currentAssembly.CreateInstance(classToInstantiate)
      ShowSelectedForm(DirectCast(selectedForm, WinPart))
 End Sub
  
 Public Sub ShowSelectedForm(ByVal formToLoad As WinPart)
      ' see if this form is already loaded
      For Each ctl As Control In MainFormPanel.Controls
         If TypeOf ctl Is WinPart Then
            If formToLoad.GetIdValue().Equals(DirectCast(ctl, WinPart).GetIdValue()) Then
               ' project already loaded so just
               ' display the existing winpart
               ShowWinPart(DirectCast(ctl, WinPart))
               formToLoad.Dispose()
               Return
            End If
         End If
      Next...

Private Sub AddWinPart(ByVal part As WinPart)
     AddHandler part.CloseWinPart, AddressOf CloseWinPart
      part.BackColor = ToolStrip1.BackColor
      MainFormPanel.Controls.Add(part)
      Me.DocumentsToolStripDropDownButton.Enabled = True
      ShowWinPart(part)
 End Sub


In some cases I want to send a parameter to the Winpart control but am unsure of the best way to accomplish this. I realize that this is mostly a .NET question but I know other are using the winpart design and likely have a solution implemented.  My first instinct is to go with an optional parameter in ShowSelectedForm but after reading about the use of optinal parameters I think this is poor design.


I have created a winpart control which contains the reportviewer control for a specific report. All works well for the single report but now I I need to make this control generic so I can use a single winpart control for all my reports. This control will accept a report name and then based on this name will aquire the correct  datasoure, perform binding etc. I am unsure how to add the report name parameter in this case.

Any advice is truly appreciated.

 

sergeyb replied on Thursday, May 08, 2008

One way is to have a Public Sub New(reportName as string) in your winpart control that contains report viewer.  You will need to store that controls class name and report name inside the tag of a menu item, maybe separated by a comma, such as .Tag = “rprtviewer, itemsreports”. 

Then you will have

If DirectCast(mi.Tag, String).Contans(“,”)

‘split the string into class name and parameter: clasToInstantiate and reportName

selectedForm = currentAssembly.CreateInstance(classToInstantiate, new object() {reportName})

 

Else

 

selectedForm = currentAssembly.CreateInstance(classToInstantiate)

End If


 

Sergey Barskiy

Senior Consultant

office: 678.405.0687 | mobile: 404.388.1899

cid:_2_0648EA840648E85C001BBCB886257279
Microsoft Worldwide Partner of the Year | Custom Development Solutions, Technical Innovation

 

From: Warren [mailto:cslanet@lhotka.net]
Sent: Thursday, May 08, 2008 3:49 PM
To: Sergey Barskiy
Subject: [CSLA .NET] Parameter for Winpart controls

 

My application is designed on Ptracker and uses WinPart. My mainform is using the following routines to load the winpart control after the menu is item is selected:

 Private Sub chosenToolStripMenuItem_Click(ByVal sender As Object, ByVal e As EventArgs)
      Dim currentAssembly As Assembly = Assembly.GetAssembly(GetType(MainForm))
      Dim mi As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)
      Dim classToInstantiate As String = "MyAppWin." + DirectCast(mi.Tag, String)
      Dim selectedForm As System.Object = currentAssembly.CreateInstance(classToInstantiate)
      ShowSelectedForm(DirectCast(selectedForm, WinPart))
 End Sub
  
 Public Sub ShowSelectedForm(ByVal formToLoad As WinPart)
      ' see if this form is already loaded
      For Each ctl As Control In MainFormPanel.Controls
         If TypeOf ctl Is WinPart Then
            If formToLoad.GetIdValue().Equals(DirectCast(ctl, WinPart).GetIdValue()) Then
               ' project already loaded so just
               ' display the existing winpart
               ShowWinPart(DirectCast(ctl, WinPart))
               formToLoad.Dispose()
               Return
            End If
         End If
      Next...

Private Sub AddWinPart(ByVal part As WinPart)
     AddHandler part.CloseWinPart, AddressOf CloseWinPart
      part.BackColor = ToolStrip1.BackColor
      MainFormPanel.Controls.Add(part)
      Me.DocumentsToolStripDropDownButton.Enabled = True
      ShowWinPart(part)
 End Sub


In some cases I want to send a parameter to the Winpart control but am unsure of the best way to accomplish this. I realize that this is mostly a .NET question but I know other are using the winpart design and likely have a solution implemented.  My first instinct is to go with an optional parameter in ShowSelectedForm but after reading about the use of optinal parameters I think this is poor design.


I have created a winpart control which contains the reportviewer control for a specific report. All works well for the single report but now I I need to make this control generic so I can use a single winpart control for all my reports. This control will accept a report name and then based on this name will aquire the correct  datasoure, perform binding etc. I am unsure how to add the report name parameter in this case.

Any advice is truly appreciated.

 



Warren replied on Friday, May 09, 2008

Hi Sergey,

Thanks for pointing the way. I ended up going with the following which seems to work, any suggestions for improvement are welcome.

' Split the tag string into class name and parameter
If classToInstantiate.Contains(",") Then
   parmString = Mid(classToInstantiate, InStr(classToInstantiate, ",") + 1)
   classToInstantiate = Mid(classToInstantiate, 1, InStr(classToInstantiate, ",") - 1)
   selectedForm = Activator.CreateInstance(Type.GetType(classToInstantiate), New Object() {parmString})
Else
   selectedForm = currentAssembly.CreateInstance(classToInstantiate)
End If

Warren replied on Saturday, May 10, 2008

I read some up on best practices for .Net and removed references to Microsoft.VisualBasic.dll assembly and use native .NET types instead:

  ' Split the tag string into class name and parameter
      If tagClassToInstantiate.Contains(",") Then
         tagParm = tagClassToInstantiate.Substring(tagClassToInstantiate.IndexOf(",", 0) + 1)
         tagClassToInstantiate = tagClassToInstantiate.Substring(0, tagClassToInstantiate.IndexOf(tagParm, 0) - 1)

         selectedForm = Activator.CreateInstance(Type.GetType(tagClassToInstantiate), New Object() {tagParm})
      Else
         selectedForm = currentAssembly.CreateInstance(tagClassToInstantiate)
      End If

Copyright (c) Marimer LLC