Windows Develop Bookmark and Share   
 index > Windows Forms Designer > User Control / Inherited Control Question
 

User Control / Inherited Control Question

Hey,

I am pretty new to user controls / inherited controls and I am trying to find out what the best way to proceed is.

Without getting into why, what I am trying to do is create a control that is essentially a textbox (has all the properties and events of a textbox), but also has a label control that overlays on it. I don't care about any of the label's events, but I do want to be able to access its properties. I would be happy to access those properties through something like 'FunkyTextBox.Label.Caption', etc.

This seems easy enough to do with a user control, but I don't get to keep all of the textbox events without explicitly declaring them all, and I would also have to access the textbox's properties through something like FunkyTextBox.TextBox.Text. So I feel likeI should be to make an inherited control, but if I do, I don't get that same user control 'designer' to place the label where I want relative to the textbox, etc.

Any thoughts on how I should proceed?

Thanks,

Pete

Pete from Cincinnati  Friday, January 04, 2008 10:04 PM

I guess I was looking for something more like this. So you get a textbox with a label over it.

Code Block

class MyTextBox: TextBox

{

Label myLabel;

public MyTextBox():base()

{

myLabel = new Label();

myLabel.Caption = "some text";

myLabel.Location = ...

...

this.Controls.Add(myLabel);

}

}

Pete from Cincinnati  Monday, January 07, 2008 5:37 PM

Does change the modifiers of the Label and TextBox to public in the UserControl work for you? i.e. something like this

public myControl:UserControl
{
.....other necessary code....

public TextBox TextBox
{
get{return this.textBox1;}
}

public LabelLabel
{
get{return this.label1;}
}

}

Zhi-Xin Ye  Monday, January 07, 2008 11:55 AM

Hey Zhi-Xin,

Thanks for the reply.

That does let me access the properties of the label and the textbox in the designer, if I go the user control route,but the part of that that it doesn't seem to address is being able to access the events of the textbox and the label in the designer. I really don't care about the events of the label, but I do want the events of the textbox and I would like to get them without explicitly declaring them all. That part of it had me thinking that maybe this has to be some sort of inhereted control rather than a user control.

I don't know.

Pete

Pete from Cincinnati  Monday, January 07, 2008 1:41 PM

I guess I was looking for something more like this. So you get a textbox with a label over it.

Code Block

class MyTextBox: TextBox

{

Label myLabel;

public MyTextBox():base()

{

myLabel = new Label();

myLabel.Caption = "some text";

myLabel.Location = ...

...

this.Controls.Add(myLabel);

}

}

Pete from Cincinnati  Monday, January 07, 2008 5:37 PM

You can use google to search for other answers

Custom Search

More Threads

• How to code situations where there is a mustinherit class in the middle
• Checkbox in a Combobox
• Help with Making ListView Columns Sortable in VC++ 2005
• MDI PARENT AND CHILD FILL PARENT AREA
• Help me designer has Bugs bugs bugs
• Making a dataGridView with more "columns"
• Implementing RadioButton Clone
• Move scrollbar at top position
• How to make a Picturebox accept focus
• How to make a control dockable...?