Basic Text Box Problem

Hello all, I've recently began my exploration into Windows Programming. I decided to make a, what I thought, fairly simple program.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

#pragma endregion
	private: System::Void Generate_Click(System::Object^  sender, System::EventArgs^  e) {

//declare variables
int X, Y, Z;
string Output + " "=;
srand ( time(NULL) );	

//X = rand 
//Y = rand	
//Z = rand

//Output = X + " - " + Y + " - " + Z	  
	X = txtOuput->Text->rand() % 8999 + 1000;;
	Y = txtOutput->Text->rand() % 8999 + 1000;;
	Z = txtOutput->Text->rand() % 8999 + 1000;;
        Output = X + " - " + Y + " - " + Z;
        Output->Text = Output.ToString("C");
				
				 

			 }



What my program does, is when you click the button "Generate" It generates three numbers, X, Y, and Z, and stores them into a string to output to a textbox. How ever, I'm getting a ton of errors which in the end, makes me feel dumb about learning Win32. Any comments would be helpful, thanks.


Miscellaneous.
I'm using Microsoft Visual C++ 2010
Windows Form Application
Little knowledge on Win32
closed account (3hM2Nwbp)
Try something like this for the method, I'm not entirely sure the syntax is all correct, but it's close.

1
2
3
4
5
6
7
8
9
10
11
12
// Assuming you have a TextBox Control named Output
System::Void Generate_Click(System::Object^  sender, System::EventArgs^  e)
{
    // Seed the random generator
    srand ( time(NULL) );
    int X = rand() % 8999 + 1000;
    int Y = rand() % 8999 + 1000;
    int Z = rand() % 8999 + 1000;

    // Set the Text property
    Output->Text = X.ToString() + " - " + Y.ToString() + " - " + Z.ToString();
}


By the way, what you're coding there is called C++/CLI - it's Microsoft's custom C++ <-> .NET mess hybrid, but believe me when I say it's quite a bit nicer to build GUIs with than the native Win32 API.
Last edited on
That helps greatly, thank you. I still get some errors, but I'm sure if I mess around enough, I can fix it.
closed account (3hM2Nwbp)
Post the compiler output and I can help point you in the right direction.

1>------ Build started: Project: Random_Gen, Configuration: Debug Win32 ------
1> Random_Gen.cpp
1>c:\users\matt\desktop\random_gen\random_gen\Form1.h(111): error C2064: term does not evaluate to a function taking 1 arguments
1>c:\users\matt\desktop\random_gen\random_gen\Form1.h(117): error C2065: 'Output' : undeclared identifier
1>c:\users\matt\desktop\random_gen\random_gen\Form1.h(117): error C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''
1> Random_Gen1.cpp
1> Generating Code...
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
closed account (3hM2Nwbp)
The best I can go on with that output (not seeing your full source code) is that your text box control isn't named "Output". Go into the designer and change the text box's name field to Output

If you paste the entire source I can tell you exactly what's wrong.
Last edited on
I tried to work it out, but I think I misnamed or never named something

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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
namespace Random_Gen {

	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::StatusStrip^  statusStrip1;
	protected: 
	private: System::Windows::Forms::Timer^  srand;
	private: System::Windows::Forms::TextBox^  textBox1;
	private: System::Windows::Forms::Button^  Generate;
	private: System::ComponentModel::IContainer^  components;

	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>


#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->components = (gcnew System::ComponentModel::Container());
			System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Form1::typeid));
			this->statusStrip1 = (gcnew System::Windows::Forms::StatusStrip());
			this->srand = (gcnew System::Windows::Forms::Timer(this->components));
			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
			this->Generate = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// statusStrip1
			// 
			this->statusStrip1->Location = System::Drawing::Point(0, 240);
			this->statusStrip1->Name = L"statusStrip1";
			this->statusStrip1->Size = System::Drawing::Size(473, 22);
			this->statusStrip1->TabIndex = 0;
			this->statusStrip1->Text = L"statusStrip1";
			// 
			// textBox1
			// 
			this->textBox1->Location = System::Drawing::Point(22, 99);
			this->textBox1->Name = L"Generate";
			this->textBox1->Size = System::Drawing::Size(339, 20);
			this->textBox1->TabIndex = 1;
			//->textBox1-> = "Test";
			// 
			// Generate
			// 
			this->Generate->Location = System::Drawing::Point(367, 99);
			this->Generate->Name = L"Generate";
			this->Generate->Size = System::Drawing::Size(75, 23);
			this->Generate->TabIndex = 2;
			this->Generate->Text = L"Generate";
			this->Generate->UseVisualStyleBackColor = true;
			this->Generate->Click += gcnew System::EventHandler(this, &Form1::Generate_Click);
			// 
			// Form1
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(473, 262);
			this->Controls->Add(this->Generate);
			this->Controls->Add(this->textBox1);
			this->Controls->Add(this->statusStrip1);
			this->Icon = (cli::safe_cast<System::Drawing::Icon^  >(resources->GetObject(L"$this.Icon")));
			this->Name = L"Form1";
			this->Text = L"Random Generator";
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion
	private: System::Void Generate_Click(System::Object^  sender, System::EventArgs^  e) {

				srand ( time(NULL) );
				int X = rand() % 8999 + 1000;
				int Y = rand() % 8999 + 1000;
				int Z = rand() % 8999 + 1000;

				// Set the Text property
				Output->Text = X.ToString() + " - " + Y.ToString() + " - " + Z.ToString();

				
				 

			 }
	};
}


closed account (3hM2Nwbp)
Line 41 -> What's the timer for? I think you want to seed the random generator with the current time, which won't happen because your timer (srand) has the same name as the functions in ctdlib.h: http://cplusplus.com/reference/clibrary/cstdlib/

To fix that, remove your timer.

Line 117 -> Output doesn't exist, you need to use your text box as the variable (see line 42)

textBox1->Text = X.ToString() + " - " + Y.ToString() + " - " + Z.ToString();

I see that the Windows section now has a C++/CLI tag :O
Last edited on
Thank you, so, very much. This has helped me greatly on the 'missing gap' on Win32 and C++. Cheers!
Topic archived. No new replies allowed.