call objects from other forms!

sepehr

Member
Joined
Oct 7, 2005
Messages
5
Location
Karaj
Programming Experience
Beginner
i have 2 forms , i have created a button in the forst form tha brings up the second form when it is clicked and the second form has a button.
now there is the problem ,i have a textbox on the first form and i want to when the button on the second form is clicked the value of the textbox in the first button is changed to "Neo" , what should i do?:confused:
 
i took a loot at it but did not help much ! is this wrong ?why?

'form 2 on click Handler sub
'the textbox1 is in the first form and the button is in the second form
' i want to know why the following code is wrong and what is the right thing
form1.textbox1.text="neo"
 
As quoted from the MSDN: The .NET system for handling forms is more consistent and more powerful than the system in Visual Basic 6.0, but even changes for the better can cause confusion for those trying to make the transition

You simply can't make that way as in VB6.

The easiest way to achieve that, though might not be the best is to put this declaration in Form1

VB.NET:
[COLOR=Blue]Public Shared[/COLOR] tBox [COLOR=Blue]As[/COLOR] TextBox
Then in the Form1_Load, put this:

VB.NET:
tBox = TextBox1
You can easily change the textbox value putting this code in Form2:

VB.NET:
Form1.tBox.Text = "Neo"
For further reference, you may go to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
http://www.devcity.net/Articles/100/1/.aspx
 
i'll test it thanks;)
 
thanks a lot my problem got solved but another question created!
now the aim is not to change a text but value ! the aim is to disable a menu item! at first in designing mode, menu items are not created as shared! so i changed them to shared :
Friend shared WithEvents MenuItem11 AsSystem.Windows.Forms.MenuItem
it went well and i could change the enabled value turned to false by:
form1.menuitem11.enable=false
now is the problem when the above code is writen there is a compiling error that the item is decalared shared so the handler should the same too! what should i do?
Private Sub MenuItem11_click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem11.Click

Dim regform As New Form4
regform.MdiParent = Me
regform.Show()
End Sub
the above code is the procedure i get the error from! and this should be shared and who knows how?
 
All that u need is to change the sub New of 2nd form
change like this (let Form1 and Form2 be the forms)
VB.NET:
Private MainForm As Form1
Public Sub New(ByVal f As Form)
        MyBase.New()
        MainForm = DirectCast(f, Form1)
                'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub
Now when showing form2 do this
VB.NET:
dim f2 as new Form2(Me)
f2.show()
In the Form2 module, u can now access all public objects(and functions and subs) in form1 like this
VB.NET:
MainForm.Textbox1.text="I have done it!"
 
Last edited:
ayozzhero said:
As quoted from the MSDN: The .NET system for handling forms is more consistent and more powerful than the system in Visual Basic 6.0, but even changes for the better can cause confusion for those trying to make the transition

You simply can't make that way as in VB6.

The easiest way to achieve that, though might not be the best is to put this declaration in Form1

VB.NET:
[COLOR=Blue]Public Shared[/COLOR] tBox [COLOR=Blue]As[/COLOR] TextBox
Then in the Form1_Load, put this:

VB.NET:
tBox = TextBox1
You can easily change the textbox value putting this code in Form2:

VB.NET:
Form1.tBox.Text = "Neo"
For further reference, you may go to:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_vstechart/html/vbtchWorkingWithMultipleFormsInVisualBasicNETUpgradingToNET.asp
http://www.devcity.net/Articles/100/1/.aspx
Making a member Shared for this purpose is a very bad idea. It will work if there is only a single instance of the form but it is a complete hack.
 
thanks i did the textbox thing ! my problem is now that i can not change the enable property!
i type
VB.NET:
 form1.menuitem11.enable=false
and i decalare my menu item 11 as an shared one!
now the problem of second form is solved but another problem came up :(
i had a click handler for menuitem11 before after making the menuitem 11 shared
it gave me the error that the handler should be shared,now how can i make the handler shared?
 
Now you see why using a Shared member is a hack. What you're doing is not what Shared members are for, so you're going to run into other issues that you didn't anticipate. Do it properly in the first place. If you want this second form to be able to affect the first form then you should be doing as jain_mj suggested and passing a reference to the first form to the second when it's created. Also, good programming practice would suggest that you make all your controls Private and then give the form Public properties and/or methods that do the things you want. For instance, if I wanted to be able to set the Enabled property of a menu item, I would make the menu item Private, and definitely not Shared, and then I would give the form a Public method that took a Boolean argument and then assigned that value to the Enabled property of the menu item. That way, the menu item is not visible from outside but you can still enable and disable it from outside. It's like taking the pizza from the delivery man at the door rather than letting him wander around the house freely just to put the pizza on the dining table.
 
ByVal. You only ever pass a parameter ByRef if you need to be able to assign a new object to that variable and have that change persist once the method returns. I would say that more than 99 time out of 100 you would pass a method argument ByVal. If you pass "Me" to the constructor you definitely aren't going to try to assign a different form to that reference are you?
 
Hmm... now I know the reason why declaring as Shared is much discouraged. Some codes do solve your problem, but some others avoid creating new problems :)

By the way, I am new to the DirectCast thingy. While I am trying too Google, can some one briefly explain what it is and what does it do?
 
DirectCast is a type conversion function like CType (not exactly like)
search MSDN for DirectCast

In the sub new()
type 'f.' you cannot find any controls in the Form1 in the list.
But if u type 'MainForm.' you can see the controls
 
Back
Top