print variables to windows form?

Hi I'm relatively new to windows forms, and c++ in general.

I was wandering what the best way to print a variable to a windows form would be?

This is the code i have in main.cpp:
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
#include <iostream>
#include <fstream>
#include <stream>
#include "stdlib.h"

int main(){

	ifstream inFile;
	inFile.open("save.txt");

	//check for error
	if (inFile.fail()) {
		cerr << "Error Opening File" << endl;
		exit(1);
	}

	int x, y;

	inFile >> x >> y;

	cout << "Num1: " << x << endl;
	cout << "Num2: " << y << endl;

	return 0;
}


instead of using the cout function, how can i print x and y to something on my windows form?
Hi Doodayer,

I'm no expert but I hope I can help...

You could use a label or textBox to display your int variable. If you wanted to have this displayed when the form loads, all you need to do is double click the form when your viewing Form1.h design..... This creates the 'Form1_Load' event.

1
2
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}


As for your variables.... This should work (providing you have a label and textBox (named label1 and textBox1 respectively).

1
2
3
4
5
6
private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
int x = 10;
int y = 2;
label1->Text = Convert::ToString(x);
textBox1->Text = y.ToString("N2")
}


Note. the code bit 'ToString("N2")' is useful to set the number of decimal places, even if it is an integer (int)... In the above example, this should display 2.00 in textBox1. i.e. N2 represents 2 decimal places. textBox1->Text = y.ToString(); will work also but will dispaly as an 'int'....

Either bit of code after the = sign would work for label or textBox...

I hope this helps.


Last edited on
when i double click on anything in the form, including the form, it opens a file named xapper.vb. what is this? it contains these:
1
2
3
    Private Sub XapperAlpha_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub


which is similar to this that you posted
1
2
	private: System::Void Form1_Load(System::Object^  sender, System::EventArgs^  e) {
}


but i cant seem to find any .h files.
Hi,

I'm guessing you're using Visual Basic? The code I suggested was for Visual C++ and I'm not familiar with VB. Sorry, I thought that this was your choice of language. As fas as my limited understanding takes me, I think there are similarities between the two (VB vs. VC++). Hence, you have pointed some out already. Both codes do look similar.

Unless someone else replies to assist you, I would suggest looking at some tutorials found on youtube. I found SchoolFreeware's channel very helpful at getting me started:

https://www.youtube.com/watch?v=_hsPfoOmNIM

With that said, I have learnt a hell of lot more form other cpluscplus forum users....

Sorry I couldn't help you any more.

Last edited on
Do you know how i can convert everything over to c++? I'd prefer to being using that instead of .vb as I've never really worked with it.
I haven't used a conversion tool but a quick google came up with 'VBto' which might help. I use Microsoft visual c++ express. It is free to download. There are many people who are quick to point out that visual c++ (windows form programming) isn't c++. However, you can mix c++ style code in your project with careful planning and conversions. The more you do it the better you get at it as with everything in life.

Good luck!
Thanks for all the help!
Topic archived. No new replies allowed.