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?
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;
}
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;
}
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:
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.
Related
I'm looking for an effective way of clearing textboxes, ideally in a function.
I have tried using:
{
Action<Control.ControlCollection> func = null;
func = (controls) =>
{
foreach (Control control in controls)
if (control is TextBox)
(control as TextBox).Clear();
else
func(control.Controls);
};
func(Controls);
}
The problem with the above solution is that I could not choose which text boxes were to be deleted.
TextBoxName.Text = "";
The above works however the only problem is that it takes up 200 lines
I have 61 boxes, i need a clear all button, that only clears 60 boxes (all except one)
i need clear row buttons, since the boxes are arranged in rows
there are 15 clear row buttons, each with 4 boxes, is there a for loop i can use that will only clear the ones i need (by name if possible)?
You could create a collection of TextBoxes to exempted and filter based on it. For example,
var exceptionList = new[] { textBox1 };
foreach(var textBox in Controls.OfType<TextBox>().Where(x=> !exceptionList.Contains(x)))
{
textBox.Clear();
}
If you want to filter based on the Control Name, you would use
foreach(var textBox in Controls.OfType<TextBox>().Where(x=> !exceptionList.Contains(x.Name)))
{
textBox.Clear();
}
Where exceptionList is collection of names of TextBoxes that needs to be exempted.
i made this code for get all the groupboxes from a winform and then take only the ones with a determinated name.
Control.ControlCollection controles = this.Controls;
GroupBox gBoxAux = new GroupBox();
List<GroupBox> gBoxes = new List<GroupBox>();
foreach (Control c in controles)
{
if (c.GetType() == typeof(GroupBox))
{
gBoxAux = (GroupBox)c;
gBoxes.Add(gBoxAux);
}
}
I don't know if there's a better way to do it instead of iterate over all the controls.
Thank you very much!
You can query that using Linq:
this.Controls.OfType<GroupbBox>().Where(x=> x.Name == "SomeName").ToList();
Well to find all groupboxes there is no better way than to iterate over all of them. But (for me) the code would look better with this:
List<GroupBox> gBoxes = this.Controls.OfType<GroupbBox>().ToList();
OfType<T> selects all elements of a sequence that are of that type.
Note that this only finds all groupboxes directly contained in this ControlCollection but not in sub-containers. You may want to collect the groupboxes recursively:
public IEnumerable<GroupBoxes> GetAllGroupBoxes(Control c)
{
return c.Controls.OfType<GroupBox>()
.Concat(c.Controls.OfType<Control>().SelectMany(GetAllGroupBoxes));
}
List<GroupBox> gBoxes = GetAllGroupBoxes(this).ToList();
To filter for a specific name you can use Where:
Controls.OfType<GroupBox>().Where(gb => gb.Name == "whatever")...
I have a tab control that has listboxes on it some of which are created and named dynamically so I can't statically program their name. Is there a way to create an array of all the list box names on a give tabPage? I have been going nuts trying to figure out a way to do it.
it would look something like this (based on a winforms example)
List<string> listBoxNames = new List<string>();
foreach (Control control in tabPage1.Controls)
{
if (control.GetType() == typeof(ListBox))
{
listBoxNames.Add(control.Name);
}
}
Or the same thing in linq syntax
List<string> listBoxNames = (from Control control in tabPage1.Controls
where control.GetType() == typeof (ListBox)
select control.Name).ToList();
if you want to find all the listbox's in the tabpage again then see below
foreach (var listBoxName in listBoxNames)
{
ListBox listBox = (ListBox) tabPage1.Controls.Find(listBoxName, true)[0];
}
I have a User Control, containing a Grid, containing a child control.
I want to get a reference to the child control from the code behind for the User Control.
This is what I have:
var childControl = (MyChildControlType)this.Grid.Children.Single(c => (string) c.GetValue(NameProperty) == "MyChildControlNameFromXAMLNameAttribute");
Ugly as a run over garbage can lid.
What is a neater way to do this?
You could either go with the name-hunting, along the lines of what's been suggested already:
var childControl = (MyChildControlType)this.Grid.FindName("MyChildControlNameEtc");
Or, if you wanted a more generic approach to what you're already trying (eg if you want to look up by a different property), you could try:
var childControl = (MyChildControlType)this.Grid.Children.OfType<FrameworkElement>().Single(f => f.Name == "Blah");
or
var childControl = (MyChildControlType)this.Grid.Children.OfType<MyChildControlType>().Single(f => f.Name == "Blah");
Or you could use the VisualTreeHelper, which would work with non-Grids, and would particularly work nicely if you needed to recurse down the visual tree:
for(int i = 0; i < VisualTreeHelper.GetChildrenCount(this.Grid); ++i)
{
var child = VisualTreeHelper.GetChild(this.Grid, i) as FrameworkElement;
if (child != null && child.Name == "Blah")
return child;
}
But really if you can just name it and access it from the codebehind normally like what John Bowen said that's by far the easiest.
Assigning a Name or x:Name to an element in XAML (unless it is inside a template) makes that element accessible from code-behind as a field with that name. So this is basically already declared and populated for you during InitializeComponent:
MyChildControlType MyChildControlNameFromXAMLNameAttribute;
and you can use it directly:
MyChildControlNameFromXAMLNameAttribute.Visibility = Visibility.Hidden;
May be this , Give x:Name to your childControl
var childControl = (MyChildControlType)MyGridNameFromXAMLNameAttribute.FindName("MyChildControlNameFromXAMLNameAttribute");
I have a load of text boxes on an aspx page whose IDs are prefixed with 'txt' the rest of the ID has a corresponding property of the same name in a certain object. I want to be able to enumerate through these string properties and update them where a text box of the same name (with the prefix removed) is found. Any Ideas? I know by using a Dictionary I can get around the problem but it's not ideal.
You can do that using reflection:
MyObject data = new MyObject();
foreach (var pi in typeof(MyObject).GetProperties().Where(i =>
i.PropertyType.Equals(typeof(string)))
{
var control = FindControl("txt" + pi.Name) as ITextControl;
if (control != null)
pi.SetValue(data, control.Text, null);
}
You can work with the controls:
foreach (Control control in divXYZ.Controls)
if (control is TextBox)
((TextBox)control).Text = "whatever";
FindControl is another method you can use in your solution:
Control myControl = FindControl("txtYourID");
http://msdn.microsoft.com/en-us/library/486wc64h.aspx
just find all textbox controls on page and then fill coresponding properties using reflection.