CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104

    Passing a Form reference

    I have three forms: Form1, Form2 and Form3. I want to be able to pass a reference to either Form2 or Form3 so that Form1 will invoke the appropriate one when a button is clicked. So I set up a Form property in Form1:
    Code:
    private System.Windows.Forms.Form m_frmAddEdit;
    ...
    public System.Windows.Forms.Form AddEditForm
    {
    	  get { return m_frmAddEdit;  }
    	  set { m_frmAddEdit = value; }
    }
    I've passed other object references in this manner (specifically, an OdbcConnection), so I figured this would be fine.

    Unfortunately, when I try to pass a Form reference:
    Code:
    Form1 frm = new Form1();
    frm.AddEditForm = Form2;
    The compiler complains that:
    'Form2' denotes a 'class' where a 'variable' was expected
    and
    'Form1.m_frmAddEdit' denotes a 'field' where a 'class' was expected
    This latter error is in reference to the following line in Form1:
    Code:
    m_frmAddEdit frmToCall = new m_frmAddEdit();
    What am I doing wrong here? I don't understand why C# expects the property I declared as a Form to be a 'variable'.

    Do I need to be looking at some way to do this with Delegates instead? (I'd like to keep the calling/invoking logic inside Form1.)
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  2. #2
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    Okay, I see what my problem is. But now the question becomes:

    How do I pass a reference to a class (not an object)?
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  3. #3
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    Okay, (in case anybody cares) I've got mostly what I want by using the GetType() method:
    Code:
    if (m_frmAddEdit.GetType() == (new Form2()).GetType())
    
     - or -
    
    if (m_frmAddEdit.GetType().Equals((new Form2()).GetType()))
    This will work, but it gets messy if there are a lot of possible forms and a lot of methods and properties I need to call - which winds up being a lot of (Form2) casting.

    Ideally, I'd like to be able to do something like
    Code:
    if (...GetType(Form2)...) {
      frm = (Form2)m_frmAddEdit;
    }
    ...
    frm.CallMethod();
    but scope rules keep me from making this work. I can't declare frm outside of the if statement (as scope rules would require) because I don't yet know what Type it is. And I can't declare it inside the if because then it won't be available outside.

    I seem to remember some languages having some kind of mechanism for making local variables available outside the blocks they were declared in. I don't see anything like this for C#, though. Is there any such thing?
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

  4. #4
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    263
    not sure if this is what you wanna do, here's how to access controls between forms...
    in Form1:
    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form2 frm=new Form2();
    this.AddOwnedForm(frm);// add this line when opening form2.
    frm.Show();
    }
    in Form2:
    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form1 frmMain=(Form1)this.Owner;
    frmMain.textBox1.AppendText("some stuff");
    }
    string Signature = Censored;

  5. #5
    Join Date
    Aug 2002
    Location
    Washington, USA
    Posts
    104
    Originally posted by dynamic_sysop
    Code:
    private void button1_Click(object sender, System.EventArgs e)
    {
    Form1 frmMain=(Form1)this.Owner;
    frmMain.textBox1.AppendText("some stuff");
    }
    My problem goes beyond this. Given your code sample above, how would you handle the fact that, within button1_Click(), you don't know that Form1 is the form you need to cast to?

    What if the form could be any of Form3, Form4, Form5, Form6, Form7, etc. - dynamically chosen at runtime?
    - Shawn
    MCP, VB6: Desktop Apps
    [ C# | VB | .NET | Java | VC++ | Perl | PHP | Javascript ]
    Unless otherwise stated, all sample code provided is UNTESTED.
    http://www.codemastershawn.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured