Referencing controls on one form from another 
Author Message
 Referencing controls on one form from another

I am having trouble referencing my Form1 from a Form2 as
there does not seem to be a forms collection as such. The
book I have says to use the 'new' keyword to instantiate
the object. The text from the book on the OK button of
form2 is. Unfortunately nothing happens - removing new
produces an error. Why new when the form1 is the startup
form and is open? Why does this code taken from a book not
work? It seems so simple in VB or VBA.

Dim N As New Form1()

        If optBlue.Checked = True Then
            N.TextBox1.ForeColor =
System.Drawing.Color.Blue
            Me.lblColour.Text = "Blue Selected"
        ElseIf optRed.Checked = True Then
            N.TextBox1.ForeColor = System.Drawing.Color.Red
            Me.lblColour.Text = "Red Selected"
        ElseIf optGreen.Checked = True Then
            N.TextBox1.ForeColor =
System.Drawing.Color.Green
            Me.lblColour.Text = "Green Selected"
        ElseIf optBlack.Checked = True Then
            N.TextBox1.ForeColor =
System.Drawing.Color.Black
            Me.lblColour.Text = "Black Selected"
        End If



Sun, 05 Jun 2005 13:08:12 GMT  
 Referencing controls on one form from another
Create N as a global object variable and somewhere in the Form1 constructor
put N = Me.


Quote:
> I am having trouble referencing my Form1 from a Form2 as
> there does not seem to be a forms collection as such. The
> book I have says to use the 'new' keyword to instantiate
> the object. The text from the book on the OK button of
> form2 is. Unfortunately nothing happens - removing new
> produces an error. Why new when the form1 is the startup
> form and is open? Why does this code taken from a book not
> work? It seems so simple in VB or VBA.

> Dim N As New Form1()

>         If optBlue.Checked = True Then
>             N.TextBox1.ForeColor =
> System.Drawing.Color.Blue
>             Me.lblColour.Text = "Blue Selected"
>         ElseIf optRed.Checked = True Then
>             N.TextBox1.ForeColor = System.Drawing.Color.Red
>             Me.lblColour.Text = "Red Selected"
>         ElseIf optGreen.Checked = True Then
>             N.TextBox1.ForeColor =
> System.Drawing.Color.Green
>             Me.lblColour.Text = "Green Selected"
>         ElseIf optBlack.Checked = True Then
>             N.TextBox1.ForeColor =
> System.Drawing.Color.Black
>             Me.lblColour.Text = "Black Selected"
>         End If



Sun, 05 Jun 2005 13:17:59 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. PRB: One (just one) control in a form

2. One one form based on a control value in another

3. Referencing a label on one form from another

4. Make a reference to a MDI form with a reference of a MDI child form

5. referencing a control on a caller form from the called form

6. Can anybody straighten me out on accessing one form's controls from another form

7. How to acess forms controls of one form from other in VB.NET

8. How refer from one form to a control in another open form

9. Self-referencing a control's name within one of it's event procedures

10. Referencing one ActiveX control from another.

11. Two ActiveX DLL controls that reference one another

12. Referencing object in form in code / Subform reference problem in code

 

 
Powered by phpBB® Forum Software