Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Scope of a function

Status
Not open for further replies.

asimov500

Programmer
Mar 14, 2008
21
Hi there,

I am programming Visual C++/cli 2008.
I can make functions with no problem and I can call them from Form1.h without a problem.

The problem comes when I want to change a textBox or Label control from a function.

I realise it won't work because the textBox is out of scope.

How do I change a textBox from within a function. eg how do I make it in scope. Can I call some kind of pointer from within the function? Or is there an easier way?

I use the Form Designer to make the textBox's and it puts them in Form1.h.

Asimov
 
if its a member function, you should be able to manipulate your member variables. If youre using the generated code like as in the last problem we were having, you have to move the declarations of your object to be member variables. if you declare and define your object in the create function you lose scope. so it would be something like:

form1.h
Code:
class form1
{
    public:
   textbox *myTextBox;     //now member variable

   void ChangeMyTextBox();

};

then in the cpp.. in your create function

Code:
void form1::create()
{
     myTextBox = new TextBox();
}

void form1::ChangeMyTextbox()
{
     myTextBox->ChangeSomeProperty();
}

as it stands.. the generated code declares and defines your object pointers in the local scope of create.
Just move the declaration to the class definition and you shouldnt have a problem using it within your class.
 
oops sorry. i think i was referencing a different problem when i said
If youre using the generated code like as in the last problem we were having, you have to move the declarations of your object to be member variables.

sorry about that. the solution should still apply though.
 
HI SDowd,

Will try that in a bit, but I have a question.
When I create a project it does not create Form1.cpp automatically.

It creates a .cpp file with the project file name and it calls that the main project file.

Yet if I make another Form it auto creates the cpp file with it.

Do I put the functions in the cpp main or do I need to manually create Form1.cpp?

In my tests I put my functions in Form1.h

Asimov
 
i think with the cli and the code generation (don't quote me because Im not too familiar with it), it just makes a .h for you. where the header file creates your namespace and class definitions within in the header. it should actually create both a .h and a .cpp for you, but it doesnt. generally the only functions you want actually defined in your header file are inline functions.
Its a hastle, but i would make the cpp for form1 by hand. your header should declare the member functions and variables, where the cpp actually defines them.
I actually tried making a little test project the other day and saw that the code generation does it with a single .h and no cpp for the class. to me it seems wrong that it should do that, but again, im not too familiar with the cli, so there may be some method to the madness.
 
Hi Sdowd,

Thanks for all the help but I am thinking of trying out Visual
C#. Tried it yesterday and managed to do what I wanted to in minutes rather than weeks. I might come back to c++/cli when the documentation gets better. I mean I have downloaded lots of ebooks on it but none of them go very much into "windows forms", and they all waste half the book with the "hello world" program ahem.

I tried out the code above by the way and I got compiler error after error. I think there is a few bugs in the interface too. As I put something in and it doesn't work and then the compiler comes up with all these errors and then I change it back and I still have errors. Plus the designer screwed up a few times.

With programming I have always learnt to try something small and if it doesn't work then change it back. Unfortunately the new interface is very buggy that way.

Anyway thanks for the help.

Asimov
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top