dynamic control creation

E

e-mid

i have a user control (say myUserControl) that inherits panel, i create
some textboxes on myUserControl dynamically, then i create some
myUserControls on the form dynamically with a loop . of course i change
name,location etc.. meanwhile.

i have some problems here then.

eg: how could i reach each myUserControl and textBoxes on this
myUserControl.
it could be betterif i reach them directly, i know renaming a control on
the runtime is useless. what should i do?


another issue is, it is not known how many myUserControls on thye from or
textboxes on a myUserControl will be created. i need autoscroll
functionality. on the panel it works, but on the form it seems, autoscroll
does not work.

thnkz
 
S

Shakir Hussain

e-mid.

There are different ways to do that, i have one recomendation.

1. To reach individual controls, access through properties.

example -

protected TextBox mMyTextBox;

public TexBox MyTextBox
{
get {return mMyTextBox;}
}

2. Autoscroll will work even in design mode of form. I am not sure why
its not working for ur form

Shak.
 
E

e-mid

Shakir Hussain said:
e-mid.

There are different ways to do that, i have one recomendation.

1. To reach individual controls, access through properties.

example -

protected TextBox mMyTextBox;

public TexBox MyTextBox
{
get {return mMyTextBox;}
i dont think, this way works. controls created dynamically.
another question arised here , is this controls are private by default?
2. Autoscroll will work even in design mode of form. I am not sure why
its not working for ur form

it scrolls horizontally, but vertical autoscroll does not work..

i did not understand why?
 
H

Herfried K. Wagner [MVP]

* "e-mid said:
i have a user control (say myUserControl) that inherits panel, i create
some textboxes on myUserControl dynamically, then i create some
myUserControls on the form dynamically with a loop . of course i change
name,location etc.. meanwhile.

i have some problems here then.

eg: how could i reach each myUserControl and textBoxes on this
myUserControl.
it could be betterif i reach them directly, i know renaming a control on
the runtime is useless. what should i do?

\\\
DirectCast(Me.UserControl1.Controls(12), TextBox).Select(2, 10)
///
another issue is, it is not known how many myUserControls on thye from or
textboxes on a myUserControl will be created. i need autoscroll
functionality. on the panel it works, but on the form it seems, autoscroll
does not work.

Mhm... It should work...
 
E

e-mid

Herfried, can you explain about this?

DirectCast(Me.UserControl1.Controls(12), TextBox).Select(2, 10)

and i hear about directCast first time, can you point some sources about it?
 
S

Shakir Hussain

emid

1. Vertical scrolling will happen only if your controls overflows
vertically. To test this, place controls which overflows horizontally and
vertically. Form will bring both scroll bars.

2. If the controls are created dynamically, to reach individual control, you
have to access Control Collection of your usercontrol

Shak.
 
H

Herfried K. Wagner [MVP]

* "e-mid said:
Herfried, can you explain about this?

DirectCast(Me.UserControl1.Controls(12), TextBox).Select(2, 10)

and i hear about directCast first time, can you point some sources about it?

The 'Controls' collection contains objects of type 'Control'. 'Control'
is the generic type for controls shown on Windows Forms. In order to
access members of an object that is an instance of a class that inherits
from 'Control', you need to cast the object to this class.
 
E

e-mid

Shakir Hussain said:
emid

1. Vertical scrolling will happen only if your controls overflows
vertically. To test this, place controls which overflows horizontally and
vertically. Form will bring both scroll bars.

it overflows vertically too , but vertical scrollbar do not appear
2. If the controls are created dynamically, to reach individual control, you
have to access Control Collection of your usercontrol

Shak.

yes that works for myUserControls on the form, but when i want to get
textBoxes on a myUserControl, i get a error : "getEnumator() does not bla
bla....." . i cant traverse through the controls on myUserControl..

another question:
is it possible to reach with the name of the control?
 
E

e-mid

i was using csharp , therefore i did not t know what directcast is, but i
learned know
The 'Controls' collection contains objects of type 'Control'. 'Control'
is the generic type for controls shown on Windows Forms. In order to
access members of an object that is an instance of a class that inherits
from 'Control', you need to cast the object to this class.

ok i got this. but i cant traverse through controls on my userControl . i
got error about getEnumarator()..

and i ask again:

is it possible to reach a control, if i know only its name?
 
S

Shakir Hussain

No. Its not possible to reach control by name. You can write a service
function which will return a control, if you pass a name. In the control
collection, you can check whether a particular control exists calling
"Contains" method. For that you need a control to find out.

Can you paste the complete error messages? I may get a clue whats
happening..

Shak.
 
H

Herfried K. Wagner [MVP]

* "e-mid said:
is it possible to reach a control, if i know only its name?

Yes, if you provide the mechanism to do that. For example, you can use
an instance of 'Hashtable' to store the (name, control) pairs and easily
look up a control by its name.
 
E

e-mid

thnkz, i will try it..

Herfried K. Wagner said:
Yes, if you provide the mechanism to do that. For example, you can use
an instance of 'Hashtable' to store the (name, control) pairs and easily
look up a control by its name.
 
E

e-mid

Here is the error message:

"foreach statement cannot operate on variables of type 'Form1.myUserControl'
because 'Form1.myUserControl' does not contain a definition for
'GetEnumerator', or it is inaccessible"

thnkz
 
S

Shakir Hussain

emid,

Try this

foreach(Control pControl in Form1.myUserControl.Controls)
{
//process the control.
}

this wont give compilation error.

Shak.
 
E

e-mid

thknz ...
Shak.

Shakir Hussain said:
emid,

Try this

foreach(Control pControl in Form1.myUserControl.Controls)
{
//process the control.
}

this wont give compilation error.

Shak.
 

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