|
Programmer
Join Date: Aug 2007
Posts: 34
Rep Power: 0 
|
Re: C++ GUI Programming
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 6:53 PM.
Reason: Forgot information.
|