toyboy61
asked on
TextBox inheritance
I have defined a new class called "XtraTextBox" which inherits all properties from the class TextBox (built-in class), and have added some extra variables.
Later on I create an instance of this new class and populates it with property-values from an existing TextBox called "Hele". This new instance is called "UtipsHel", and I want to copy property values from the existing TextBox called "Hele" to this new instance of XtraTextBox.
"UtipsHel" should after this be a copy of "Hele", and in the end I make "Hele" invisible and "UtipsHel" visible. My goal is the replace "Hele" with "UtipsHel" on the form (Form1). But "UtipsHel" never shows up.
What have I done wrong here ?
(Visual Studio 2003 .NET).
Later on I create an instance of this new class and populates it with property-values from an existing TextBox called "Hele". This new instance is called "UtipsHel", and I want to copy property values from the existing TextBox called "Hele" to this new instance of XtraTextBox.
"UtipsHel" should after this be a copy of "Hele", and in the end I make "Hele" invisible and "UtipsHel" visible. My goal is the replace "Hele" with "UtipsHel" on the form (Form1). But "UtipsHel" never shows up.
What have I done wrong here ?
(Visual Studio 2003 .NET).
XtraTextBox* UtipsHel = new XtraTextBox();
UtipsHel->Location = Hele->Location;
UtipsHel->BackColor = Color::FromArgb(255,255,0,0); // pure red
UtipsHel->ForeColor = Color::FromArgb(255,0,0,255); // pure blue
UtipsHel->Enabled = Hele->Enabled;
UtipsHel->Font = Hele->Font;
UtipsHel->Size = Hele->Size;
UtipsHel->Text = Hele->Text;
UtipsHel->Text = S"RUBBISH";
Hele->Visible=false;
UtipsHel->Visible=true;
__gc class XtraTextBox: public TextBox
{/*...*/
int HeleMat;
int HalveMat;
int Sikre;
int HeleUt;
int HalveUt;
};
ASKER
Zoppo: Thanx, but that didn't help. Maybe I have to tie the XtraTextBox-button to Form1 ?
I think you are missing something like
this->Controls->Add(this-> UtipsHel);
That will inform the form that the new textbox belongs to it.
this->Controls->Add(this->
That will inform the form that the new textbox belongs to it.
In fact you would be better to use something like this:
this->Hele->Parent->Contro ls->Add(th is->UtipsH el);
The reason being is the textbox (I expect) should belong to the same parent control which could be a panel on a form, not the form itself for example.
this->Hele->Parent->Contro
The reason being is the textbox (I expect) should belong to the same parent control which could be a panel on a form, not the form itself for example.
ASKER
AndyAinscow:
Alternative 1: this->Controls->Add(this-> UtipsHel);
// Error message: error C2039: 'UtipsHel' : is not a member of 'SystemTipp201101::Form1'
Alternative 2: this->Hele->Parent->Contro ls->Add(th is->UtipsH el);
// Error message: error C2039: 'UtipsHel' : is not a member of 'SystemTipp201101::Form1'
<code snippet>:
27: public __gc class Form1 : public System::Windows::Forms::Fo rm
28: {
29:
30: //
31: //
32: //
33:
34: __gc class XtraTextBox: public TextBox
35: {/*...*/
36: int HeleMat;
37: int HalveMat;
38: int Sikre;
39: int HeleUt;
40: int HalveUt;
41: };
<end of code snippet>
Error message: Form1.h(27) : see declaration of 'SystemTipp201101::Form1'
What do I do now?
Alternative 1: this->Controls->Add(this->
// Error message: error C2039: 'UtipsHel' : is not a member of 'SystemTipp201101::Form1'
Alternative 2: this->Hele->Parent->Contro
// Error message: error C2039: 'UtipsHel' : is not a member of 'SystemTipp201101::Form1'
<code snippet>:
27: public __gc class Form1 : public System::Windows::Forms::Fo
28: {
29:
30: //
31: //
32: //
33:
34: __gc class XtraTextBox: public TextBox
35: {/*...*/
36: int HeleMat;
37: int HalveMat;
38: int Sikre;
39: int HeleUt;
40: int HalveUt;
41: };
<end of code snippet>
Error message: Form1.h(27) : see declaration of 'SystemTipp201101::Form1'
What do I do now?
Does this line of code not appear at form level ?
XtraTextBox* UtipsHel = new XtraTextBox();
XtraTextBox* UtipsHel = new XtraTextBox();
Try this.
Create a new project (windows forms) and add a panel and a textbox to the panel.
Now modify the .h file so this is in place
Run the app - no second text box, now uncommment the line
Create a new project (windows forms) and add a panel and a textbox to the panel.
Now modify the .h file so this is in place
private: System::Windows::Forms::TextBox^ textBox2;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
this->textBox2 = (gcnew System::Windows::Forms::TextBox());
this->textBox2->Location = System::Drawing::Point(67, 94);
this->textBox2->Name = L"textBox2";
this->textBox2->Size = System::Drawing::Size(100, 20);
this->textBox2->TabIndex = 1;
//this->textBox1->Parent->Controls->Add(this->textBox2);
}
instead ofpublic:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
Run the app - no second text box, now uncommment the line
this->textBox1->Parent->Controls->Add(this->textBox2);
, run the code and a second text box appearsASKER
AndyAinscow: Apart from the fact that Visual Studio .NET 2003 uses "*" instead of "^" and "new" instead of "gcnew" your solution worked as it should.
But TextBox is still a build-in class, and I want to enhanced the class with my own variables (methods/properties). And that problem is still not solved. Does that mean I have to put them on a panel ?
But TextBox is still a build-in class, and I want to enhanced the class with my own variables (methods/properties). And that problem is still not solved. Does that mean I have to put them on a panel ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not experienced doing such things in .NET - but I guess the problem is that the 'UtipsHel's control isn't created as control. I think you have to call 'CreateControl' against it, but I'm not really sure where this needs to be called - I would try it directly after the instantiation, i.e.:
Open in new window
Hope that helps,ZOPPO