Klassen-Definition von Arrays



  • Ich hoffe jeder hat die Weihnachtstage gut überstanden und ist gewappnet für Sylvester. Die Feiertage bringen jedoch auch Probleme mit sich; bei mir folgendes:^^

    Man nehme ein ganz normales array von RadioButtons:

    [...]
    array<RadioButton^>^ myRadioButtons;
    [...]
    
    void RadioButtonArray(void)
    {
    	int Anzahl = 5;
    	myRadioButtons = gcnew array<RadioButton^>(Anzahl);
    
    	int YAbstand = 20;
    
    	for( int i = 0; i < Anzahl; i++ )
    	{
    		this->myRadioButtons[i] = gcnew RadioButton();
    		this->myRadioButtons[i]->Parent = this;
    		this->myRadioButtons[i]->Name = L"MyRadioButton" + i;
    		this->myRadioButtons[i]->Text = L"RadioButton " + (i+1);
    		this->myRadioButtons[i]->Location = System::Drawing::Point( 20, YAbstand );
    		this->myRadioButtons[i]->Size = System::Drawing::Size( 120, 20 );
    		this->myRadioButtons[i]->Checked = false;
    		YAbstand += 20;
    	}
    }
    

    Nun würde ich gerne eine Klasse definieren, in der ich einfach nur die Form, das entsprechenden RadioButtonArray und die Anzahl der Buttons auswählen muss. Bei einem einzelnen Element funktioniert dies auch:

    //RadioButtonClass.h
    
    #pragma once
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    public ref class RadioButtonClass
    {
    	static RadioButton ^myRadioButton = gcnew RadioButton();
    
    public:
    	static void Main( RadioButton ^myRadioButton, String ^RBName, String ^RBText );
    };
    
    //RadioButtonClass.cpp
    
    #include "stdAfx.h"
    #include "RadioButtonClass.h"
    
    void ClassArray::Main( RadioButton ^myRadioButton, System::String ^RBName, System::String ^RBText )
    {
    		myRadioButton->Location = System::Drawing::Point( 20, 20 );
    		myRadioButton->Size = System::Drawing::Size( 120, 20 );
    		myRadioButton->Name = RBName;
    		myRadioButton->Text = RBText;
    		myRadioButton->Checked = false;
    }
    
    //Form1.h
    
    void RadioButton(void)
    {
    	this->neuerButton = gcnew RadioButton();
    	this->Controls->Add(this->neuerButton);
    	RadioButtonClass^ neueInstanz = gcnew ClassArray;
    	neueInstanz->Main( neuerButton, "RB1", "RadioButton 1" );
    }
    

    Wenn ich jedoch ein Array auf diese Art definieren möchte, bekomme ich das Problem, dass er mir das Array zwar erstellt, ich es aber nicht angezeigt bekomme. Dies resultiert daraus, dass ich bei dem Array ja schlecht einen this-pointer verwenden kann ( sehen wir mal davon ab, dass static sowieso keinen this-pointer hat ).

    Was ich gerne wissen möchte ist also wie ich definieren kann, dass das von mir gewünschte array auf einer Form liegt, die ich selbst angebe. Das stelle ich mir ungefähr so vor:

    //ArrayClass.h
    
    #pragma once
    
    using namespace System;
    using namespace System::Windows::Forms;
    
    public ref class ClassArray
    {
            int x;
    	array<RadioButton^> ^myRadioButton = gcnew array<RadioButton^>(x);
            Form ^myForm
    
    public:
    	void Main( Form^ myForm, array<RadioButton^> ^myRadioButton, x );
    };
    
    //ArrayClass.cpp
    
    #include "stdAfx.h"
    #include "ArrayClass.h"
    
    void ClassArray::Main( Form^ myForm, array<RadioButton^> ^myRadioButton, x )
    {
            //Hier besteht mein Problem*
    }
    

    *Ich weiß wie gesagt nicht,wie ich definiere, dass die RadioButtons auf der von mir gewünschten Form (anzugeben in Form^ myForm) sein sollen ...

    Das wars in aller Ausführlichkeit, hoffe auf Hilfe! Besten Dank!



  • Sowas schon versucht:?

    foreach (RadioButton ^ r in myRadioButton)
    {
        myForm->Controls->Add(r);
    }
    

    Spricht was dagegen, das Radiobutton-Array gleich dem Form-Konstruktor zu übergeben? Dann funktionierts auch wieder mit "this"


Anmelden zum Antworten