| | |
Borland C++ Builder 6 : creating new objects - HOW ??
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2006
Posts: 16
Reputation:
Solved Threads: 0
Hi, I am trying to make small game , but I am confronting with a problem. I have a function that should add a new button to my form when it's called. The problem is that I don't know how to create a new object (a button, a shape or whatever...) and adding it to my form. In Java, this would be fairly simple, but how do I it here ? my header file contains the form class:
At public I've added a test button (TestBtn). The same problem: how can use it ? It must be first allocated (and how)?
Thanx !
class TForm1 : public TForm
{
__published: // IDE-managed Components
TShape *Shape1;
TButton *Button1;
void __fastcall Button1Click(TObject *Sender);
private: // User declarations
public: // User declarations
TButton *TestBtn ;
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;At public I've added a test button (TestBtn). The same problem: how can use it ? It must be first allocated (and how)?
Thanx !
c++ Syntax (Toggle Plain Text)
//... void __fastcall TForm1::example(void) { TestBtn = new TButton(); TestBtn->Visible = false; }
•
•
Join Date: Aug 2008
Posts: 206
Reputation:
Solved Threads: 31
TButton's constructor needs to be passed a pointer to a TComponent (eg a parent form) that is responsible for managing the button.
It is also necessary to set various attributes: position, caption, visibility, the function to be called when the newly created button is created, etc etc. TButton is derived from TWinControl, so some inherited attributes probably also need to be set.
It would probably be easier to create a button on your form at design time, and have your function make that button visible as needed.
It is also necessary to set various attributes: position, caption, visibility, the function to be called when the newly created button is created, etc etc. TButton is derived from TWinControl, so some inherited attributes probably also need to be set.
It would probably be easier to create a button on your form at design time, and have your function make that button visible as needed.
c++ Syntax (Toggle Plain Text)
TestBtn = new TButton(this); TestBtn->Parent = this; TestBtn->Caption = "Check";
•
•
Join Date: Sep 2006
Posts: 16
Reputation:
Solved Threads: 0
Ok , this sorted things out a little bit. My next problem is, how to assign each button a different function when it's pressed.For example I do something like this: where myfunction is defined like this in the header file: I get an "Not an allowed type" error when compiling. It seems that I can't pass a variable like this to myfunction. But, if I do like this: it works, but it doesn't do what I wanted it to do.But how do I know which of the ten buttons is clicked now? How can I pass this information to the function?
Thanx,
Adrian
for (x = 0; x < 10; x++)
{
TButton *TestBtn = new TButton(this);
TestBtn->Caption = "My button " + IntToStr(x);
TestBtn->Top = 100;
TestBtn->Left = 100 + 10 * x;
TestBtn->OnClick = myfunction(x);
TestBtn->Parent = this;
}void __fastcall TForm1::myfunction(int x)
{
ShowMessage("Button " + IntToStr(x) + " clicked");
}TestBtn->OnClick = myfunction; with myfunction looking now : void __fastcall TForm1::myfunction(TObject *Sender)
{
ShowMessage("Button ? clicked");
}Thanx,
Adrian
c++ Syntax (Toggle Plain Text)
class TForm1:public TForm { private: // create temp var.. int m_x; //... }; void __fastcall TForm1::myfunction1(int x) { ShowMessage("Button " + IntToStr(x) + " clicked"); } void __fastcall TForm1::myfunction(TObject *Sender) { myfunction1(m_x); } //###################################### TButton *TestBtn = new TButton(this); TestBtn->OnClick = myfunction; TestBtn->Parent = this; for (x = 0; x < 10; x++) { TestBtn->Caption = "My button " + IntToStr(x); TestBtn->Top = 100; TestBtn->Left = 100 + 10 * x; m_x = x; TestBtn->OnClick(this/*sender*/); } //######################################
Last edited by cikara21; Dec 26th, 2008 at 5:23 pm.
try yhis...
c++ Syntax (Toggle Plain Text)
//###################################### class TForm1:public TForm { //... public: TButton *TestBtn[10] //... }; //###################################### for (x = 0; x < 10; x++) { TestBtn[i] = new TButton(this); TestBtn[i]->Parent = this; TestBtn[i]->OnClick = myfunction; TestBtn[i]->Caption = "My button " + IntToStr(x); TestBtn[i]->Top = 100; TestBtn[i]->Left = 100 + 10 * x; m_x = x; } //######################################
Last edited by cikara21; Dec 26th, 2008 at 7:11 pm.
![]() |
Similar Threads
- Help for learnign JSP (JSP)
Other Threads in the C++ Forum
- Previous Thread: VC++ 8.0 Express & Unicode
- Next Thread: Pointers and winforms?
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news node output parameter pointer problem program programming project proxy python read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





