PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : [C++/CLR] Update Labels in Form1 Fehler


glow
2010-07-04, 19:56:09
Moin, hab folgendes Problem in einer Windows Forms anwendung unter C++(CLR)

Habe in Form1 folgende Methode drin

public: void UpdateLabels(String^ lbl1, String^ lbl2, String^ lbl3)
{
this->label1->Text = lbl1;
this->label2->Text = lbl2;
this->label3->Text = lbl3;
}

Dies rufe ich dann in einer extra Methode auf

form1->UpdateLabels("Hello", "world", String::Empty);

Dabei erhalte ich den Fehler
Fehler 2 error C2143: Syntaxfehler: Es fehlt ';' vor '->'

Ich hoffe ihr könnt mir hierbei helfen, bin da leider nochn ein Anfänger.

Gnafoo
2010-07-04, 20:05:12
Da ich in deinem Code gerade keinen Fehler sehe und das Ganze bei mir in einer schnell zusammengeklatschten Anwendung funktioniert, würde ich einmal vermuten, dass der Fehler außerhalb des von dir geposteten Codes liegt. Der Fehlermeldung nach zu urteilen, hast du irgendwo ein Semikolon am Ende der Zeile vergessen. Das passiert häufiger mal mit dem Semikolon nach class/struct-Blöcken (class {...}; ). Eventuell ist es das?

Im übrigen liest sich der Code besser, wenn man ihn in [code]...[ /code] verpackt (ohne Leerzeichen im zweiten Tag ;)).

Edit: wenn du es nicht findest kannst du ja nochmal den Code der vor dem UpdateLabels-Aufruf steht hier reinstellen.

glow
2010-07-04, 20:21:58
Hier mal der Code, für das komplette Projekt -> Klick mich (http://www.distraint.de/dev/Code.rar)
Form1

#pragma once

#include "addy_create.h"
#include "addy_view.h"

namespace CRMPWNY {

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>
/// Zusammenfassung für Form1
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{

public:
Form1(void)
{
InitializeComponent();
//
//TODO: Konstruktorcode hier hinzufügen.
//
}

protected:
/// <summary>
/// Verwendete Ressourcen bereinigen.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ b_a_create;
protected:

protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ close_button;
public: System::Windows::Forms::Label^ label1;
private:

private: System::Windows::Forms::Button^ button1;
public: System::Windows::Forms::Label^ label2;
private:
public: System::Windows::Forms::Label^ label3;



private:
/// <summary>
/// Erforderliche Designervariable.
/// </summary>
System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
/// <summary>
/// Erforderliche Methode für die Designerunterstützung.
/// Der Inhalt der Methode darf nicht mit dem Code-Editor geändert werden.
/// </summary>
void InitializeComponent(void)
{
this->b_a_create = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->close_button = (gcnew System::Windows::Forms::Button());
this->label1 = (gcnew System::Windows::Forms::Label());
this->button1 = (gcnew System::Windows::Forms::Button());
this->label2 = (gcnew System::Windows::Forms::Label());
this->label3 = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// b_a_create
//
this->b_a_create->Location = System::Drawing::Point(16, 39);
this->b_a_create->Name = L"b_a_create";
this->b_a_create->Size = System::Drawing::Size(75, 23);
this->b_a_create->TabIndex = 0;
this->b_a_create->Text = L"Anlegen";
this->b_a_create->UseVisualStyleBackColor = true;
this->b_a_create->Click += gcnew System::EventHandler(this, &Form1::b_a_create_Click);
//
// button2
//
this->button2->Location = System::Drawing::Point(16, 68);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Anzeigen";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
//
// close_button
//
this->close_button->Location = System::Drawing::Point(172, 68);
this->close_button->Name = L"close_button";
this->close_button->Size = System::Drawing::Size(75, 23);
this->close_button->TabIndex = 2;
this->close_button->Text = L"Schließen";
this->close_button->UseVisualStyleBackColor = true;
this->close_button->Click += gcnew System::EventHandler(this, &Form1::close_button_Click_1);
//
// 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->Location = System::Drawing::Point(12, 9);
this->label1->Name = L"label1";
this->label1->Size = System::Drawing::Size(91, 24);
this->label1->TabIndex = 3;
this->label1->Text = L"Adressen";
//
// button1
//
this->button1->Location = System::Drawing::Point(172, 39);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 4;
this->button1->Text = L"Readme";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
//
// label2
//
this->label2->AutoSize = true;
this->label2->Location = System::Drawing::Point(40, 173);
this->label2->Name = L"label2";
this->label2->Size = System::Drawing::Size(35, 13);
this->label2->TabIndex = 5;
this->label2->Text = L"label2";
//
// label3
//
this->label3->AutoSize = true;
this->label3->Location = System::Drawing::Point(172, 173);
this->label3->Name = L"label3";
this->label3->Size = System::Drawing::Size(35, 13);
this->label3->TabIndex = 6;
this->label3->Text = L"label3";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 294);
this->Controls->Add(this->label3);
this->Controls->Add(this->label2);
this->Controls->Add(this->button1);
this->Controls->Add(this->label1);
this->Controls->Add(this->close_button);
this->Controls->Add(this->button2);
this->Controls->Add(this->b_a_create);
this->Location = System::Drawing::Point(50, 50);
this->Name = L"Form1";
this->StartPosition = System::Windows::Forms::FormStartPosition::Manual;
this->Text = L"PWNY CRM";
this->ResumeLayout(false);
this->PerformLayout();

}
#pragma endregion

private: System::Void close_button_Click_1(System::Object^ sender, System::EventArgs^ e) {
Application::Exit();
}
private: System::Void b_a_create_Click(System::Object^ sender, System::EventArgs^ e) {
addy_create^ ac_var = gcnew addy_create();
ac_var ->ShowDialog();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e) {
addy_view^ av_var = gcnew addy_view();
av_var ->ShowDialog();
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
system("C:\\WINDOWS\\NOTEPAD.exe Readme.txt");
}
public: System::Void UpdateLabels(String ^lbl1, String ^lbl2, String^ lbl3)
{
label1->Text = lbl1;
label2->Text = lbl2;
label3->Text = lbl3;
}
};
}




Und hier die Datei wo der Aufruf erfolgt, bei der SetMethode


#include "StdAfx.h"
#include "CASP.h"

#include "addy_view.h"
#include <string>
#include <iostream>
#include "Form1.h"

using namespace CRMPWNY;
using namespace System;

CASP::CASP(void)
{
}
CASP::~CASP(void)
{
}
//Set Methode
void CASP::SetFields (int c)
{
Form1->UpdateLabels("Leer","bla","muh");
/*String^ asp_search_var;
String^ kundennummer_var = Convert::ToString(Kundennummer);
String^ file_path = "data\\";
String^ var_prefix_asp = "ASP_";
String^ asp_trennzeichen = "_";
String^ var_ASP_Nummer;
String^ Textfile = ".txt";

asp_search_var = file_path + var_prefix_asp + kundennummer_var + asp_trennzeichen + var_ASP_Nummer + Textfile;
System::IO::StreamReader^ ASP_Read = gcnew StreamReader(asp_search_var);
String^ asp_anr_var = ASP_Read->ReadLine();
String^ asp_vorname_var = ASP_Read->ReadLine();
String^ asp_nachname_var = ASP_Read->ReadLine();*/


//Form filling with a switch case
/*switch (c)
{
case '0':

addy_view::debug_label->Text = asp_anr_var;
addy_view::box_vorname->Text = asp_vorname_var;
addy_view::box_nachname->Text = asp_nachname_var;
break;
case '1':
addy_view::box_anrede2->Text = asp_anr_var;
addy_view::box_vorname2->Text = asp_vorname_var;
addy_view::box_nachname2->Text = asp_nachname_var;
break;
case '2':
addy_view::box_anrede3->Text = asp_anr_var;
addy_view::box_vorname3->Text = asp_vorname_var;
addy_view::box_nachname3->Text = asp_nachname_var;
break;
case '3':
addy_view::box_anrede4->Text = asp_anr_var;
addy_view::box_vorname4->Text = asp_vorname_var;
addy_view::box_nachname4->Text = asp_nachname_var;
break;
case '4':
addy_view::box_anrede5->Text = asp_anr_var;
addy_view::box_vorname5->Text = asp_vorname_var;
addy_view::box_nachname5->Text = asp_nachname_var;
break;
}*/
}
void CASP::SetKundennummer (int KNDNR)
{
Kundennummer = KNDNR;
}

Gnafoo
2010-07-04, 21:55:08
Okay du versuchst eine Instanzmethode (UpdateLabels) ohne Instanz aufzurufen. Du hast an der Stelle ja nur den Typ. Damit bleiben dir eigentlich folgende Alternativen:

(a) du reichst die Instanz von Form1 an CASP durch.
(b) du machst aus Form1 ein Singleton (dann kann das Fenster aber nur einmal geöffnet sein).
(c) du fischst dir aus Application::Windows das passende Fenster heraus.
(d) du änderst etwas am Design und ziehst den Darstellungskram gemäß MVC-Pattern aus dem Business-Object heraus.

glow
2010-07-04, 22:02:10
Hättest du mir evtl. ein Code Beispiel für die schnellste, bzw. eleganteste Lösung?

Ich tippe drauf dass das Form1 durchreichen schnell gehen sollte.

Gnafoo
2010-07-04, 22:27:45
Hm ich hab mit C++/CLI nicht viel am Hut, aber dürfte etwa so gehen:

In Form1.h

private:
System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
addy_view^ av_var = gcnew addy_view(this);
av_var->ShowDialog();
}


addy_view.h

ref class Form1;

public ref class addy_view : public System::Windows::Forms::Form
{
private:
Form1^ mainForm;

public:
addy_view(Form1^ form)
{
InitializeComponent();
mainForm = form;
}
//...
private:
System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
// ...
CASP Ansprechpartner[4];
for (int i=0; i = 4; i++)
{
Ansprechpartner[i] = CASP(mainForm);
Ansprechpartner[i].SetKundennummer(Privatadresse.GetKundennummer());
Ansprechpartner[i].SetFields(i);
}
// ...
}
};


CASP.h:

ref class Form1;

class CASP
{
private:
Form1^ mainForm;
// ...

public:
// Könnte sein, dass man einen zusätzlichen parameterlosen
// Konstruktor braucht, damit man das Array in addy_view
// erstellen kann.
CASP(Form1^ form);
};


CASP.cpp

#include "Form1.h"
// ...

CASP::CASP(Form1^ form) : mainForm(form) { }
// ...

void CASP::SetFields (int c)
{
mainForm->UpdateLabels("Leer","bla","muh");
}


Abhängig davon was CASP machen soll ist es evtl. sinnvoll, den Code in addy_view herauszuziehen oder diese Konstruktion mit dem Array zu ändern. Aber das kann ich hier nicht wirklich beurteilen.

glow
2010-07-04, 22:50:37
Funktioniert leider noch nicht aber du hast mir schonmal weitergeholfen.
Da ich somit rauslesen kann wie ich an die Klasse CASP die Form addy_view übergeben kann.

Sinn dahinter ist, ich lese in der Methode von CASP ()setfields) die Werte aus einer Textfiles und diese sollen dann nacheinander in der addy_view Maske ausgefüllt werden.