Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

How do i reuse a class from withing two diffrent forms?

 

clsCommon is to be used with both frmMain and frmSettings, but when starting the second form (frmSettings) i get "Specified cast is not valid".

 

The code i use is:

 

#clsCommon
Public Class clsCommon

   Private objForm As frmMain

   Public Sub New(ByVal objCallingForm As Form)
       objForm = objCallingForm
   End Sub

   Public Sub DoSomething()
       objForm.change_form_in_some_way
   End Sub

End Class

#frmMain
Public Class frmMain

   Inherits System.Windows.Forms.Form
   Private clsCommon As New clsCommon(Me)

   Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
       Dim frmSettings As New frmSettings
       frmSettings.Show()
   End Sub

End Class

#frmSettings
Public Class frmSettings

   Inherits System.Windows.Forms.Form
   Private clsCommon As New clsCommon(Me)

   Private Sub frmSettings_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       'clsCommon.ShowUserMessage(0, , "Hej hej", True, False)
   End Sub

End Class

 

When clicking button4 to show the frmSettings i get the error "Specified cast is not valid".

 

I know that there is some error in clsCommon:

Private objForm As frmMain

But how can i do this different?

 

Im so into the old VB6 that my mind is twisted.. :(

 

Thanks!

 

// Peter

  • Leaders
Posted

you should be using objCallingForm As frmMain , not as Form , eg:

#clsCommon
Public Class clsCommon

   Private objForm As frmMain

   Public Sub New(ByVal objCallingForm As frmMain) '/// not As Form
       objForm = objCallingForm
   End Sub

   Public Sub DoSomething()
       objForm.change_form_in_some_way
   End Sub

End Class

Posted

I got you.

But how can i call this class from both frmMain and frmSettings?

Isnt the class bound to use only frmMain with your suggestion?

 

// Peter

  • Leaders
Posted

if it's a Class and Not a form , you shouldn't do it like above , the above is a way of creating reference to a form , eg:

Public Class Form2
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public frmMe As Form2

   Public Sub New(ByVal f2 As Form2)
       MyBase.New()
       frmMe = f2

 

Public Class Form3
   Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

   Public frmMe1 As Form3

   Public Sub New(ByVal f3 As Form3)
       MyBase.New()
       frmMe1 = f3

 

in frmMain ...

Dim frmSecond As New Form2(Me)
Dim frmThird As New Form3(Me)
'/// do stuff

Posted

Well, i got two forms that i want to use the same class to call common functions. I got it to work as i wanted. Thanks!

 

What i did in my real project was attaching a notfyicon on the frmMain, and i wanted to get to that via the class. But now i realize i must do this the other way around.

 

What is the best way to create an application that need to access this notifyicon from all classes and forms?

 

Shall i start the project from a class (Sub Main) and then start all forms from there. As it is right now i use frmMain as startup object.

 

Feedback?

 

// Peter

  • Leaders
Posted (edited)

the penny dropped after , lol

[color=green]'/// the class / form you wish to access from both forms ...[/color]
[Color=Blue]Public Class[/color] Form3
   [Color=Blue]Inherits[/color] System.Windows.Forms.Form

#[color=blue]Region[/color] [color=gray]" Windows Form Designer generated code "[/color]

   [Color=Blue]Private[/color] frm1 [Color=Blue]As[/color] Form1
   [Color=Blue]Private[/color] frm2 [Color=Blue]As[/color] Form2

   [Color=Blue]Public Sub[/color] [Color=Blue]New[/color]([Color=Blue]Optional[/color] [Color=Blue]ByVal[/color] f1 [Color=Blue]As[/color] Form1 = [Color=Blue]Nothing[/color], [Color=Blue]Optional[/color] [Color=Blue]ByVal[/color] f2 [Color=Blue]As[/color] Form2 = [Color=Blue]Nothing[/color])
       [Color=Blue]MyBase[/color].New()
       [Color=Blue]If[/color] [Color=Blue]Not[/color] f1 [Color=Blue]Is[/color] [Color=Blue]Nothing[/color] [Color=Blue]AndAlso[/color] [Color=Blue]Not[/color] f2 [Color=Blue]Is[/color] [Color=Blue]Nothing[/color] [Color=Blue]Then
[/color]            frm1 = f1
           frm2 = f2
       [Color=Blue]ElseIf[/color] [Color=Blue]Not[/color] f1 [Color=Blue]Is[/color] [Color=Blue]Nothing[/color] [Color=Blue]Then
[/color]            frm1 = f1
       [Color=Blue]ElseIf[/color] [Color=Blue]Not[/color] f2 [Color=Blue]Is[/color] [Color=Blue]Nothing[/color] [Color=Blue]Then
[/color]            frm2 = f2
       [Color=Blue]End[/color] [Color=Blue]If

[/color]        [Color=Green]'This[/color] [Color=Green]call is required by the Windows Form Designer .[/color]
       InitializeComponent()

       [Color=Green]'Add any initialization after the InitializeComponent() call[/color]

   [Color=Blue]End Sub[/color] 
[color=green]'/// test it with a button click ...[/color]
[Color=Blue]Private Sub[/color] Button1_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] Button1.Click
       [Color=Blue]If[/color] [Color=Blue]Not[/color] frm1 [Color=Blue]Is[/color] [Color=Blue]Nothing[/color] [Color=Blue]Then[/color]
          frm1.Button1.PerformClick()
       [Color=Blue]End If[/color]
   [Color=Blue]End Sub[/color]

from one of your forms , eg: Form1 ...


   [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Form1_Load([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] [Color=Blue]MyBase[/color].Load
       [Color=Blue]Dim[/color] frm [Color=Blue]As[/color] [Color=Blue]New[/color] Form3([Color=Blue]Me[/color])
       frm.Show()
   [Color=Blue]End[/color] [Color=Blue]Sub

[/color]    [Color=Blue]Private[/color] [Color=Blue]Sub[/color] Button1_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] Button1.Click
       [color=Green]'/// you will call this from Form3 to test you have access to this Form.[/color]
       MessageBox.Show("test")
   [Color=Blue]End[/color] [Color=Blue]Sub[/color]

Edited by dynamic_sysop

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...