Form Help

Hi
Im using Visual Studio 2008 Professional
I need some help with forms.
Im trying to edit a form object property in a c++ file instead of a form file.

Example:

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
// kek.cpp : main project file.

#include "stdafx.h"
#include "Form1.h"
#include <string>

using namespace System;
using namespace System::Windows::Forms;
using namespace kek;
using namespace std;

bool isEnabled = false;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Form1());

	// CODE HERE
	
	Form1::label1::Text = "blah blah blah";
	
	return 0;
}


Quick question: Is it possible? Or how is it done, if it is.
Yes it is possibly.

First you need to create a static constructor in Form1 and create the label there.
Second you need to declare a static property to return the label.

Form1.h
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
#pragma once


namespace FormDemo 
{
  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
  ///
  /// Warnung: Wenn Sie den Namen dieser Klasse ändern, müssen Sie auch
  ///          die Ressourcendateiname-Eigenschaft für das Tool zur Kompilierung verwalteter Ressourcen ändern,
  ///          das allen RESX-Dateien zugewiesen ist, von denen diese Klasse abhängt.
  ///          Anderenfalls können die Designer nicht korrekt mit den lokalisierten Ressourcen
  ///          arbeiten, die diesem Formular zugewiesen sind.
  /// </summary>
  public ref class Form1 : public System::Windows::Forms::Form
  {
  public:
    Form1(void)
    {
      InitializeComponent();
      
      MyLabel->Parent = this;
    }
    static Form1(void)
    {
      MyLabel = gcnew Label();
      MyLabel->AutoSize = true;
      MyLabel->Location = System::Drawing::Point(42, 69);
      MyLabel->Name = L"MyLabel";
      MyLabel->Size = System::Drawing::Size(46, 17);
      MyLabel->TabIndex = 0;
      MyLabel->Text = L"MyLabel";
    }

  protected:
    /// <summary>
    /// Verwendete Ressourcen bereinigen.
    /// </summary>
    ~Form1()
    {
      if (components)
      {
        delete components;
      }
    }

  protected: 

  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->SuspendLayout();
      // 
      // Form1
      // 
      this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
      this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
      this->ClientSize = System::Drawing::Size(282, 255);
      this->Name = L"Form1";
      this->Text = L"Form1";
      this->ResumeLayout(false);

    }
#pragma endregion
  private:
    static Label^ MyLabel; // = gcnew Label();
  public:
    static property Label^ label1
    {
      Label^ get()
      {
        return MyLabel;
      }
    }
  };
}



FormDemo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// FormDemo.cpp: Hauptprojektdatei.

#include "stdafx.h"
#include "Form1.h"

using namespace FormDemo;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Aktivieren visueller Effekte von Windows XP, bevor Steuerelemente erstellt werden
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 
  Form1::label1->Text = "Hello world";
	Application::Run(gcnew Form1());
	return 0;
}
Topic archived. No new replies allowed.