Another newbie windows form question

I’m totally new to creating a window form application. My first attempt will be a form with two drop down combo boxes and a WebBrowser control under them.

I’m converting a console application that I wrote that creates an Intranet website on your hard drive to an interactive windows form application that creates the pages one at a time and displays them directly in the WebBrowser control.

The initialization process will be a bit complex so I want to make it a separate cpp file. I’ve looked at quite a few video examples on how to create the controls and modify them, but none of them go into how to do the initialization in a separate file that can do things like adding items to the dropdown controls and defining the default value. I can do that in the main form.h file, just not sure how to create an initialize.cpp file, call it from “InitializeComponent”, and then include lines in the external cpp file like:

this->comboBox1->Items->Add("line 1");

I’ve create the initialize.cpp file:

1
2
3
4
5
6
7
8
9
10
11
12
13
// Initialize.cpp : load data files and initialize controls..

#include "stdafx.h"
#include "Form1.h"

using namespace MarketView;


void Initialize()
{
	this->comboBox1->Items->Add("line 1");
	return;
}


But when I compile the program I get a lot of errors

1
2
3
4
5
6
7
8
9
10
11
12
13
1
1>Compiling...
1>Initialize.cpp
1>.\Initialize.cpp(11) : error C2673: 
     'Initialize' : global functions do not have 'this' pointers
1>.\Initialize.cpp(11) : error C2227: 
     left of '->comboBox1' must point to class/struct/union/generic type
1>.\Initialize.cpp(11) : error C2227:
      left of '->Items' must point to class/struct/union/generic type
1>.\Initialize.cpp(11) : error C2227: 
     left of '->Add' must point to class/struct/union/generic type
1>Build log was saved at "...MarketView\Debug\BuildLog.htm"
1>MarketView - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


I’m not sure how to fix this simple newbie problem. What is the proper list of include files that should be in the additional cpp file? Since I want to call Initialize() from the InitializeComponent function of the main Form1.h file, what is the proper location for the “extern void Initialize(void)” line?
Last edited on
One thing I have tried it to reference the form by it’s name:

When I type “MarketView::” I get the intellisense that includes Form1 which then gives me comboBox1, but that is where it stops.

I’ve tried to include the line: “using namespace System::Windows::Forms::ComboBox;” but still no intellesense options for “MarketView::Form1::comboBox1::”

The line I want is “MarketView::Form1::comboBox1->Items->Add("line 1");”

comboBox1->Items gives error C2227: left of '->Items' must point to class/struct/union/generic type

comboBox1.Items gives error C2228: left of '.Items' must have class/struct/union

comboBox1::Items gives two errors:
error C3083: 'comboBox1': the symbol to the left of a '::' must be a type
error C2039: 'Items' : is not a member of 'MarketView::Form1'

What is the proper syntax for this?
Last edited on
This just keeps getting more and more confusing! Where do I go to find out the syntax of making a simple call to a form?

OK, I moved the code Combo Box Add line into form.h
1
2
3
4
5
6
7
8
9
10
11
12
13
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}
		void AddDate(char *date, int ID)
		{
			this->comboBox1->Items->Add("line 1");
		}


It compiles fine, but the call to it in my Initialize.cpp file

MarketView::Form1::AddDate("abs",1);

Give the error C2352: 'MarketView::Form1::AddDate' : illegal call of non-static member function

OK, so I change void AddDate to “static void AddDate” and now get the error that “static member functions do not have 'this' pointers” so I go back to the “MarketView::Form1::comboBox1” situation where there is no legal syntax after …Box1 to get me Items->Add

I’ve been an old fashion programmer for over 47 years. It seems as is the concept of programming computers has changed from the concepts of logic to memorization of complex syntax.

There has to be a simple answer to do this other than to write thousands of lines of code in one Form1.h file. I refuse to believe that the new programming cocepts will not allow you to write code in smaller more manageable modules.
Last edited on
I suspect Form1 is an instance of a class; if you want to call methods of an instance use the . operator (e.g. Form1.AddDate()).

If this is the problem, the following might help you:
* The :: operator is used to access static members and methods of a class (not an object or instance)
* The . opertator is used to access members and methods of an object (class instance)
The code was created by Visual Studio as:

1
2
3
4
5
6
7
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
		}


And I added:

1
2
3
4
		void AddDate(char *date, int ID)
		{
			this->comboBox1->Items->Add("test");
		}


I copied header of the MarketView.cpp file create by Visual Studio and added my own Initialize function, but I can’t seem to find the proper syntax for calling the AddDate procedure I create in the Form1 class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// Initialize.cpp : load data files and initialize controls..

#include "stdafx.h"
#include "Form1.h"

using namespace  MarketView;

[STAThreadAttribute]
void Initialize()
{
	//MarketView::Form1::comboBox1->Items->Add("line 1");
	//this->comboBox1->Items->Add("line 1");
	//Form1.AddDate("abs",1);
	Form1::comboBox1.AddDate("abs",1);
	return;
}


Any idea on what the proper syntax is for defining and calling the entry point in the Form1 class?

FYI, I got the answer on the Visual Studio Forum

The key is to call the initialize process from form1.h and pass the pointer to the class:
1
2
extern void Initialize(Form1^ form1);
Initialize(this);


The initialize function can then call the non-static methods in the class:
1
2
3
4
5
void Initialize(Form1^ form1)
{
    form1->AddDate();
    return;
}


Thank u guys, had the same problem!
Topic archived. No new replies allowed.