-1

hi all here i try to make method which can loop on the form and convert any text boxenter image description here from Read only=true to be Read only = false but its not working

public static void unread only(Form frm)
    {
        foreach (Control item in frm.Controls)
        {
            if (item is TextBox)
            {
               // item.ReadOnly = false;
            }
        }            

  }
Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
  • 3
    _"its not working"_ -- that is not even close to being a specific enough problem statement. Please fix your question so that it's more clear. Provide a good [mcve] that reliably reproduces whatever problem you're having, and describe that problem _precisely_ and in detail. Explain what _specifically_ you are having trouble figuring out. – Peter Duniho Aug 20 '17 at 00:58
  • Try this: `(item as TextBox).ReadOnly = false;` – John Alexiou Aug 20 '17 at 04:31

3 Answers3

1

In your code, the compiler thinks that the object you have found is a Control, and does not know what type of control it is. You need to tell it what sort of control it is, and you can do this by casting it to a textbox:

((TextBox)item).ReadOnly = false;

However, there are better ways of doing this. Your code will only look at Top-level controls, and if you have container controls on your form, it will not recursively search those to find other textboxes. A recursive method to do this is as follows:

public static IEnumerable<T> GetControlsOfType<T>(Control root)
where T : Control
{
var t = root as T;
if (t != null)
    yield return t;

var container = root as ContainerControl;
if (container != null)
    foreach (Control c in container.Controls)
        foreach (var i in GetControlsOfType<T>(c))
            yield return i;
}

This is some code I got from here. It allows you to do something like this:

foreach (var textBox in GetControlsOfType<TextBox>(theForm)) 
{
    textBox.ReadOnly = false;
}
ainwood
  • 992
  • 7
  • 17
0

You need to be recursive. A Form Control can contain other Controls.

Richard Schneider
  • 34,944
  • 9
  • 57
  • 73
0

Primarily your problem is that you are not casting the control to TextBox so that you have access to the property you want to set.

foreach (Control item in frm.Controls) {
    if (item is TextBox) {
        var textBox = item as TextBox;
        textBox.ReadOnly = false;
    }
}

That said, controls can contain child control, so you would need to crawl the entire form looking for embedded text boxes.

This will check the form and its controls for text boxes and set them to read only

public static void SetReadOnly(Control frm) {
    var controls = new Queue<Control>();
    controls.Enqueue(frm);
    while (controls.Count > 0) {
        var control = controls.Dequeue();
        if (control is TextBox) {
            var txtBox = control as TextBox;
            txtBox.ReadOnly = true;
        }
        if (control.HasChildren) {
            foreach (var child in control.Controls.OfType<Control>()) {
                controls.Enqueue(child);
            }
        }
    }
}

The code is pretty self explanatory but you should walk through the flow to understand what it is doing.

Nkosi
  • 235,767
  • 35
  • 427
  • 472