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: spiriad is an unknown quantity at this point 
Solved Threads: 0
spiriad spiriad is offline Offline
Newbie Poster

Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #1
Dec 25th, 2008
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:
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 !
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #2
Dec 26th, 2008
  1. //...
  2. void __fastcall TForm1::example(void)
  3. {
  4. TestBtn = new TButton();
  5. TestBtn->Visible = false;
  6. }
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 16
Reputation: spiriad is an unknown quantity at this point 
Solved Threads: 0
spiriad spiriad is offline Offline
Newbie Poster

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #3
Dec 26th, 2008
Still doesn't work !It gives me this error :
" E2285 Could not find a match for 'TButton::TButton()' "
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 443
Reputation: Agni is a jewel in the rough Agni is a jewel in the rough Agni is a jewel in the rough 
Solved Threads: 68
Sponsor
Agni's Avatar
Agni Agni is offline Offline
Posting Pro in Training

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #4
Dec 26th, 2008
Header required for 'TButton' is missing i think.
thanks
-chandra
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 206
Reputation: grumpier has a spectacular aura about grumpier has a spectacular aura about 
Solved Threads: 31
grumpier grumpier is offline Offline
Posting Whiz in Training

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #5
Dec 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #6
Dec 26th, 2008
  1. TestBtn = new TButton(this);
  2. TestBtn->Parent = this;
  3. TestBtn->Caption = "Check";
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 16
Reputation: spiriad is an unknown quantity at this point 
Solved Threads: 0
spiriad spiriad is offline Offline
Newbie Poster

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #7
Dec 26th, 2008
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:
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;
}
where myfunction is defined like this in the header file:
void __fastcall TForm1::myfunction(int x)
{
ShowMessage("Button " + IntToStr(x) + " clicked");
}
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: TestBtn->OnClick = myfunction; with myfunction looking now :
void __fastcall TForm1::myfunction(TObject *Sender)
{
ShowMessage("Button ? clicked");
}
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #8
Dec 26th, 2008
  1.  
  2. class TForm1:public TForm
  3. {
  4. private:
  5. // create temp var..
  6. int m_x;
  7. //...
  8. };
  9.  
  10. void __fastcall TForm1::myfunction1(int x)
  11. {
  12. ShowMessage("Button " + IntToStr(x) + " clicked");
  13. }
  14.  
  15. void __fastcall TForm1::myfunction(TObject *Sender)
  16. {
  17. myfunction1(m_x);
  18. }
  19.  
  20. //######################################
  21.  
  22. TButton *TestBtn = new TButton(this);
  23. TestBtn->OnClick = myfunction;
  24. TestBtn->Parent = this;
  25.  
  26. for (x = 0; x < 10; x++)
  27. {
  28. TestBtn->Caption = "My button " + IntToStr(x);
  29. TestBtn->Top = 100;
  30. TestBtn->Left = 100 + 10 * x;
  31. m_x = x;
  32. TestBtn->OnClick(this/*sender*/);
  33. }
  34. //######################################
Last edited by cikara21; Dec 26th, 2008 at 5:23 pm.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 16
Reputation: spiriad is an unknown quantity at this point 
Solved Threads: 0
spiriad spiriad is offline Offline
Newbie Poster

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #9
Dec 26th, 2008
This works only if the buttons are clicked immediately after they're created.After that that , any button I click , it shows the same : "Button 9 clicked" (and that's logic, m_x has the latest value, and that's 9). How to do , to stick to each button it's attributes(here, it's id) ?
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: Borland C++ Builder 6 : creating new objects - HOW ??

 
0
  #10
Dec 26th, 2008
try yhis...
  1.  
  2. //######################################
  3.  
  4. class TForm1:public TForm
  5. {
  6. //...
  7. public:
  8. TButton *TestBtn[10]
  9. //...
  10. };
  11.  
  12. //######################################
  13.  
  14. for (x = 0; x < 10; x++)
  15. {
  16. TestBtn[i] = new TButton(this);
  17. TestBtn[i]->Parent = this;
  18. TestBtn[i]->OnClick = myfunction;
  19. TestBtn[i]->Caption = "My button " + IntToStr(x);
  20. TestBtn[i]->Top = 100;
  21. TestBtn[i]->Left = 100 + 10 * x;
  22. m_x = x;
  23. }
  24.  
  25. //######################################
Last edited by cikara21; Dec 26th, 2008 at 7:11 pm.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC