Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

How to access a Win Form from a module/Class

I would like accees a FORM from module
So that i could access controls  of the FORM.

In the early vb 6.0 it could be done by
form2=form

........
Can any one help me on the issue.

0
muntasirrahman
Asked:
muntasirrahman
  • 6
  • 4
  • 2
  • +2
1 Solution
 
Wayne Taylor (webtubbs)Commented:
Try something like this....

    Dim frm As New Form1
    frm.Label1.Text = "Something"

....where Form1 is the name of the form you want

Regards

Wayne
0
 
muntasirrahmanAuthor Commented:
But it does not get my original form's control property

messagebox does not show "muntasir"

ie:

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        TextBox1.Text = "muntasir"
        TEst()
    End Sub

------------------------- in the module
Imports System.Data.SqlClient
Module C10
    Public mForm1 As New Form2

    Public Sub TEst()
        ' mForm1.Owner = Form2.ActiveForm
        ' MessageBox.Show(mForm1.TextBox1.Text())

        'mForm1.TextBox1.Text = "xp"

        MessageBox.Show(mForm1.TextBox1.Text())
    End Sub
0
 
newyuppieCommented:
if you want to access an already displayed form rather than a new one, use this code:

dim frm as form1 = directcast(frm, form1)
frm.label1.text = "something"

if you are unsure if your form1 has been initialized you could modify my code to read:
dim frm as form1 = trycast(frm,form1)
if (frm isnot nothing)
  from.label1.text = "something"
end if

NY
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
muntasirrahmanAuthor Commented:
Following Error Occurd:
An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

Additional information: Object reference not set to an instance of an object.
0
 
newyuppieCommented:
of course you are replacing what reads "form1" for the actual name in your project? (Form2 i believe?)
0
 
muntasirrahmanAuthor Commented:
---    my actual form is Form2 then

  Dim frm As Form2 = DirectCast(frm, Form2)
        MessageBox.Show(frm.TextBox1.Text)
0
 
YZlatCommented:
for that purpose your original win form has to be public
0
 
YZlatCommented:
Public Class Form1
    Inherits System.Windows.Forms.Form
0
 
muntasirrahmanAuthor Commented:
it is as below:
Public Class Form2
    Inherits System.Windows.Forms.Form



Few months ago i seen a code some thing like:

mForm1 = Form2.ActiveForm.ParentForm.OwnedForms.CreateInstance
0
 
newyuppieCommented:
<<mForm1 = Form2.ActiveForm.ParentForm.OwnedForms.CreateInstance>>
that cant be good coding
0
 
muntasirrahmanAuthor Commented:
Now that one not working.... i forgot what was it actually.....
0
 
newyuppieCommented:
<<Module C10>> at the top of your class, what is that? can you change it to "Public Class C10"? end it with End Class instead of End Module.
0
 
Fernando SotoRetiredCommented:
Hi muntasirrahman;

I used your original code posted in the question with two changes. This should do what you want.

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        mForm1 = Me                     ' Add this line
        TextBox1.Text = "muntasir"
        TEst()
    End Sub

------------------------- in the module
Imports System.Data.SqlClient
Module C10

    Public mForm1 As Form2              ' Change this line to this

    Public Sub TEst()
        ' mForm1.Owner = Form2.ActiveForm
        ' MessageBox.Show(mForm1.TextBox1.Text())

        'mForm1.TextBox1.Text = "xp"

        MessageBox.Show(mForm1.TextBox1.Text())
    End Sub


Fernando
0
 
muntasirrahmanAuthor Commented:
Thanks newyuppie  and FernandoSoto ......
Boss "FernandoSoto" It is now Working ....
0
 
Fernando SotoRetiredCommented:
Glad I was able to help. ;=)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 4
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now