Using a component to add a control to a form

G

Guest

Hi!

I would like to use a component to add a control to a form. This will be
used to make a component that can enable/disable visual features on a form
without making every feature a toolbox item.

I can add and remove the control to the forms component container by using
the following code:

private bool m_bDisplayTextBox = false;
private TextBox tbTest = new TextBox();

[DefaultValue(false)]
public bool DisplayTextBox
{
get { return(m_bDisplayTextBox); }
set
{
if(value == true) this.Container.Add(tbTest);
else this.Container.Remove(tbTest);

m_bDisplayTextBox = value;
}
}


This will make the TextBox visible in the dropdown list in the properties
browser, but it will not be visible on the form itself. I have pined this
down to the fact that there is no line containing
"this.Controls.Add(this.textBox1);" added to "Windows Form Designer generated
code" in the Form class.

How can I also get the "this.Controls.Add(this.textBox1);" added to the Form
class "Windows Form Designer generated code"?


BR
Christian Wikander
 
J

Joey Calisay

you should use a designer for your form, and there use the createcomponent
method
 
G

Guest

Thanks for the quick response.

I don't quite understand. Maybe it's not possible, but I would like to add
the component to the form, and be setting some attributes of the component I
want to create one or more controls on the form. Can you give me another hint
in the right direction?


Joey Calisay said:
you should use a designer for your form, and there use the createcomponent
method

Christian Wikander said:
Hi!

I would like to use a component to add a control to a form. This will be
used to make a component that can enable/disable visual features on a form
without making every feature a toolbox item.

I can add and remove the control to the forms component container by using
the following code:

private bool m_bDisplayTextBox = false;
private TextBox tbTest = new TextBox();

[DefaultValue(false)]
public bool DisplayTextBox
{
get { return(m_bDisplayTextBox); }
set
{
if(value == true) this.Container.Add(tbTest);
else this.Container.Remove(tbTest);

m_bDisplayTextBox = value;
}
}


This will make the TextBox visible in the dropdown list in the properties
browser, but it will not be visible on the form itself. I have pined this
down to the fact that there is no line containing
"this.Controls.Add(this.textBox1);" added to "Windows Form Designer generated
code" in the Form class.

How can I also get the "this.Controls.Add(this.textBox1);" added to the Form
class "Windows Form Designer generated code"?


BR
Christian Wikander
 
J

Joey Callisay

I hope this link helps:

http://www.divil.co.uk/net/articles/designers/hosting.asp

try to browse other articles from this domain...

Christian Wikander said:
Thanks for the quick response.

I don't quite understand. Maybe it's not possible, but I would like to add
the component to the form, and be setting some attributes of the component I
want to create one or more controls on the form. Can you give me another hint
in the right direction?


Joey Calisay said:
you should use a designer for your form, and there use the createcomponent
method

message news:[email protected]...
Hi!

I would like to use a component to add a control to a form. This will be
used to make a component that can enable/disable visual features on a form
without making every feature a toolbox item.

I can add and remove the control to the forms component container by using
the following code:

private bool m_bDisplayTextBox = false;
private TextBox tbTest = new TextBox();

[DefaultValue(false)]
public bool DisplayTextBox
{
get { return(m_bDisplayTextBox); }
set
{
if(value == true) this.Container.Add(tbTest);
else this.Container.Remove(tbTest);

m_bDisplayTextBox = value;
}
}


This will make the TextBox visible in the dropdown list in the properties
browser, but it will not be visible on the form itself. I have pined this
down to the fact that there is no line containing
"this.Controls.Add(this.textBox1);" added to "Windows Form Designer generated
code" in the Form class.

How can I also get the "this.Controls.Add(this.textBox1);" added to
the
Form
class "Windows Form Designer generated code"?


BR
Christian Wikander
 
G

Guest

This was helpful but stil not. I get the same result if I try to get the
IDesignerHost service and create the control from there with the
CreateControl method. It's added to the list of components in the properties
window, but not to the actual form.

Joey Callisay said:
I hope this link helps:

http://www.divil.co.uk/net/articles/designers/hosting.asp

try to browse other articles from this domain...

Christian Wikander said:
Thanks for the quick response.

I don't quite understand. Maybe it's not possible, but I would like to add
the component to the form, and be setting some attributes of the component I
want to create one or more controls on the form. Can you give me another hint
in the right direction?


Joey Calisay said:
you should use a designer for your form, and there use the createcomponent
method

message Hi!

I would like to use a component to add a control to a form. This will be
used to make a component that can enable/disable visual features on a form
without making every feature a toolbox item.

I can add and remove the control to the forms component container by using
the following code:

private bool m_bDisplayTextBox = false;
private TextBox tbTest = new TextBox();

[DefaultValue(false)]
public bool DisplayTextBox
{
get { return(m_bDisplayTextBox); }
set
{
if(value == true) this.Container.Add(tbTest);
else this.Container.Remove(tbTest);

m_bDisplayTextBox = value;
}
}


This will make the TextBox visible in the dropdown list in the properties
browser, but it will not be visible on the form itself. I have pined this
down to the fact that there is no line containing
"this.Controls.Add(this.textBox1);" added to "Windows Form Designer
generated
code" in the Form class.

How can I also get the "this.Controls.Add(this.textBox1);" added to the
Form
class "Windows Form Designer generated code"?


BR
Christian Wikander
 
J

Joey Callisay

After creating the component with CreateControl, did you add it to the
control collection of the form?

Christian Wikander said:
This was helpful but stil not. I get the same result if I try to get the
IDesignerHost service and create the control from there with the
CreateControl method. It's added to the list of components in the properties
window, but not to the actual form.

Joey Callisay said:
I hope this link helps:

http://www.divil.co.uk/net/articles/designers/hosting.asp

try to browse other articles from this domain...

message news:[email protected]...
Thanks for the quick response.

I don't quite understand. Maybe it's not possible, but I would like to add
the component to the form, and be setting some attributes of the
component
I
want to create one or more controls on the form. Can you give me
another
hint
in the right direction?


:

you should use a designer for your form, and there use the createcomponent
method

message Hi!

I would like to use a component to add a control to a form. This
will
be
used to make a component that can enable/disable visual features
on a
form
without making every feature a toolbox item.

I can add and remove the control to the forms component container
by
using
the following code:

private bool m_bDisplayTextBox = false;
private TextBox tbTest = new TextBox();

[DefaultValue(false)]
public bool DisplayTextBox
{
get { return(m_bDisplayTextBox); }
set
{
if(value == true) this.Container.Add(tbTest);
else this.Container.Remove(tbTest);

m_bDisplayTextBox = value;
}
}


This will make the TextBox visible in the dropdown list in the properties
browser, but it will not be visible on the form itself. I have
pined
this
down to the fact that there is no line containing
"this.Controls.Add(this.textBox1);" added to "Windows Form Designer
generated
code" in the Form class.

How can I also get the "this.Controls.Add(this.textBox1);" added
to
the
Form
class "Windows Form Designer generated code"?


BR
Christian Wikander
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top