Cannot find my void?

closed account (LAfSLyTq)
ok so i made a windows form application and there are 2 seperate .cpp files...

Windows form.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Windows form.cpp : main project file.

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

using namespace Windows form;

void Search()
{
	Process::Start("IExplore.exe", "http://youtube.com/");
}

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());
	return 0;
}


and then there is Form1.cpp
all you need to know about form1.cpp is that i did this

1
2
3
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 Search();
		 }


i want the button to open the void from the other .cpp, how do i do this?
Last edited on
Put the function and button click in form1.h
Last edited on
closed account (LAfSLyTq)
where?
I guess when you say "void" you really mean the "Search()" function.

You need to put the prototype (i.e. void Search();) somewhere before the button1_Click() function. Maybe in its own header file like "search.h"

When you double click on the button in the form designer it will generate the click function, add your search function below it. Also there is little reason to add any code to the <project_name>.cpp file, Windows form.cpp in your case. It is the entry point of the app and really only starts the form
1
2
// Create the main window and run it
	Application::Run(gcnew Form1());
anything you do in that file will only happen either before the form opens or after it closes. You can do what coder777 suggested and put you functions in a seperate header, but I usually find it easiest to just add (simple) code to Form1.h
closed account (LAfSLyTq)
i added the code in its own header
Search.h
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include "Form1.h"

using namespace Windows form;

void Search()
{
	Process::Start("IExplore.exe", "http://youtube.com/");
}


but it still doesnt work, and i added
void Search();
to the top of my From1.h

i keep gettting LNK errors.
Last edited on
You need to include search.h in form1.h, but dont need to include form1.h in search.h
Edit: This simplified code works
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#pragma once

namespace formtest {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
	private: System::Windows::Forms::Button^  button1;
	protected: 

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(124, 123);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 0;
			this->button1->Text = L"button1";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(282, 255);
			this->Controls->Add(this->button1);
			this->Name = L"Form1";
			this->Text = L"Form1";
			this->ResumeLayout(false);

		}
#pragma endregion
	private: void Search(){
				 System::Diagnostics::Process::Start("IExplore.exe", "http://cplusplus.com");
				}
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				 Search();
			 }
	};
}
Last edited on
Topic archived. No new replies allowed.