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

Visual C++.NET

Avatar of undefined
Last Comment
AndyAinscow
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 ?

Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

I think you are missing something like
                  this->Controls->Add(this->UtipsHel);
That will inform the form that the new textbox belongs to it.
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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.
Avatar of toyboy61
toyboy61

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->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?
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Does this line of code not appear at form level ?

XtraTextBox* UtipsHel = new XtraTextBox();
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

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

Avatar of toyboy61
toyboy61

ASKER

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

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Visual C++.NET
Visual C++.NET

Microsoft Visual C++ (often abbreviated as MSVC or VC++) is an integrated development environment (IDE) product from Microsoft for the C, C++, and C++/CLI programming languages. It features tools for developing and debugging C++ code, especially code written for the Microsoft Windows API, the DirectX API, and the Microsoft .NET Framework. Many applications require redistributable Visual C++ packages to function correctly and are often installed independently of applications, allowing multiple applications to make use of the package while only having to install it once

7K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo