943,708 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 5245
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 25th, 2008
0

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

Expand Post »
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 !
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spiriad is offline Offline
16 posts
since Sep 2006
Dec 26th, 2008
0

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

c++ Syntax (Toggle Plain Text)
  1. //...
  2. void __fastcall TForm1::example(void)
  3. {
  4. TestBtn = new TButton();
  5. TestBtn->Visible = false;
  6. }
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 26th, 2008
0

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

Still doesn't work !It gives me this error :
" E2285 Could not find a match for 'TButton::TButton()' "
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spiriad is offline Offline
16 posts
since Sep 2006
Dec 26th, 2008
0

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

Header required for 'TButton' is missing i think.
Featured Poster
Reputation Points: 431
Solved Threads: 116
Practically a Master Poster
Agni is offline Offline
654 posts
since Dec 2007
Dec 26th, 2008
0

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

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.
Reputation Points: 193
Solved Threads: 32
Posting Whiz in Training
grumpier is offline Offline
206 posts
since Aug 2008
Dec 26th, 2008
0

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

c++ Syntax (Toggle Plain Text)
  1. TestBtn = new TButton(this);
  2. TestBtn->Parent = this;
  3. TestBtn->Caption = "Check";
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 26th, 2008
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spiriad is offline Offline
16 posts
since Sep 2006
Dec 26th, 2008
0

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

c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Dec 26th, 2008
0

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

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) ?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
spiriad is offline Offline
16 posts
since Sep 2006
Dec 26th, 2008
0

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

try yhis...
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: VC++ 8.0 Express & Unicode
Next Thread in C++ Forum Timeline: Pointers and winforms?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC