vb .net driving me nuts!!

Raziel

Member
Joined
Feb 12, 2004
Messages
7
Location
Lima, Peru
hi!

i have made a user control and im using it on my form1 class

now i have added a second form to my project (form2) and this one has button control which when clicked i want it to access a method of my control that is on form1. If i used vb6 itd be like this:

[VB]Private Sub Command1_Click()
Dim str as String
str = Text1.Text
Call Form1.MyControl.InsertText(str)
End Sub[/VB]

can you tell me how the heck do i do that on VB .NET???

any help will be apreciated
 
[VB]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FormInstanceName As New Form1
Dim str As String
str = Textbox1.Text
FormInstanceName.InsertText(str)
FormInstanceName.Dispose()
End Sub
[/VB]

Ive never used the Call statement in VB.Net, a sub is just called by its name.

The procedure InsertText(Byval str as String) needs to be public, private wont work.

You need to create an instance of the form as a variable:

Code:
Dim FormInstanceName as new Form1

If youre only going to use it once and create it inside of the button, dispose of it (delete the instance variable) after youre done.

Code:
FormInstanceName.Dispose

If youre going to access your Form1 many times, you want to make a public declaration.

Ive found that when you work with multiple forms, that adding a module and declairing common variables as public will make it accessable throughout your entire project.

"Dim FormInstanceName as new Form1" on Form1, Form2 or Form3 will only allow that variable to be accessed on that respective form.

"Public FormInstanceName as new Form1" in a module allows any form to access the variable.

In my most recent project, I had to add a lot of dataAdaptors to a form. I added them to my main form, then created procedures to access them in public procedures on my module (cleverly named modPublic) so that any form could call the procedures to update or manipulate them.

This is all true to my experience unless my memory is getting foggy.
 
dude, you didnt get me, right??

the code i posted was "IF IT WAS VB6 ITD BE LIKE THAT" but its NOT

well, anyway... i suposse that if i declare a Form1 variable on my Form2 itll display another one, and what i want to do is to access MyControl which is on Form1 through the button in Form2. Thats why i said IF IT WAS VB6.

on the other hand, you said that i might be able to access it from a module, but how do i create a control by code?? MyControl is set manually on Form1, and ive tried creating it by code, by declaring it as new both on Form1 and Module1 but it doesnt appear at run time... so what can i do?? im confused, and that little thing is stopping me from finishing my project
 
Originally posted by Raziel
dude, you didnt get me, right??

the code i posted was "IF IT WAS VB6 ITD BE LIKE THAT" but its NOT

Yes. You posted VB6 code and I posted the VB.Net equivilent.

The code I posted is contained in a button on Form2 (or whatever) which grabs the text from the textbox on Form2 and passes it to the procedure located in Form1.

thats what your code does in vb6. Thats what my code does in vb7.

Actually I pretty much directly converted the code you gave, which isnt the most efficient way.

Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim FormInstanceName As New Form1
        FormInstanceName.TextDisplay.Text = Textbox1.Text
        FormInstanceName.Dispose()
    End Sub

This allows you to click a button on Form2 and transfer the text from Textbox1 on Form2 to the TextDisplay control (whatever it may be, textbox, label, button text, etc) on Form1.

There wasnt any reason to declare a string, the .Text method sends and recieves as a string.

"Textbox1.Text = Textbox2.Text" is perfectly valid, as is: "Label.Text = Textbox2.Text"

a controls .Text method can be used in leu of a string as a parameter in a procedure as well.

well, anyway... i suposse that if i declare a Form1 variable on my Form2 itll display another one,

No. Declaring an instance of Form1 on Form2 is required to display Form1 (FormInstanceName.Show), but its just an instance name of the Form1 object.

Each form becomes an object, which can be accessed through its instance name if its declared. Once declared you can access its default methods (Frex: .Show or .Modal = True) as well as user defined methods (the procedures you write).

You must declare an instance of a form to access it off the form. Once you declare it you can read or write from/to controls on that form, set their properties, etc.

what i want to do is to access MyControl which is on Form1 through the button in Form2. Thats why i said IF IT WAS VB6.

That was a mybad, as I cut out the MyControl portion. It doesnt change anything, thats what the code will do.

Once you type this: "Dim frmInst as new Form1" you can access any of Form1s controls, properties, methods, procedures, etc.

Just type: frmInst.Show to show it, frmInst.Text to change its caption, frmInst.TextBox1.Text to access its TextBox1s Text property, frmInst.strText to access the public variable strText on Form1.

Remember, this only works with public variables and procedures. If its Private you cant access it from another form.

on the other hand, you said that i might be able to access it from a module, but how do i create a control by code??

You can access it by a module as well the exact same way as I showed you in my example.

"Public frmInst as new Form1" if its in your general declarations outside of any procedures or "Dim frmInst as new Form1" if its in a procedure.

You dont need to create a control by code. Its already on Form1, you can access it through the variable you create.

MyControl is set manually on Form1, and ive tried creating it by code, by declaring it as new both on Form1 and Module1 but it doesnt appear at run time... so what can i do?? im confused, and that little thing is stopping me from finishing my project

Is "MyControl" a control youre creating by code yourself or one you used the IDE to plop down, like a textbox or something?

declaring doesnt show, it creates an instance of an object, which gives you access to its properties and methods.

If you have a textbox on Form1, the only way you can access it from Form2 (or from a module) is to create an instance variable for Form1 (dim frmInst as new Form1) and use the instance variable to manipulate Form1s controls (frmInst.TextBox1.Text = "X")

In anycase, this is a procedure you can pop into your module and all you have to do is pass the formname (Me).

Code:
    Public Sub AddButton(ByVal frmForm As Form)
        Dim x As New Button
        x.Top = 300
        x.Left = 200
        x.Width = 100
        x.Height = 100
        x.Text = "X"
        x.BackColor = System.Drawing.Color.Black
        x.ForeColor = System.Drawing.Color.Red
        frmForm.Controls.Add(x)
    End Sub

and in your formload:

Code:
AddButton(Me)
 
hey man that was long...

but thanks to you i finally solved my problem
THANKS MAN

look, you told me to declare it at Button1_Click but it didnt work cause i needed it to be the same tabber and it creates another instance of form1 with another instance of my control

im used to program in c++ and the model is completely different so i thought to pass it by reference but it didnt work anyway

what i finally did was declare a form1 variable at my module and on the Form1 load event set it to frmMain = Me so when i used the variable at Form2 it was the same Form1 that im seeing

but you helped me understand some aspects of the object programming in VB... thanks again!!
 
Back
Top