Link to home
Start Free TrialLog in
Avatar of toyboy61
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).








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;

Open in new window

__gc class XtraTextBox: public TextBox 
{/*...*/ 
 int HeleMat;
 int HalveMat;
 int Sikre;
 int HeleUt;
 int HalveUt;
};

Open in new window

Avatar of Zoppo
Zoppo
Flag of Germany image

Hi toyboy61,

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.:
XtraTextBox* UtipsHel = new XtraTextBox();
UtipsHel->CreateControl();

Open in new window

Hope that helps,

ZOPPO
Avatar of toyboy61
toyboy61

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.
In fact you would be better to use something like this:
                  this->Hele->Parent->Controls->Add(this->UtipsHel);
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.
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->Controls->Add(this->UtipsHel);
// Error message: error C2039: 'UtipsHel' : is not a member of 'SystemTipp201101::Form1'

<code snippet>:
27:      public __gc class Form1 : public System::Windows::Forms::Form
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();
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

	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);
		}

Open in new window

instead of
public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

Open in new window


Run the app - no second text box, now uncommment the line
this->textBox1->Parent->Controls->Add(this->textBox2);

Open in new window

, run the code and a second text box appears

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 ?


ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial