473,320 Members | 2,029 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Accessing Controls on Other Forms

I have a windows form (FORM1) and in that I have a Richtextbox control.
I am loading another form (FORM2) from form 1. From FORM2 I would like
to access the richtexbox control on FORM1. So I tried doing the
following code in form 2.

Dim m_Form1 As New Form1
MessageBox.Show(m_Form1.RichTextBox1.Text)

But the above code returns me nothing.

Any suggestions???

Regards
Mani

Jan 2 '07 #1
4 936
Mani wrote:
From FORM2 I would like to access the richtexbox control on FORM1.
Dim m_Form1 As New Form1
Creates a new instance of the Class Form1 and store a reference to it in
the variable m_Form1.
MessageBox.Show(m_Form1.RichTextBox1.Text)
Retrieves the Text property of the Richtextbox control on that /new/
form - probably not a lot in it at this point ... ;-)

Forms are Classes, so to access a property of one, you have to have a
reference to that particular object. Pass a reference to Form1 to
Form2, as in

[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Master = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Public WriteOnly Property Master() as Form1
Set(value As Form1)
_Master = value
End Set
End Property

Private _Master As Form1 = Nothing

Sub otherButton_Click( ... )
MessageBox.Show( _Master.RichTextBox1.Text )
End Sub

End Class

HTH,
Phill W.
Jan 2 '07 #2
Thanks very much Phil. I tried your sample and it is working fine.

Regards
Mani

Leon Mayne wrote:
"Mani" wrote:
Dim m_Form1 As New Form1
MessageBox.Show(m_Form1.RichTextBox1.Text)

But the above code returns me nothing.

It's because the code is creating a new instance of the Form1 class and then
getting the text from the textbox (which presumably is blank when first
created).

How are you handling the two forms? Does one open the other as a new window?
Jan 2 '07 #3
Phill,

Your "_Master" property exist already, the name for it is "Owner".

In my idea is that nicer to use and than you can forget to create that
property yourself.

Cor

"Phill W." <p-.-a-.-w-a-r-d@o-p-e-n-.-a-c-.-u-kschreef in bericht
news:en**********@south.jnrs.ja.net...
Mani wrote:
>From FORM2 I would like to access the richtexbox control on FORM1.
>Dim m_Form1 As New Form1

Creates a new instance of the Class Form1 and store a reference to it in
the variable m_Form1.
>MessageBox.Show(m_Form1.RichTextBox1.Text)

Retrieves the Text property of the Richtextbox control on that /new/
form - probably not a lot in it at this point ... ;-)

Forms are Classes, so to access a property of one, you have to have a
reference to that particular object. Pass a reference to Form1 to Form2,
as in

[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Master = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Public WriteOnly Property Master() as Form1
Set(value As Form1)
_Master = value
End Set
End Property

Private _Master As Form1 = Nothing

Sub otherButton_Click( ... )
MessageBox.Show( _Master.RichTextBox1.Text )
End Sub

End Class

HTH,
Phill W.

Jan 3 '07 #4
Cor Ligthert [MVP] wrote:
Phill,

Your "_Master" property exist already, the name for it is "Owner".
Cor,

Another day, another new property I've not met before - and it makes for
/much/ nicer code, too ...

Many Thanks,
Phill W.
>[Form1.vb]
Sub button_Click( ... )
Dim f2 as New Form2
f2.Owner = Me
f2.ShowDialog() ' or whatever
End Sub

Class Form2
Sub otherButton_Click( ... )
MessageBox.Show( Me.Owner.RichTextBox1.Text )
End Sub

End Class
Jan 3 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Jim Heavey | last post by:
Hello, I have created a form which I intend to use a a dialog box. It has a few controls on that form and I changed the properties of those controls to "public" with the idea that I could access...
5
by: RSH | last post by:
I havent been able to set a property from another class with out getting some sort of error. Can someone please tell me what I'm doing wrong here? Public Class Form1
7
by: Sudhakara.T.P. | last post by:
Hi, I have an tab control in one of the forms and it has two tab strips. Now I have written an general function that accepts form name as parameter and iterates through all the controls in the...
3
by: DotNetNewbie | last post by:
I am reading the book Teach Yourself Microsoft Visual Basic .Net 2003 in 21 Days. I am having trouble getting one of the exercises to work at the end of day 4. Exercises: 1. Create a new...
4
by: raj_genius | last post by:
I hav two queries, whc are as follows: FIRSTLY: is it possible to access the controls(by name) of a parent form(MDI) from its child forms??if yes then how??plzz provide a coded example in VB if...
0
by: aakash | last post by:
Hello Guys I am upsizing ms access project to give it a ms sql connectivity I am having problem in accessing form control values in ms sql function CREATE FUNCTION "ReportList DateRange"() ...
8
by: Ryan | last post by:
Ok.. I have a form with lots of stuff on it; a tool strip panel, menu strip, data binding elements (dataset, binding source, table adapter), tab control with 7 tab pages, each page contains a...
3
by: judy.j.miller | last post by:
Does anyone know why i can't access a form element value using dot notation in firefox, when i'm in a function. Works ok in the body. I'm trying to do this: var FarTemp = faren.temp.value; I...
8
by: colmkav | last post by:
Hi, could someone tell me how I can check whether a database is open by name eg something like db("mydbname")
0
by: ansh20 | last post by:
I am adding run time generate controls in array. Now I want to retrieve those controls in other subroutine in same form. But when I am tring to retrieve control it says "Object variable or with...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.