7 Replies - 469 Views - Last Post: 19 July 2014 - 09:33 AM Rate Topic: -----

#1 bgengine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-July 14

Form inside a dll

Posted 15 July 2014 - 03:36 AM

Hi
I want to to be able to call a dll - and pass some parameters to it.
When I call the Dll - I want a form ( which is already formatted inside the Dll) to load.
This form will allow the user to enter in text into two text boxes (in my case username and password) then click on a login button.
When the login button in clicked - the username and password is validated with code inside the dll/form...the form then closes and the result of this validation is returned back along with the username to the dll caller.

Does anyone have any sample code of how I can do this ...
I was reading this post from some time ago .. but was wondering if anyone successfully did it and could post me an example ..

http://www.dreaminco...-from-dll-file/

thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Form inside a dll

#2 Kizzle  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 07-July 14

Re: Form inside a dll

Posted 15 July 2014 - 05:26 AM

Could you post some of your code so we can see where your at?
Was This Post Helpful? 0
  • +
  • -

#3 bgengine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-July 14

Re: Form inside a dll

Posted 15 July 2014 - 05:38 AM

View PostKizzle, on 15 July 2014 - 05:26 AM, said:

Could you post some of your code so we can see where your at?



Hi - I'd like to ... but I'm not sure where to start

I can create a dll .. with a form in it ... but
1. How to I call the DLL so that it will start the form .. when the dll is called
2. How to I pass the parameters ( from the Dll caller) to the form
3. ... I think I know how to work the form so that it reads the user inputs from the two text boxes and validates the user
4.. how do I send the results back to the dll caller

thanks
Was This Post Helpful? 0
  • +
  • -

#4 IronRazer  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 288
  • View blog
  • Posts: 906
  • Joined: 01-February 13

Re: Form inside a dll

Posted 19 July 2014 - 05:28 AM

Hi bgengine,

You will need to add or modify some of its code for passing data to it and for being able to send data back to the caller form.

There are a few ways to send or receive data.
1. Add some Public Properties to the dll code that can be set or read from the caller form.
2. You can modify the Constructor of the dll to accept data when you create the new instance of the dll in your caller form. Then add a custom Public event to the dll so that it can be received in the caller form and get the data from it.
3. You can also use Public Subs and Functions to transfer data to and from the dll form.
The way you choose is up to you and depends on how and when you need to send or receive the data.

If this dll with a form is a separate class library project (dll) then you will need to add a reference to the dll in your caller form project. You can do that by opening or creating your caller form project and then on the menu click (Project) and select (Add Reference...). Then when the Add Reference window opens you can use the (Browse) tab and click Browse to browse to your dll file and select it. It should be in the Release folder of the dll project folder.
If it is a Class that you added in your caller Form project then you will not need to add a reference to it.

Then you create a new instance of the dll class in your caller form project. If you used a Public event in the dll to send data back to the caller form you will want to create the new instance using the WithEvents keyword or you will need to use AddHandler to add handler event subs in the caller form.

Below is a small example of using the Constructor of the dll to pass some data to it when the new instance is created in the caller form. I also used a custom public event to send data back to the caller form so, you will notice i declared the instance of the dll form using WithEvents.


This is the code for my (dll) class library project that i built separate from the caller form project. You will need to add 2 references to it before you build the class. I explained how at the top of the code.
'You must add a reference to (System.Windows.Forms) and (System.Drawing). Do that by clicking (Project) on the VB menu
'And selecting (Add Reference...). On the Add reference window select the (.Net) tab and selelect these from the list.

Imports System.Windows.Forms
Imports System.Drawing

Public Class Class1
    Inherits Form

    'the custom event use to send the name and password back to the caller form.
    'you can declare them to be whatever type of data you want. They don`t have to be strings
    Public Event LoginInfo(ByVal uName As String, ByVal uPassword As String)

    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents TextBox2 As System.Windows.Forms.TextBox
    Friend WithEvents TextBox1 As System.Windows.Forms.TextBox

    'Use the constructor to pass info to this form. Use as many parameters of whatever type you need to pass to the form
    Public Sub New(ByVal info1 As String) 'i only used on parameter as a string for this example
        InitializeComponent()
        Me.Text = info1
    End Sub

    Private Sub InitializeComponent()
        Me.TextBox1 = New System.Windows.Forms.TextBox
        Me.TextBox2 = New System.Windows.Forms.TextBox
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'TextBox1
        '
        Me.TextBox1.Location = New System.Drawing.Point(25, 24)
        Me.TextBox1.Name = "TextBox1"
        Me.TextBox1.Size = New System.Drawing.Size(240, 20)
        Me.TextBox1.TabIndex = 0
        '
        'TextBox2
        '
        Me.TextBox2.Location = New System.Drawing.Point(25, 59)
        Me.TextBox2.Name = "TextBox2"
        Me.TextBox2.Size = New System.Drawing.Size(240, 20)
        Me.TextBox2.TabIndex = 1
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(107, 104)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(75, 23)
        Me.Button1.TabIndex = 2
        Me.Button1.Text = "Button1"
        Me.Button1.UseVisualStyleBackColor = True
        '
        'Class1
        '
        Me.ClientSize = New System.Drawing.Size(292, 157)
        Me.Controls.Add(Me.Button1)
        Me.Controls.Add(Me.TextBox2)
        Me.Controls.Add(Me.TextBox1)
        Me.Name = "Class1"
        Me.ResumeLayout(False)
        Me.PerformLayout()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text.Trim <> "" And TextBox2.Text.Trim <> "" Then
            'do you code to check if the name and password are correct.
            ' then raise the custom LoginInfo event to send the info to the caller form
            RaiseEvent LoginInfo(TextBox1.Text, TextBox2.Text)
        End If
    End Sub
End Class



Here is the code for the caller form project. You will need to add a reference to the dll that was created from the code above.
'On VB menu click (Project) and select (Add Reference...). When the window opens Browse to the dll file you created
'and select it.

Imports ClassLibrary1 'My dll`s Namespace is ClassLibrary1

Public Class Form1
    Private WithEvents loginform As Class1 'Create an instance of Class1 which is the name of my class

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Create the new instance of my dll class library and pass it a string that will be used for its Text property
        Dim titleinfo As String = "My Login Form Test"
        loginform = New Class1(titleinfo) 'passes the title info to the dll form using the constructor
        loginform.ShowDialog()
    End Sub

    'This is the event i created in the class that is raised when its button is clicked which sends the
    'username and password in its parameters back to this form
    Private Sub loginform_LoginInfo(ByVal uName As String, ByVal uPassword As String) Handles loginform.LoginInfo
        Label1.Text = uName
        Label2.Text = uPassword
        loginform.Close()
    End Sub
End Class



As i said, there are a few ways to pass the data back and forth so this was just an example of one way you can do it. Hope it helps. B)
Was This Post Helpful? 0
  • +
  • -

#5 bgengine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-July 14

Re: Form inside a dll

Posted 19 July 2014 - 08:46 AM

Many thanks iron razor,

This looks like what I'm looking for ,
I'll load it it and verify.

I'm just wondering is it possible to have the form already constructed in the dll and just call it ?
Is it easier to do this ?

Regards
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 288
  • View blog
  • Posts: 906
  • Joined: 01-February 13

Re: Form inside a dll

Posted 19 July 2014 - 09:10 AM

Your Welcome. :)

You need to have a Form project that you can create a new instance of the dll class library in before you can show the form that is in the dll class library. Dll files are not executable like a Form Project is.

In my example the dll has a form that is shown when you create the new instance of the dll and call .Show or .ShowDialog on the instance from within the caller Form project.
Was This Post Helpful? 1
  • +
  • -

#7 bgengine  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 15-July 14

Re: Form inside a dll

Posted 19 July 2014 - 09:19 AM

View PostIronRazer, on 19 July 2014 - 09:10 AM, said:

Your Welcome. :)/>

You need to have a Form project that you can create a new instance of the dll class library in before you can show the form that is in the dll class library. Dll files are not executable like a Form Project is.

In my example the dll has a form that is shown when you create the new instance of the dll and call .Show or .ShowDialog on the instance from within the caller Form project.


Thanks IronRazor , you code example is perfect ... many thanks for your help.

regards
Was This Post Helpful? 0
  • +
  • -

#8 IronRazer  Icon User is online

  • D.I.C Addict
  • member icon

Reputation: 288
  • View blog
  • Posts: 906
  • Joined: 01-February 13

Re: Form inside a dll

Posted 19 July 2014 - 09:33 AM

Your very Welcome bgengine. :D
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1