![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
Can somebody help me? I've asked around and nobody seems to have any useful information for me. I'm rewriting a program I previously wrote, but this time with a GUI... My program is a password generator. I have Visual Studio 2008 Pro. I designed a form with a button and a text box. Basically, I need to know how to work with forms. Specifically, I want to know how to actually make the form run, how to run a function (the generation function) when the button is pressed, and how to output a string (the generated password) to a text box.
Could somebody help me with this, please? Or recommend me a book I could get that would teach me this? I'm in CIS: Programming in college, but it'll be at least 2 more semesters before they teach anything related to this. |
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,035
Rep Power: 15
![]() ![]() |
Re: C++ GUI Programming
Learn about event-driven programming.
__________________
The person who coined the phrase, "There's no such thing as a stupid question," hasn't hung around here much. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers Politically Incorrect |
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
Re: C++ GUI Programming
We meet again, DaWei... sorry about being such a douche in my "game" thread all that time ago. Anyway, do you think you could recommend a book or web resource?
|
|
|
|
|
#4 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
Someone else on another forum found me a very helpful link... in case anyone else who needs to know this information comes across my thread, here's the link...
http://www.visualcplusdotnet.com/cpl...mcontrols.html |
|
|
|
|
#5 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
New question:
I designed the form and even made a menu. The clear button works and clears whatever text is in the text box. Now, there's a problem. I want to run a function when I click the button. Not sure what I'm doing wrong here. The compiler keeps telling me that "string" and "srand" and "rand" are all undefined, as well as a few other errors. Could someone tell me what's wrong with this? #include "stdafx.h"
#include "time.h"
#include "string.h"
#pragma once
namespace PassGenv20 {
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
///
/// WARNING: If you change the name of this class, you will need to change the
/// 'Resource File Name' property for the managed resource compiler tool
/// associated with all .resx files this class depends on. Otherwise,
/// the designers will not be able to interact properly with localized
/// resources associated with this form.
/// </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::MenuStrip^ menuStrip1;
private: System::Windows::Forms::ToolStripMenuItem^ toolStripMenuItem1;
private: System::Windows::Forms::ToolStripMenuItem^ aboutMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ exitToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ helpToolStripMenuItem;
private: System::Windows::Forms::ToolStripMenuItem^ instructionsMenuItem;
private: System::Windows::Forms::Label^ lblTitle;
private: System::Windows::Forms::TextBox^ txtPass;
private: System::Windows::Forms::Button^ btnGen;
private: System::Windows::Forms::Button^ btnClear;
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->menuStrip1 = (gcnew System::Windows::Forms::MenuStrip());
this->toolStripMenuItem1 = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->aboutMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->exitToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->helpToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->instructionsMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
this->lblTitle = (gcnew System::Windows::Forms::Label());
this->txtPass = (gcnew System::Windows::Forms::TextBox());
this->btnGen = (gcnew System::Windows::Forms::Button());
this->btnClear = (gcnew System::Windows::Forms::Button());
this->menuStrip1->SuspendLayout();
this->SuspendLayout();
//
// menuStrip1
//
this->menuStrip1->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->toolStripMenuItem1,
this->helpToolStripMenuItem});
this->menuStrip1->Location = System::Drawing::Point(0, 0);
this->menuStrip1->Name = L"menuStrip1";
this->menuStrip1->Size = System::Drawing::Size(284, 24);
this->menuStrip1->TabIndex = 0;
this->menuStrip1->Text = L"menuStrip1";
//
// toolStripMenuItem1
//
this->toolStripMenuItem1->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(2) {this->aboutMenuItem,
this->exitToolStripMenuItem});
this->toolStripMenuItem1->Name = L"toolStripMenuItem1";
this->toolStripMenuItem1->Size = System::Drawing::Size(37, 20);
this->toolStripMenuItem1->Text = L"&File";
//
// aboutMenuItem
//
this->aboutMenuItem->Name = L"aboutMenuItem";
this->aboutMenuItem->Size = System::Drawing::Size(107, 22);
this->aboutMenuItem->Text = L"&About";
this->aboutMenuItem->Click += gcnew System::EventHandler(this, &Form1::aboutMenuItem_Click);
//
// exitToolStripMenuItem
//
this->exitToolStripMenuItem->Name = L"exitToolStripMenuItem";
this->exitToolStripMenuItem->Size = System::Drawing::Size(107, 22);
this->exitToolStripMenuItem->Text = L"&Exit";
this->exitToolStripMenuItem->Click += gcnew System::EventHandler(this, &Form1::exitToolStripMenuItem_Click);
//
// helpToolStripMenuItem
//
this->helpToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^ >(1) {this->instructionsMenuItem});
this->helpToolStripMenuItem->Name = L"helpToolStripMenuItem";
this->helpToolStripMenuItem->Size = System::Drawing::Size(44, 20);
this->helpToolStripMenuItem->Text = L"&Help";
//
// instructionsMenuItem
//
this->instructionsMenuItem->Name = L"instructionsMenuItem";
this->instructionsMenuItem->Size = System::Drawing::Size(136, 22);
this->instructionsMenuItem->Text = L"&Instructions";
this->instructionsMenuItem->Click += gcnew System::EventHandler(this, &Form1::instructionsMenuItem_Click);
//
// lblTitle
//
this->lblTitle->AutoSize = true;
this->lblTitle->Font = (gcnew System::Drawing::Font(L"Maiandra GD", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->lblTitle->Location = System::Drawing::Point(40, 38);
this->lblTitle->Name = L"lblTitle";
this->lblTitle->Size = System::Drawing::Size(201, 39);
this->lblTitle->TabIndex = 1;
this->lblTitle->Text = L"PassGen v2.0";
//
// txtPass
//
this->txtPass->Location = System::Drawing::Point(47, 80);
this->txtPass->Name = L"txtPass";
this->txtPass->Size = System::Drawing::Size(183, 20);
this->txtPass->TabIndex = 2;
//
// btnGen
//
this->btnGen->ForeColor = System::Drawing::SystemColors::ActiveCaptionText;
this->btnGen->Location = System::Drawing::Point(47, 106);
this->btnGen->Name = L"btnGen";
this->btnGen->Size = System::Drawing::Size(94, 23);
this->btnGen->TabIndex = 3;
this->btnGen->Text = L"Generate";
this->btnGen->UseVisualStyleBackColor = true;
this->btnGen->Click += gcnew System::EventHandler(this, &Form1::btnGen_Click);
//
// btnClear
//
this->btnClear->ForeColor = System::Drawing::SystemColors::ActiveCaptionText;
this->btnClear->Location = System::Drawing::Point(147, 106);
this->btnClear->Name = L"btnClear";
this->btnClear->Size = System::Drawing::Size(83, 23);
this->btnClear->TabIndex = 4;
this->btnClear->Text = L"Clear";
this->btnClear->UseVisualStyleBackColor = true;
this->btnClear->Click += gcnew System::EventHandler(this, &Form1::btnClear_Click);
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::SystemColors::ActiveCaptionText;
this->ClientSize = System::Drawing::Size(284, 152);
this->Controls->Add(this->btnClear);
this->Controls->Add(this->btnGen);
this->Controls->Add(this->txtPass);
this->Controls->Add(this->lblTitle);
this->Controls->Add(this->menuStrip1);
this->ForeColor = System::Drawing::SystemColors::ButtonHighlight;
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::Fixed3D;
this->MainMenuStrip = this->menuStrip1;
this->MaximizeBox = false;
this->Name = L"Form1";
this->Text = L"PassGen v2.0";
this->menuStrip1->ResumeLayout(false);
this->menuStrip1->PerformLayout();
this->ResumeLayout(false);
this->PerformLayout();
}
void passGen() {
// Global Variables
srand(time(NULL));
int pass_Length = 0;
string password;
char characters [] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', ' ', '\0' };
int characters_Length = sizeof(characters) / sizeof(char);
// Generate the password.
while (pass_Length < 20) {
password += characters[rand()%characters_Length];
pass_Length++;
}
txtPass->Text = password;
}
#pragma endregion
private: System::Void aboutMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show(L"PassGen v2.0 by Darin Beaudreau\n\nThis program is free to distribute.");
}
private: System::Void instructionsMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
MessageBox::Show(L"Simply click the button to generate\na password. Future versions will have\nthe ability to generate multiple passwords.");
}
private: System::Void exitToolStripMenuItem_Click(System::Object^ sender, System::EventArgs^ e) {
// Exit the application.
Application::Exit();
}
private: System::Void btnClear_Click(System::Object^ sender, System::EventArgs^ e) {
txtPass->Text = "";
}
private: System::Void btnGen_Click(System::Object^ sender, System::EventArgs^ e) {
passGen();
}
};
}I know it's long, but I'd appreciate some help. EDIT: Here is the code for the original password generator. It works and compiles normally... so I don't understand what I did wrong in the new GUI version. #include <iostream>
#include <fstream.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
int main () {
// Global Variables
srand(time(NULL));
int pass_Length = 0;
string password;
char characters [] = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '\0' };
int characters_Length = sizeof(characters) / sizeof(char);
int userLength;
// Ask user the password length desired.
cout << "How long do you want your password to be? ";
cin >> userLength;
// Generate the password.
while (pass_Length < userLength) {
password += characters[rand()%characters_Length];
pass_Length++;
}
// Write the generated password to a file.
ofstream fout("password.txt");
fout << "The password generated was: " << password << endl;
fout << flush;
fout.close();
// End file creation.
// Clear screen and give exit message.
system("CLS");
cout << "Thank you for using PassGen++!" << endl;
cout << "\nYour password can be found in a file called password.txt in the \nfolder the program is in.\n" << endl;
system("PAUSE");
return 0;
}Can someone tell me the correct way to make this function work with my form? Last edited by PaCkEtPiRaTe; Nov 23rd, 2009 at 5:53 PM. Reason: Forgot information. |
|
|
|
|
#6 |
|
PFO God In Training
![]() ![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 1,783
Rep Power: 7
![]() |
Re: C++ GUI Programming
>>The compiler keeps telling me that "string" and "srand" and "rand" are all undefined
That's because neither are defined in any of the headf files you have included. string.h does NOT define c++ string class, <string> does.
__________________
PFO's FAQ is here |
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 8,035
Rep Power: 15
![]() ![]() |
Re: C++ GUI Programming
If you're going to program in C++, then you need to do some research. The old C header files have been renamed. They are NOT precisely the same files. The new files deal with the std:: namespace.
Files such as string.h are now cstring (without the extension). As an example, math.h would be cmath. A file such as "cstring" is NOT the same as "string". Nor is it precisely the same as "string.h," though it is highly similar. C++ appears, in many instances, to be a superset of C. Considering it thus works in many instances. Considering it thus is not a panacea. One example is that a C++ struct is not the same as a C struct. There are other differences. Do not rely on your knowledge of C to carry you through C++ programs. You will be sorely disappointed and your code will suffer greatly.
__________________
The person who coined the phrase, "There's no such thing as a stupid question," hasn't hung around here much. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers Politically Incorrect |
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
Re: C++ GUI Programming
Ok, so I replaced "string.h" with <cstring>...
It's still not working... same errors as before. Is there a new syntax or should "string password;" work? Also, why is srand showing as undefined? I've used it in programs before (hence, the password generator)... I've compiled it and run it with Visual Studio 2008 before and it ran fine... so what's the problem? |
|
|
|
|
#9 |
|
PFO God In Training
![]() ![]() Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 1,783
Rep Power: 7
![]() |
Re: C++ GUI Programming
why don't you learn how to use google? And read some c++ tutorials?
__________________
PFO's FAQ is here |
|
|
|
|
#10 |
|
Hobbyist Programmer
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 214
Rep Power: 4
![]() |
Why don't you learn to not try and discourage everyone who asks a simple question? I came here because I couldn't find the answer on Google or any other forums, not for your sarcasm... so please, either help me with my problem, or don't reply to the topic. Is that so hard?
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Java Programming Tutorial : Part I to GUI Programming | Raikiriu | Java | 0 | Feb 21st, 2009 2:22 AM |
| Programming With Java : Basic GUI | Raikiriu | Java | 0 | Feb 21st, 2009 1:29 AM |
| Starting C++ GUI Programming | kurt | C++ | 9 | Jun 17th, 2008 7:35 PM |
| Programming a simple GUI | bulio | Other Scripting Languages | 12 | Jun 9th, 2007 2:29 PM |