controls
-
hello, i need a help, can we replace the controls dynamically(example: replace radio button by textbox when some event has occurred), if yes then please help me in this thank you :)
theanil
As you haven't specified what technology you are using (e.g. WinForms), I'm going to have to be fairly general here. Yes you can. You would need to remove the controls you don't want from the parents control collection (you have to iterate over this using a for loop as you are removing items which you can't do inside a foreach if it directly affects the item being iterated). NOTE: Don't forget to dereference the eventhandlers. Once you have removed them, add in your new controls taking care to set any relevant properties such as location, etc.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
As you haven't specified what technology you are using (e.g. WinForms), I'm going to have to be fairly general here. Yes you can. You would need to remove the controls you don't want from the parents control collection (you have to iterate over this using a for loop as you are removing items which you can't do inside a foreach if it directly affects the item being iterated). NOTE: Don't forget to dereference the eventhandlers. Once you have removed them, add in your new controls taking care to set any relevant properties such as location, etc.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
thank you, but i tried that way, i need to toggle between the controls and as the answer you said, deleting the controls every time and adding new one is time consuming, is there any alternate way to do this.
theanil
Rather than delete them, change their Visibility. The downside is that you will consume more memory leaving that much open.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
Rather than delete them, change their Visibility. The downside is that you will consume more memory leaving that much open.
Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility
-
hello, i need a help, can we replace the controls dynamically(example: replace radio button by textbox when some event has occurred), if yes then please help me in this thank you :)
theanil
Well, yes you can, but why would you ? Particurlarly in this case: it's hard for me to imagine a scenario in which a RadioButton (usually used in groups where the function desired is to select one option from many) replaces a TextBox, or, the reverse. The second thought ... which I see Pete O'Hanlon has already explained, is to implement both controls, and "swap" them by changing their visibility. In the case where both "alternate" controls expose the same Events (with the exact same type of EventArgs parameter), you could have, for example, a RadioButton and a TextBox use the same EventHandler for Click, although I think that, too is "dubious" practice: better, imho, they each have their own Click handler, and then call a common method if there's any code that both Click events need to use. So, I suggest you re-consider your design here.
"Science is facts; just as houses are made of stones: so, is science made of facts. But, a pile of stones is not a house, and a collection of facts is not, necessarily, science." Henri Poincare