Thread: Windows form C++ - referencing the form

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    36

    Windows form C++ - referencing the form

    Hello,

    I am trying to create my first windows form using visual C++. To create the form I use the following code in my .cpp file
    Code:
    		Form1^ fas;
    		fas = gcnew Form1();
    	        updateTextBox(fas);
    	        Application::Run(fas);
    I have functions such as 'updateTextBox(fas)' that sends some data to be output onto a text field on the form.

    The problem is I am writing a function where I am unable to pass 'fas' as an argument. (the function is called from form.h). This function needs to update the text box as well and I have no way to tell the function what form to communicate with!

    I thought about having 'fas' as a global variable but I cannot seem to do that. Is there any other way of referencing a form?

    Many thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rocketman50 View Post
    Hello,

    I am trying to create my first windows form using visual C++. To create the form I use the following code in my .cpp file
    Code:
    		Form1^ fas;
    		fas = gcnew Form1();
    	        updateTextBox(fas);
    	        Application::Run(fas);
    I have functions such as 'updateTextBox(fas)' that sends some data to be output onto a text field on the form.

    The problem is I am writing a function where I am unable to pass 'fas' as an argument.
    This either indicates a lack of understanding of how functions work or indicates that you have done something seriously wrong.

    Quote Originally Posted by rocketman50 View Post
    (the function is called from form.h).
    And this confirms that something is seriously wrong. Code in a header file? Templates had better be involved, or else you are in Big Trouble, mister.

    But really: you're not supposed to have the actual objects when you write functions -- you're supposed to pass them in. Write your function to take a Form1^ thing, and then you can pass it to wherever it needs to go.

    Also: why isn't updateTextBox a member function of your form in the first place?

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I suspect:

    Code:
    public void UpdateTextBox(Form1 ^form);
    ..will work as tabstop has described. But UpdateTextBox should be a member of your form or at least be in the code that invalidates the form so the redraw is correct.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Windows 7, First Impressions
    By Mario F. in forum General Discussions
    Replies: 16
    Last Post: 08-11-2009, 06:46 AM
  2. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 04:44 AM
  4. IE 6 status bar
    By DavidP in forum Tech Board
    Replies: 15
    Last Post: 10-23-2002, 05:31 PM
  5. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM