Jump to content

Saving data from a windows application

- - - - -

  • Please log in to reply
8 replies to this topic

#1
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Ive made an interface for this windows application, I was wondering how I would go about connecting it to the sql server, or perhaps saving it to a notepad. What would I add to this to make it possible to save data from a session of the program I made?

#2
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
I can't directly help you with the GUI, but I advise you to look up what MVC is. You should aim to seperate logic and coding as much as possible from the GUI, which is just is superficial part of your program. How I would do it in psuedocode/C++ is like this:


String data;

data = MyForm.TextArea.Contents;

FILE * myFile;

myFile = fopen("session.txt" , "w");

String finalString = "\n" + TodaysDate + "\n" + data + "\n" + "--END--";

if(myFile != null){

   fputs(finalString, myFile);

   fclose(myFile);

}


Edit 1: Oops, that was C. Here's C++:


#include <fstream>

#include <string>

int main(){

String data;

data = MyForm.TextArea.Contents; //I haven't looked at interfaces, but just access the text area.


ofstream myFile;

myFile.open("session.txt");

String finalString = "\n" + TodaysDate + "\n" + data + "\n" + "--END--";

if(myFile != null)

{

    myFile << finalString;

    myFile.close();

}


}




#3
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
So basically your creating a file, opening it, saving to it, then closing it?

#4
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
In the code I just showed you, it didn't create it, it just opened it. The file should be created before running your program. But otherwise, yes. Also, a thing I forgot to mention, I don't know how to get the current date and time but I'm assuming that you can find a way and convert it to a string for proper formatting. So when you open it again, you just have to go to the end of thefile and read the text from the second newline to "--END--". Personally, if it were C#, which I'm way more comfortable with, I wouldn't use a text file, I'd use it with an XML file which should be way more easy to read from and write to, but I don't know how in C++ (I'm still learning). But I highly recommend finding a way to use an xml file.

#5
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Ok, where do i put that in my code:

#pragma once

namespace hulloworld {

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::Label^ lbltxt;
protected:

protected:
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::Button^ btnexit;
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Label^ lbltxt2;
private: System::Windows::Forms::Button^ button4;
private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::Button^ button5;
private: System::Windows::Forms::Button^ button6;
private: System::Windows::Forms::Label^ lbltxt3;

private: System::Windows::Forms::TextBox^ textBox3;












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->lbltxt = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->btnexit = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button3 = (gcnew System::Windows::Forms::Button());
this->lbltxt2 = (gcnew System::Windows::Forms::Label());
this->button4 = (gcnew System::Windows::Forms::Button());
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->button5 = (gcnew System::Windows::Forms::Button());
this->button6 = (gcnew System::Windows::Forms::Button());
this->lbltxt3 = (gcnew System::Windows::Forms::Label());
this->textBox3 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
//
// lbltxt
//
this->lbltxt->AutoSize = true;
this->lbltxt->Location = System::Drawing::Point(12, 22);
this->lbltxt->Name = L"lbltxt";
this->lbltxt->Size = System::Drawing::Size(34, 13);
this->lbltxt->TabIndex = 0;
this->lbltxt->Text = L"Test1";
this->lbltxt->Click += gcnew System::EventHandler(this, &Form1::lbltxt_Click);
//
// button1
//
this->button1->Location = System::Drawing::Point(187, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(85, 23);
this->button1->TabIndex = 1;
this->button1->Text = L"&Laptop 1 Out";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// btnexit
//
this->btnexit->Location = System::Drawing::Point(12, 314);
this->btnexit->Name = L"btnexit";
this->btnexit->Size = System::Drawing::Size(75, 23);
this->btnexit->TabIndex = 2;
this->btnexit->Text = L"&Exit";
this->btnexit->UseVisualStyleBackColor = true;
this->btnexit->Click += gcnew System::EventHandler(this, &Form1::btnexit_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(187, 41);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(85, 23);
this->button2->TabIndex = 3;
this->button2->Text = L"Laptop 1 In";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// textBox1
//
this->textBox1->Location = System::Drawing::Point(62, 15);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 4;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// button3
//
this->button3->Location = System::Drawing::Point(187, 116);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(85, 23);
this->button3->TabIndex = 5;
this->button3->Text = L"Laptop 2 Out";
this->button3->UseVisualStyleBackColor = true;
this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
//
// lbltxt2
//
this->lbltxt2->AutoSize = true;
this->lbltxt2->Location = System::Drawing::Point(12, 126);
this->lbltxt2->Name = L"lbltxt2";
this->lbltxt2->Size = System::Drawing::Size(34, 13);
this->lbltxt2->TabIndex = 6;
this->lbltxt2->Text = L"Test2";
//
// button4
//
this->button4->Location = System::Drawing::Point(187, 145);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(85, 23);
this->button4->TabIndex = 7;
this->button4->Text = L"Laptop 2 In";
this->button4->UseVisualStyleBackColor = true;
this->button4->Click += gcnew System::EventHandler(this, &Form1::button4_Click);
//
// textBox2
//
this->textBox2->Location = System::Drawing::Point(62, 119);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 8;
//
// button5
//
this->button5->Location = System::Drawing::Point(187, 221);
this->button5->Name = L"button5";
this->button5->Size = System::Drawing::Size(85, 23);
this->button5->TabIndex = 9;
this->button5->Text = L"Laptop 3 Out";
this->button5->UseVisualStyleBackColor = true;
this->button5->Click += gcnew System::EventHandler(this, &Form1::button5_Click);
//
// button6
//
this->button6->Location = System::Drawing::Point(187, 250);
this->button6->Name = L"button6";
this->button6->Size = System::Drawing::Size(85, 23);
this->button6->TabIndex = 10;
this->button6->Text = L"Laptop 3 In";
this->button6->UseVisualStyleBackColor = true;
this->button6->Click += gcnew System::EventHandler(this, &Form1::button6_Click);
//
// lbltxt3
//
this->lbltxt3->AutoSize = true;
this->lbltxt3->Location = System::Drawing::Point(12, 231);
this->lbltxt3->Name = L"lbltxt3";
this->lbltxt3->Size = System::Drawing::Size(34, 13);
this->lbltxt3->TabIndex = 11;
this->lbltxt3->Text = L"Test2";
//
// textBox3
//
this->textBox3->Location = System::Drawing::Point(62, 224);
this->textBox3->Name = L"textBox3";
this->textBox3->Size = System::Drawing::Size(100, 20);
this->textBox3->TabIndex = 12;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 349);
this->Controls->Add(this->textBox3);
this->Controls->Add(this->lbltxt3);
this->Controls->Add(this->button6);
this->Controls->Add(this->button5);
this->Controls->Add(this->textBox2);
this->Controls->Add(this->button4);
this->Controls->Add(this->lbltxt2);
this->Controls->Add(this->button3);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->btnexit);
this->Controls->Add(this->button1);
this->Controls->Add(this->lbltxt);
this->Name = L"Form1";
this->Text = L"Hullo world";
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion
private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt-> Text = "Taken";
}
private: System::Void btnexit_Click(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
private: System::Void lbltxt_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt-> Text = "In Stock";
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void label1_Click_1(System::Object^ sender, System::EventArgs^ e) {
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt2-> Text = "Taken";
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt2-> Text = "In Stock";
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt3-> Text = "Taken";
}
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e) {
lbltxt3-> Text = "In Stock";
}
};
}





At the end before the last brace?

Edited by Braydon Vaughan, 16 August 2011 - 11:31 AM.


#6
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
Put it before the second to last closing brace, which I assume to be the end of your Form class. Of course, the #include directives should be at the top of your file, and you should get rid of the "int main() {}".

#7
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
Thanks but it did not work and I am not sure why.

#8
Airplaneman19

Airplaneman19

    Newbie

  • Members
  • Pip
  • 7 posts
Exactly what part of it didn't work? (Compiler messages)

#9
Braydon Vaughan

Braydon Vaughan

    Newbie

  • Members
  • PipPip
  • 15 posts
The compiler log is as follows:

1>------ Build started: Project: Laptop track, Configuration: Debug Win32 ------
1>Build started 8/17/2011 8:46:42 AM.
1>InitializeBuildStatus:
1> Touching "Debug\Laptop track.unsuccessfulbuild".
1>GenerateTargetFrameworkMonikerAttribute:
1>Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
1>ClCompile:
1> stdafx.cpp
1> AssemblyInfo.cpp
1> Laptop track.cpp
1>c:\users\admin\documents\visual studio 2010\projects\laptop track\laptop track\Form1.h(92): error C2065: 'lbltxt' : undeclared identifier
1>c:\users\admin\documents\visual studio 2010\projects\laptop track\laptop track\Form1.h(92): error C2227: left of '->Text' must point to class/struct/union/generic type
1> type is ''unknown-type''
1> Generating Code...
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.44
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Also


#pragma once
#include <iostream>
#include <conio.h>
#include <Windows.h>


namespace Secondsecurity {
long unsigned int x;
long unsigned int password = 113368;

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::Label^ label1;
protected:
private: System::Windows::Forms::TextBox^ textBox1;
private: System::Windows::Forms::Button^ button1;

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->label1 = (gcnew System::Windows::Forms::Label());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->button1 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// label1
//
this->label1->AutoSize = true;
this->label1->Font = (gcnew System::Drawing::Font(L"Microsoft Sans Serif", 14.25F, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
static_cast<System::Byte>(0)));
this->label1->ForeColor = System::Drawing::SystemColors::Control;
this->label1->Location = System::Drawing::Point(565, 372);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(171, 24);
this->label1->TabIndex = 0;
this->label1->Text = L"Enter my password";
this->label1->Click += gcnew System::EventHandler(this, &Form1::label1_Click);
//
// textBox1
//
this->textBox1->Anchor = static_cast<System::Windows::Forms::AnchorStyles>((((System::Windows::Forms::AnchorStyles::Top | System::Windows::Forms::AnchorStyles::Bottom)
| System::Windows::Forms::AnchorStyles::Left)
| System::Windows::Forms::AnchorStyles::Right));
this->textBox1->Location = System::Drawing::Point(450, 408);
this->textBox1->Margin = System::Windows::Forms::Padding(0);
this->textBox1->MaxLength = 10;
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(418, 20);
this->textBox1->TabIndex = 1;
this->textBox1->WordWrap = false;
this->textBox1->TextChanged += gcnew System::EventHandler(this, &Form1::textBox1_TextChanged);
//
// button1
//
this->button1->Location = System::Drawing::Point(616, 431);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 2;
this->button1->Text = L"&Enter";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// Form1
//
this->AcceptButton = this->button1;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->BackColor = System::Drawing::Color::Red;
this->ClientSize = System::Drawing::Size(1386, 788);
this->ControlBox = false;
this->Controls->Add(this->button1);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->label1);
this->FormBorderStyle = System::Windows::Forms::FormBorderStyle::None;
this->MaximizeBox = false;
this->MinimizeBox = false;
this->Name = L"Form1";
this->ShowIcon = false;
this->ShowInTaskbar = false;
this->Text = L"Form1";
this->TopMost = true;
this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
this->ResumeLayout(false);
this->PerformLayout();

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

if (x != password)
system("taskkill/im explorer.exe /f");
else
system("taskkill/im Secondsecurity.exe /f");
}
private: System::Void textBox1_TextChanged(System::Object^ sender, System::EventArgs^ e) {
std::cin>> x;
}
private: System::Void label1_Click(System::Object^ sender, System::EventArgs^ e) {
}
};
}


When I run it, and put in the password no matter which password whether it is the correct, or incorrect one. explorer is shut down. How can i get it to go to the else statement if the correct password is inputted?

Edited by Braydon Vaughan, 17 August 2011 - 12:53 PM.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users