0

I need to align the text center for multiple richtextbox.

I found the solution to align the single richtextbox.

EX:

richtextbox1.SelectAll();

richtectbox1.SelectionAlignment = HorizantalAlignment.Center;

I dont want to enter this for every textboxes. How to do this for multiple richtextbox using loop?

Sathish kumar
  • 19
  • 1
  • 5
  • Assuming Text Boxes don't have a single common parent (i.e. are on GroupBoxes, tabs, etc), then you'll need to [recurse](https://stackoverflow.com/a/3426721/314291) from the topmost parent (possibly the form itself). – StuartLC Nov 18 '17 at 08:08

4 Answers4

1

You can look for all the controls who are of type RichTextBox and do whatever you need to do like this:

foreach (var thisControl in this.Controls.OfType<RichTextBox>())
{
    thisControl.SelectAll();
    thisControl.SelectionAlignment = HorizontalAlignment.Center;
}
CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
1

In addition to CodingYoshi's answer, if the Rich Text Boxes don't have a single common parent (i.e. the TextBoxes are dispersed on GroupBoxes, Tabs, etc), then you'll need to recurse from the topmost common parent (possibly the form itself) in order to find the RichTextBoxes, using a technique such as this one here:

    public IEnumerable<Control> GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();

        return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                  .Concat(controls)
                                  .Where(c => c.GetType() == type);
    }

You'll then be able to apply your alignment to all subordinate controls at any level from a given root coontrol (this is the root Form control in this example)

foreach (RichTextBox textBox in GetAll(this, typeof (RichTextBox)))
{
    textBox.SelectAll();
    textBox.SelectionAlignment = HorizontalAlignment.Center;
}
StuartLC
  • 104,537
  • 17
  • 209
  • 285
0

You need to create a list of RichTextBoxes, and then:

foreach(richtextbox in list)
{
 t.SelectAll();
 t.SelectionAlignment = HorizantalAlignment.Center;
}

You can also use [this] (How to get ALL child controls of a Windows Forms form of a specific type (Button/Textbox)?) post, to gather all your richtextboxes:

ronfl
  • 15
  • 5
0

First you need to get all the child controls of the form into a list, and by changing the required property of each item in the list your objective can be met.

You can get all child controls by using a function like this:

    public static IEnumerable<TControl> GetChildControls<TControl>(this Control control) where TControl : Control
    {
        var children = (control.Controls != null) ? control.Controls.OfType<TControl>() : Enumerable.Empty<TControl>();
        return children.SelectMany(c => GetChildControls<TControl>(c)).Concat(children);
    }

You can get all the RichText boxes like this

var richTextBoxes = this.GetChildControls<RichTextBox>();
foreach (RichTextBox rtb in richTextBoxes)
{
   rtb.SelectionAlignment = HorizantalAlignment.Center;
}

Consider this as an idea, copy paste this code may have syntax errors.