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: