Jest jakaś opcja, żeby iterować button? W sumie gdyby stworzyć tablicę buttonów to by się dało, ale żeby ją stworzyć to jak chce ominąc ręczne wypełnianie tablicy to musze je iterować.
Wybacz. Późno było i widzę, że trochę nie jasno sprecyzowałem pytanie. Opiszę mój problem raz jeszcze na konkretnym przykładzie. Chciałbym iterować w pętli 16 buttonów:
for(int i=1;i<=16;i++)
{
button[i]->Text=i;
}
Taki zapis od razu ort! mi się z tablicą komponentów. No ale przecież żeby ją stworzyć to tak samo w pętli muszę zrobić iteracje, której nie umiem:
for(int i=1;i<=16;i++)
{
tablica[i]->Text=i;
}
Jest jakaś możliwość iteracji? W ostateczności będę przypisywać ręcznie:
button t[16];
t[0]=button1;
t[1]=button2; ...
Z góry dziękuję za pomoc.
const int ileButtonow = 16 ;
TButton*tab[ileButtonow];
for (int i = 0 ; i < ileButtonow ;i++)
{
tab[i]=new TButton(this);
tab[i]->Parent=this;
tab[i]->Width = 25;
tab[i]->Left =+ tab[i]->Width*i ;
tab[i]->Caption = i+1 ;
}
Hmm wygląda na to, że źle się zrozumieliśmy. Do dnia dzisiejszego mimo, że cały czas kombinuje i czytam nie udało mi się zrobić tej tablicy. Ostatnia nadzieja w forum :-) Napiszę od początku co mam i co chciałbym z tego uzyskać.
Nadmienię najpierw, że korzystam z podstawowych bibliotek programu Visual Studio 2008 Professional. Tak wygląda mój program:
#pragma once
namespace My4 {
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::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::Button^ button3;
private: System::Windows::Forms::Button^ button4;
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->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->button3 = (gcnew System::Windows::Forms::Button());
this->button4 = (gcnew System::Windows::Forms::Button());
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Drawing::Point(12, 12);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
//
// button2
//
this->button2->Location = System::Drawing::Point(12, 41);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"button2";
this->button2->UseVisualStyleBackColor = true;
//
// button3
//
this->button3->Location = System::Drawing::Point(12, 70);
this->button3->Name = L"button3";
this->button3->Size = System::Drawing::Size(75, 23);
this->button3->TabIndex = 2;
this->button3->Text = L"button3";
this->button3->UseVisualStyleBackColor = true;
//
// button4
//
this->button4->Location = System::Drawing::Point(12, 99);
this->button4->Name = L"button4";
this->button4->Size = System::Drawing::Size(75, 23);
this->button4->TabIndex = 3;
this->button4->Text = L"button4";
this->button4->UseVisualStyleBackColor = true;
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(284, 262);
this->Controls->Add(this->button4);
this->Controls->Add(this->button3);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
}
#pragma endregion
};
}
Oczywiście jest to tylko prototyp, żeby dokładniej zilustrować mój problem. Chciałbym utworzyć tablicę składającą się z tych 4 buttonów (button1, button2, button3, button4) - odpowiednio:
tablicabuttonow[0]=button1;
tablicabuttonow[1]=button2;
tablicabuttonow[2]=button3;
tablicabuttonow[3]=button4;
Jak już przypiszę buttony do tablicy to chciałbym się do nich odwoływać w sposób przedstawony poniżej:
t[0]->Text="xxx";
Oczywiście w moim kodzie nie ma tablicy na ów komponenty gdyż właśnie nie potrafię jej utworzyć. Bardzo proszę o pomoc i z góry dziękuję za wszystkie wysiłki.
Pozdrawiam.
nie możesz przechowywać w tablicy wskaźników do tych buttonów?
Może być i tak. Mogę prosić o konkretny przykład? To co powiedziałeś widzę tak:
Button^ *tab=new Button^ [2];
Co tu jest źle?
Bladowski napisał(a)
Może być i tak. Mogę prosić o konkretny przykład? To co powiedziałeś widzę tak:
Button^ *tab=new Button^ [2];
Co tu jest źle?
Wskaźnik * mający udawać niezarządzalną tablicę którego elementami są zarządzalne referencje.
Użyj Array<Button> albo jeszcze lepiej List<Button>:
List<Button^>^ lista=gcnew List<Button^>^();
// dodawanie elementu
lista->Add(button1);
// iterowanie po liście
for each (Button^ b in lista) b->Text="ala ma kota";
// albo
for (int i=0;i<lista->Count;i++) lista[i]->Text="ala ma kota";
// reszta w helpie.
Temat widzę dość stary jednak muszę go odświeżyć.
A mianowicie stworzyłem tablicę buttonów:
#pragma once
namespace ukladankanew {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::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::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::Button^ button1;
private: array <System::Button> Plansza;
protected:private:
/// <summary>
/// Required designer variable.
/// </summary>
System::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->button1 = (gcnew System::Button());
Plansza = gcnew arraySystem::Windows::Forms::Button^(9);
for(int i=0; i<9; i++)
{
Plansza[i] = (gcnew System::Button());
}
this->SuspendLayout();
//
// button1
//
this->button1->Location = System::Point(108, 60);
this->button1->Name = L"button1";
this->button1->Size = System::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"button1";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);//button2 this->Plansza[0]->Location = System::Drawing::Point(108, 60); this->Plansza[0]->Name = L"button1"; this->Plansza[0]->Size = System::Drawing::Size(75, 23); this->Plansza[0]->TabIndex = 0; this->Plansza[0]->Text = L"button1"; this->Plansza[0]->UseVisualStyleBackColor = true; <b>//this->Plansza[0]->Click += gcnew System::EventHandler(this, &Form1::Plansza[0]_Click);</b> // // Form1 // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(284, 262); this->Controls->Add(this->button1); this->Name = L"Form1"; this->Text = L"Form1"; this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load); this->ResumeLayout(false); }
#pragma endregion
private: System::Void Form1_Load(System::Object sender, System::EventArgs e) {
}
private: System::Void button1_Click(System::Object sender, System::EventArgs e) {
}
};
}
button1 jako przykład. Problem pojawia się tam gdzie pogrubione. Ma ktoś pomysł jak to poprawić aby działało?
FYI: pogrubianie w tagach < code > NIE działa, zmieniłem Ci na quote
odpowiedź właściwa: w cytacie poniżej pojawia się problem:
button1 jako przykład. Problem pojawia się tam gdzie pogrubione. Ma ktoś pomysł jak to poprawić aby działało?
masz pomysł jak go rozwiązać? podpowiem Ci - nie napisałeś JAKI masz problem z zaznaczona linijką..