Array of ListBox Names from a Tab C# - c#

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];
}

Related

Align the text in center for multiple richtextbox in c#

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.

UWP - Update listview source dynamic from code behind

I have a group item. Then, each group in group item, i put it into a listview
var Groups = query.GroupBy(query => query.Name);
foreach (var group in Groups)
{
if (group.records.Count() > 2)
{
ListView listview = new ListView();
var itemsource = new ObservableCollection<FileProperties>();
var header = "";
foreach (var item in group.records)
{
header = item.Name;
itemsource.Add(new FileProperties(item.Name, item.Size, item.DateModified, item.Hash, item.Path, item.IsOrigin));
}
listview.ItemsSource = itemsource;
var itemsTemplate = (DataTemplate)this.Resources["Show"];
listview.ItemTemplate = itemsTemplate;
//test is mother listview
test.Items.Add(listview);
}
}
Now, i have a question, how can i update listview UI if i change value in group items without reset mother listview
The default ListView can be grouped by using CollectionViewSource. There is no need to create a child ListView for each group of the parent ListView.
My answer here shows how to create a grouped ListView, you may take a look. Or there are a bunch of demos on internet, you can googling them.
But the most important point here is that by using the default grouped ListView, we can simply create one data collection for the whole ListView, modify the items source collection to update the grouped children automatically, we don't need to create ObservableCollections for each group any more.

Get only the groupBoxes from a winform

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")...

Save the selected controls in a list and then compare

I want to save some selected controls in a list and then compare, i can only select two so it's okay.
Each of the controls have a boolean property Selected, and i need the : Selected == True.
How do i create a method that returns the Selected controls?
Something like:
foreach (Control c in Controls)
{
if ( this.Selected == true )
{
// how to add to a list
}
}
Using LINQ you can do it in one line, this will put all the selected controls into a list and return:
return Controls.Where(c => c.Selected).ToList();
If you want to use foreach just create a list to store controls and populate your list with selected controls:
var selectedControls = new List<Control>();
foreach(var c in Controls)
{
if(c.Selected) selectedControls.Add(c);
}
return selectedControls;

AllowDrop for only some ListBoxItems

I have a list box representing the contents of a direcory. I want to allow dropping only on those items that represent directories themselves. I've tried two approaches:
First i set the itemsource of the listbox to a compositeCollection of all directory contents and tried to iterate through those with:
foreach (ListBoxItem lbItem in directoryExplorer.Items)
{
MessageBox.Show(lbItem.DataContext.GetType().ToString());
}
The messagebox comes up with 'Directory' or 'UserFile' for each item. I was hoping to have access to the items, check what they represent, and set AllowDrop as necessary.
My second approach was to add individual items like so:
ListBoxItem nxt;
foreach (Directory d in dir.childdirs)
{
MessageBox.Show(d.name);
nxt = new ListBoxItem();
nxt.DataContext = d;
nxt.AllowDrop = true;
nxt.Name = d.name;
directoryExplorer.Items.Add(nxt);
}
foreach (UserFile f in dir.childfiles)
{
MessageBox.Show(f.name);
nxt = new ListBoxItem();
nxt.AllowDrop = false;
nxt.DataContext = f;
nxt.Name = f.name;
directoryExplorer.Items.Add(nxt);
}
but then it just comes up blank.
look at mdm20's answer wpf treeview blues. I want to select an item
Just make an allowDrop property and bind it appropriately.

Categories

Resources