question

kavehrahimi-5744 avatar image
0 Votes"
kavehrahimi-5744 asked karenpayneoregon answered

Add form to module and where can I define module

Hi ,I don't know where do I have to add form and module? I want add both module and form in an application.
how can I call form in my module?
Please explain me by example.
Thanks

dotnet-visual-basic
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

karenpayneoregon avatar image
0 Votes"
karenpayneoregon answered

Generally speaking if you want to interact between a module and a form I'd recommend using a class (rather than a module) with a delegate/event.

Your delegate can send information to the form or perhaps send a signal to a form and when the form gets the signal do something. The signal could be a number, an enum or an instance of a class and more if needed.

Example, we want to interact with a object, in this case

 Public Class PersonGrades
     Public Property PersonID() As Integer
     Public Property FirstName() As String
     Public Property LastName() As String
     Public Property Grade() As Decimal?
     Public Property GradeLetter() As String
 End Class

Our super simple class

 Public Class Operations
     Public Delegate Sub OnIteratePersonGrades(ByVal personData As PersonGrades)
     Public Shared Event OnIteratePersonGradesEvent As OnIteratePersonGrades
    
     Public Shared Sub DoSomething()
         RaiseEvent OnIteratePersonGradesEvent(New PersonGrades() With
             {
                  .PersonID = -1,
                  .FirstName = "Karen",
                  .LastName = "Payne",
                  .Grade = 100
             })
     End Sub
 End Class


Form code where in form shown event we subscribe to the event in the Operations class then for simplicity call a do nothing method and send a person object to the form. In turn the form looks at the id, if -1 we do something for a new record while greater than -1 we have an edit. The do something may then interact with form controls for instance.

 Public Class MainForm
     Private Sub MainForm_Shown(sender As Object, e As EventArgs) Handles Me.Shown
         AddHandler Operations.OnIteratePersonGradesEvent, AddressOf OnIteration
     End Sub
    
     Private Sub OnIteration(persondata As PersonGrades)
         If persondata.PersonID = -1 Then
             ' do something for a new person
         Else
             ' do something for an edited person
         End If
     End Sub
    
     Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
         Operations.DoSomething()
     End Sub
 End Class

In closing, many developers never learn about delegates/events and are well worth time placed to learn about them.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered kavehrahimi-5744 commented

Hi,
try this module in console demo:

 Imports System.Windows.Forms
    
 Module Module1
   Sub Main()
     Try
       Dim c As New Demo
       c.Execute()
     Catch ex As Exception
       Console.WriteLine(ex.ToString)
     End Try
     Console.WriteLine("Continue enter key")
     Console.ReadKey()
   End Sub
   Friend Class Demo
     Friend Sub Execute()
       Application.EnableVisualStyles()
       Application.SetCompatibleTextRenderingDefault(False)
       Application.DoEvents()
       Application.Run(New Form1())
     End Sub
   End Class
    
   Friend Class Form1
     Inherits Form
     Private Label1 As New Label() With {.Text = "Form 1 loaded"}
     Public Sub New()
       AddHandler Me.Load, Sub()
                             Me.Controls.Add(Label1)
                           End Sub
     End Sub
   End Class
 End Module
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Please explain about that where can I use this
In my solution or project?

0 Votes 0 ·