Solved

How to access a Win Form from a module/Class

Posted on 2006-11-15
15
245 Views
Last Modified: 2010-04-23
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
Comment
Question by:muntasirrahman
  • 6
  • 4
  • 2
  • +2
15 Comments
 
LVL 47

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 17946310
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
 

Author Comment

by:muntasirrahman
ID: 17946343
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
 
LVL 13

Expert Comment

by:newyuppie
ID: 17946348
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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

Author Comment

by:muntasirrahman
ID: 17946383
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
 
LVL 13

Expert Comment

by:newyuppie
ID: 17946409
of course you are replacing what reads "form1" for the actual name in your project? (Form2 i believe?)
0
 

Author Comment

by:muntasirrahman
ID: 17946452
---    my actual form is Form2 then

  Dim frm As Form2 = DirectCast(frm, Form2)
        MessageBox.Show(frm.TextBox1.Text)
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17946492
for that purpose your original win form has to be public
0
 
LVL 35

Expert Comment

by:YZlat
ID: 17946494
Public Class Form1
    Inherits System.Windows.Forms.Form
0
 

Author Comment

by:muntasirrahman
ID: 17946531
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
 
LVL 13

Expert Comment

by:newyuppie
ID: 17946547
<<mForm1 = Form2.ActiveForm.ParentForm.OwnedForms.CreateInstance>>
that cant be good coding
0
 

Author Comment

by:muntasirrahman
ID: 17946556
Now that one not working.... i forgot what was it actually.....
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17946558
<<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
 
LVL 62

Accepted Solution

by:
Fernando Soto earned 70 total points
ID: 17947265
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
 

Author Comment

by:muntasirrahman
ID: 17953378
Thanks newyuppie  and FernandoSoto ......
Boss "FernandoSoto" It is now Working ....
0
 
LVL 62

Expert Comment

by:Fernando Soto
ID: 17955986
Glad I was able to help. ;=)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now