Removing Controls from asp.net page at runtime - c#

I have an asp.net dashboard site that allows a user to load HTML templates from a dropdownlist. There are multiple types of DevExpress components on the page, including the ASPxDockPanel. If a user changes templates I get an error that the dockpanel already exists, I would like to include a recursive function like the one below that checks to see if any ASPxDockPanels are present on the page, and if they are present remove them. This works for only the first dock panel then bombs out. I think this is because an enumerable set of controls cannot be modified while looping through it. How can I loop though the controls and remove the dock panels at runtime?
protected void LoadTableTemplate(string selectedTemplate, int currentMode)
{
FindAllDockPanels(this);
}
public void FindAllDockPanels(Control ctrl)
{
if (ctrl != null)
{
foreach (Control control in ctrl.Controls)
{
if (control is ASPxDockPanel)
{
ctrl.Controls.Remove(control);
control.Dispose();
}
FindAllDockPanels(control);
}
}
}

Use a temporary collection, like so:
public void FindAllDockPanels(Control ctrl) {
if (ctrl != null) {
List<Control> remove = new List<Control>();
foreach (Control control in ctrl.Controls) {
if (control is ASPxDockPanel) {
remove.Add( control );
}
}
foreach(Control control in remove) {
control.Controls.Remove( control );
control.Dispose(); // do you really need to dispose of them?
}
FindAllDockPanels(control);
}
}
If you find yourself doing this often, it might be worth moving these "DelayedDelete" actions to an extension method, like so:
public static void DelayedRemove<T>(this IEnumerable<T item> collection, T itemToRemove) {
// add it to a private static dictionary bound to the `collection` instance.
}
public static void DelayedRemoveFinish(this IEnumerable<T item> collection) {
// empty the private static dictionary in here
}
then you'd use it like so:
public void FindAllDockPanels(Control ctrl) {
if (ctrl != null) {
foreach (Control control in ctrl.Controls) {
if (control is ASPxDockPanel) control.Controls.DelayedRemove( control );
}
control.Controls.DelayedRemoveFinish();
FindAllDockPanels(control);
}
}
Much cleaner, no? :)

Related

Loop through all textbox and dropdown controls on a webform and disable them

Having some trouble with this one. I want to loop through every textbox and dropdown control on a webform and disable them under cerrtain conditions. I'm finding a lot of sample code for winforms, but apparently in a webform you can't use Control.Enabled because it doesn't exist. I've got this, which (again) doesn't work because I'm using a webform:
private void DisableControls(Control con)
{
foreach (Control c in con.Controls)
{
DisableControls(c);
}
con.Enabled = false;
}
private void EnableControls(Control con)
{
if (con != null)
{
con.Enabled = true;
EnableControls(con.Parent);
}
}
and I would call them in my Page_Load event like so:
protected void Page_Load(object sender, EventArgs e)
{
// If certain conditions exist, then...
DisableControls(this);
EnableControls(Button1);
}
You could implement a walker:
public void ApplyToAllControls(Control node, Action<Control> lambda)
{
lambda(node);
foreach(Control c in node.Controls)
{
ApplyToAllControls(c, lambda);
}
}
And implement two lambdas like this:
private void ControlDisabler(Control c)
{
if(c != Button1)
{
c.Enabled = false;
}
}
private void ControlEnabler(Control c)
{
if(c != Button1)
{
c.Enabled = true;
}
}
You then invoke them this way:
ApplyToAllControls(root, ControlDisabler);
ApplyToAllControls(root, ControlEnabler);
There are surely some rough edges you can smooth out but you should already get the basic idea.
With currying you can even parameterize the lambdas but that's a bit far to go.
Why you are using recursion ? I don't understand that part.
You can make your method generic
private void DisableControls<T>(IEnumerable<T> controls) where T: Control
{
foreach(var control in controls)
{
control.Enabled = false;
}
}
Then call it:
DisableControls(this.Controls.OfType<TextBox>());
DisableControls(this.Controls.OfType<ComboBox>());
private void DisableControls()
{
var Controls = this.Controls.OfType<TextBox>();
foreach (Control c in Controls)
{
((TextBox)c).Enabled = false;
}
}
private void EnableControls()
{
var Controls = this.Controls.OfType<TextBox>();
foreach (Control c in Controls)
{
((TextBox)c).Enabled = true;
}
}
fastest solution you can use is change the
private void DisableControls(Control con)
to:
private void DisableControls(WebControl con)
that should work,
of course do the same in the loop....
and now the explanations, the class Control doesn't have a definition for Enabled it is defined in an upper level base class ( WebControl ), so this is why you need to use it to gain access to enabled .
EDIT:
just checked the code and it fails on the literal control that is new line ( it inherits directly from control )
so the only other thing you need to add is something like this:
foreach (WebControl con in form1.Controls.OfType<WebControl>())
{
con.Enabled = false;
}

How to determine if Control has Text property

When I iterate over a bunch of different controls on a Form, instead of trying to access the Text property:
String text = String.Empty;
foreach(Control control in this.Controls)
{
try
{
text = control.Text;
}
catch(Exception exception)
{
// This control probably doesn't have the Text property.
Debug.WriteLine(exception.Message);
}
}
Is there a way to just determine whether or not a given control has a Text property? Something like this:
String text = String.Empty;
foreach(Control control in this.Controls)
{
if(control has Text property)
{
text = control.Text;
}
}
I absolutely despise the Try/Catch blocks (unless there is no better alternative, of-course).
All Control objects have a Text property, so there is no point in using reflection to determine that. It will always return true.
Your problem actually is that some controls throw an exception from their Text property because they don't support it.
If you also want to be able to use custom controls that you don't know in advance, you should stick to your current solution and catch the exceptions. However, you should catch the specific exception thrown, for example NotSupportedException.
If you only ever encounter controls that you know in advance, you can select the controls that you know have a working Text property. For example:
public static bool HasWorkingTextProperty(Control control)
{
return control is Label
|| control is TextBox
|| control is ComboBox;
}
var controlsWithText = from c in this.Controls
where HasWorkingTextProperty(c)
select c;
foreach(var control in controlsWithText)
{
string text = control.Text;
// Do something with it.
}
And if you implement your own custom controls that may or may not have a Text property, then you can derive them from a base class that indicates this:
public abstract class CustomControlBase : Control
{
public virtual bool HasText
{
get { return false; }
}
}
public class MyCustomControl : CustomControlBase
{
public override bool HasText
{
get { return true; }
}
public override string Text
{
get { /* Do something. */ }
set { /* Do something. */ }
}
}
public static bool HasWorkingTextProperty(Control control)
{
return (control is CustomControlBase && ((CustomControlBase)control).HasText)
|| control is Label
|| control is TextBox
|| control is ComboBox;
}
Your question is How to determine if Control has Text property, so here is how you can do it using Reflection:
control.GetType().GetProperties().Any(x => x.Name == "Text");
Edit: If you take a look at the Control class, you will see it has a Text property.
Now, if some custom control that overrides the Control class throws an exception when accessing to the Text property, it is violating the Liskov substitution principle. In that case, I suggest you identifying those controls, although what you're doing seems to be fine.
Check this out:
private void Form1_Load(object sender, EventArgs e)
{
foreach (Control ctrl in this.Controls)
{
PropertyInfo[] properties = ctrl.GetType().GetProperties();
foreach(PropertyInfo pi in properties)
if (pi.Name == "Text")
{
//has text
}
}
}

C# Lost Focus without using control event

There is a way to get who lost his focus in a c# form without using the LostFocus event each component?
[edit]
I need for a On Screen Keyboard.
I need to store last focussed control to fire keypress, but i need to do it to all in the window.
Also the main project is wpf, than i have some component nested as itemsTemplate and so on...
I finally used this:
foreach (Control uie in FindInLogicalTreeDown(this, typeof(TextBox))) AssignEvents(uie);
private static IEnumerable<DependencyObject> FindInLogicalTreeDown(DependencyObject obj, Type type)
{
if (obj != null)
{
if (obj.GetType() == type) { yield return obj; }
foreach (object child in LogicalTreeHelper.GetChildren(obj))
if (typeof(DependencyObject).IsAssignableFrom(child.GetType()))
foreach (var nobj in FindInLogicalTreeDown((DependencyObject)child, type)) yield return nobj;
}
yield break;
}
void AssignEvents(Control element)
{
element.GotMouseCapture += new MouseEventHandler(Component_GotFocus);
}
public Control LastFocus { get; set; }
public void Component_GotFocus(object sender, RoutedEventArgs e)
{
LastFocus = (Control)sender;
if (LastFocus.GetType() == typeof(TextBox)) { KeyboardVisible = true; }
}
i don't think there is any way until unless you subscribe events and keep track which lost focus event has fired last

Make all Controls on a Form read-only at once

Does anyone have a piece of code to make all Controls (or even all TextBoxes) in a Form which is read-only at once without having to set every Control to read-only individually?
Write an extension method which gathers controls and child controls of specified type:
public static IEnumerable<T> GetChildControls<T>(this Control control) where T : Control
{
var children = control.Controls.OfType<T>();
return children.SelectMany(c => GetChildControls<T>(c)).Concat(children);
}
Gather TextBoxes on the form (use TextBoxBase to affect RichTextBox, etc - #Timwi's solution):
IEnumerable<TextBoxBase> textBoxes = this.GetChildControls<TextBoxBase>();
Iterate thru collection and set read-only:
private void AreTextBoxesReadOnly(IEnumerable<TextBoxBase> textBoxes, bool value)
{
foreach (TextBoxBase tb in textBoxes) tb.ReadOnly = value;
}
If want - use caching - #igor's solution
In Form:
if (_cached == null)
{
_cached = new List<TextBox>();
foreach(var control in Controls)
{
TextBox textEdit = control as TextBox;
if (textEdit != null)
{
textEdit.ReadOnly = false;
_cached.Add(textEdit);
}
}
}
else
{
foreach(var control in _cached)
{
control .ReadOnly = false;
}
}
Add recursion also (Controls can be placed into other controls (panels)).
You should be able to write yourself a utility function to do this. You can iterate over the form’s controls and then each control’s child controls recursively. For example:
public static void SetEnableOnAllControls(Control parentControl, bool enable)
{
parentControl.Enabled = enable;
foreach (Control control in parentControl.Controls)
SetEnableOnAllControls(control, enable);
}
[...]
// inside your form:
SetEnableOnAllControls(this, false);
This doesn’t take care of ToolStrips, which aren’t controls. You could write a separate, similar method for those.
Notice that the above disables the form itself too. If you don’t want that, try this:
public static void SetEnableOnAllChildControls(Control parentControl, bool enable)
{
foreach (Control control in parentControl.Controls)
{
control.Enabled = enable;
SetEnableOnAllChildControls(control, enable);
}
}
If you really meant the ReadOnly property, which is only relevant for TextBoxes, try this:
public static void SetReadOnlyOnAllControls(Control parentControl, bool readOnly)
{
if (parentControl is TextBoxBase)
((TextBoxBase) parentControl).ReadOnly = readOnly;
foreach (Control control in parentControl.Controls)
SetReadOnlyOnAllControls(control, readOnly);
}
I would use reflection to check to see if the generic Control object has an Enabled property.
private static void DisableControl(Control control)
{
PropertyInfo enProp = control.GetType().GetProperty("Enabled");
if (enProp != null)
{
enProp.SetValue(control, false, null);
}
foreach (Control ctrl in control.Controls)
{
DisableControl(ctrl);
}
}
this.Enabled = false;
Depends on what you doing really, you might want to consider putting the control within a panel and disabling that.
I haven't tested this, but it should work:
foreach (var textBox in this.Controls.OfType<TextBox>())
textBox.ReadOnly = true;
Edit: This is not such a good solution it seems: see Timwi's comment.
I just developed a recursive solution that handles any kind of Web Control, using a simple static method and ASP.NET polymorphysm.
/// <summary>
/// Handle "Enabled" property of a set of Controls (and all of the included child controls through recursivity)
/// By default it disable all, making all read-only, but it can also be uset to re-enable everything, using the "enable" parameter
/// </summary>
/// <param name="controls">Set of controls to be handled. It could be the entire Page.Controls set or all of the childs of a given control (property "Controls" of any Control-derived class)</param>
/// <param name="enable">Desired value of the property "enable" of each control. </param>
public static void DisableControls(ControlCollection controls, bool enable = false)
{
foreach (Control control in controls)
{
var wCtrl = control as WebControl;
if (wCtrl != null)
{
wCtrl.Enabled = enable;
}
if (control.Controls.Count > 0)
DisableControls(control.Controls, enable);
}
}
/// <summary>
/// Enable a set of Controls (and all of the included child controls through recursivity).
/// Friendly name for DisableControls(controls, true), that achieve the same result.
/// </summary>
/// <param name="Controls">Set of controls to be handled. It could be the entire Page.Controls set or all of the childs of a given control (property "Controls" of any Control-derived class)</param>
public static void EnableControls(ControlCollection controls)
{
DisableControls(controls, true);
}
This is tested and looks fairly fast (less than a millisecond in a web form with 25+ controls to be disabled).
If you prefer an extension method, i think it should be enough to change the solution as follows:
public static void DisableControls(this Control control, bool enable = false)
{
foreach (Control ctrl in control.Controls)
{
var wCtrl = ctrl as WebControl;
if (wCtrl != null)
{
wCtrl.Enabled = enable;
}
if (ctrl.Controls.Count > 0)
ctrl.DisableControls(enable);
}
}
public static void EnableControls(this Control control)
{
control.DisableControls(true);
}

Get available controls from a Form

How do I get available controls from a Windows Forms form using C#?
Or, ProfK's solution in enumerable syntax:
public static IEnumerable<Control> GetControls(Control form) {
foreach (Control childControl in form.Controls) { // Recurse child controls.
foreach (Control grandChild in GetControls(childControl)) {
yield return grandChild;
}
yield return childControl;
}
}
Try this method in your form. It will recursively get all controls on your form, and their children:
public static List<Control> GetControls(Control form)
{
var controlList = new List<Control>();
foreach (Control childControl in form.Controls)
{
// Recurse child controls.
controlList.AddRange(GetControls(childControl));
controlList.Add(childControl);
}
return controlList;
}
Then call it with a:
List<Control> availControls = GetControls(this);
I think you mean all controls on the form.
So simply you can use Controls property inside your form object.
foreach(Control c in this.Controls)
{
//TODO:
}

Categories