enter image description here
I have 41 combobox and instead of coding each one of them combobox1.resettext(); bla bla how to make it short
If all of your comboboxes are contained in the same container (mean that they are direct children of the form and not contained in a panel or groupbox) then you could use this code
foreach(ComboBox cbo in this.Controls.OfType<ComboBox>())
cbo.ResetText();
The IEnumerable OfType allows to iterate through the Controls collection of the form extracting only the elements of the specified type.
As explained in the comment below from Mr Schmelter this code loops on every combo present in the form Controls collection. If you have some combos that you don't want to reset then it is a good idea to move these combos in a separate container like a panel (with or without borders) so the code doesn't affect them
You can add all your combobox in an array and iterate through this array.
foreach(var combo in myCombos){
combo.ResetText()
}
If all your combos are in a container you can get them by :
var myCombos=yourContainer.Controls.OfType<ComboBox>();
Related
Right now I have a List<> of Dictionary elements that I display to a webpage. I use a Repeater for the entire List<> because the List<> length is arbitrary, and DropDownList for each Dictionary element. The user sees a Repeating list of DropDownLists and selects one Dictionary element from each of these said DropDownLists as their choice.
I would like to retrieve the Dictionary key of whatever the user's said choice is in each DropDownList. If this step is easier done afterwards, just retrieving everything is fine too, but my end goal is to isolate said keys and put them into an array. Right now I bind the List<> directly to the Repeater and everything displays fine, but I have no idea what to put into the function that is called when they press the final Submit button.
Cheers~
You can loop through each item in the repeater and then find the child control:
foreach (RepeaterItem ri in myRepeater.Items)
{
DropDownList dropDownList = (DropDownList)ri.FindControl("dropDownList");
string myValue = dropDownList.SelectedValue;
}
ive got for example 25 panels that are formed like a grid (5 rows, 5 columns),
if i click at one of those i want to get e.g. the panel above too.
at first i named the panels like their positions e.g. PanelX1Y1, PanelX1Y2 ...
then i took the coords out of the name and created the new coords...
and after i got the new name, i used a foreach loop to go through all the items and get that one with the right name.
ive already tried it with the winforms positions so i took the positions of the clicked one addet e.g. 25 pixels and lopped through all the items in form and checked them via their location.
But i dont want to looped through all the items ...
how can i get the item if i know its name whitout to loop through all items and check for their names..
can i use this:
this.Controls["name"];
okay my grid:
p11 p12 p13
p21 p22 p23
p31 p32 p33
if i click on p31 i want to change something at p31 and p21 so i need the object p21
To directly answer you question, you can use the Find() method of the ControlCollection class. e.g.:
myForm.Controls.Find("panelX1Y2")
To suggest a better method, don't use strings for something like this. It is hackish and sloppy.
Instead, initialize your panels in a 2D array and use the array indices to find the right panel.
Could you not use a dictionary? Remember that a dictionary isn't ordered if this is important to you.
Dictionary<string, Panel> dcPanels = new Dictionary<string, Panel>();
dcPanels.Add("GridA", new Panel());
Panel p = dcPanels["GridA"];
My question is an exact duplicate of this:
"To add items to column 1 in my listView control (Winform) I'm using listView1.Items.Add, this works fine but how do I add items to columns 2 and 3 etc? "
Lots of similar Q&A elsewhere, but none of them talk about how to add items using the WinForms interactive listView builder as opposed to coding it directly. I know how to code it, but since you can add items to the first column using the builder, I assume there must be a way to do it for the other columns.
Right-click on the ListView and select "Edit Items..." to get the ListViewItem Collection Editor.
Select a ListViewItem (or click Add to add one and then select it). In the properties pane find SubItems in the Data category. Click on the "..." button to open the ListViewSubItem Collection Editor and you can add sub-items, which appear in the columns after the first.
You need to set the Text property of your ListViewItems and ListViewSubItems if you want to see anything.
I think (not sure) that most simple way is to add array of strings, and the list view knows the column according to the array index.
How can I select a subitem of an ObjectListView programmatically?
SelectObject() and SelectItem() work only with root items, not with subitems.
The things called subitems in an ObjectListView are actually the strings and images that show in the columns.
If you actually want to use the ObjectListView then the most direct way to select a subitem is
objectListView1.Items[index].SubItems[index]
If you use a TreeListView then you should use the method you already found.
Though I recommend selecting and changing in the source instead.
I solve this problem. It can be useful for anybody, who have similar problem. For this, I need to change a source code of control by next:
Change access type for TreeModel property in TreeListView class from protected to public. After this I have access to manipulate Branch objects of TreeListView object. For example, to select any subitem of root element I write next code:
var branch = tlvMain.TreeModel.GetBranch(tlvMain.SelectedObject);
var children = branch.Children.Cast<SecurityObject>().ToList();
tlvMain.SelectObject(children.SingleOrDefault(p=>p.Id == soft.Id));
I ran into a new problem on my project;
I have a panel on my page in which I add labels, radiobuttons and 2 imagebuttons using pure programming code (because the number of labels and radiobuttons can be different).
I gave each radiobutton a special ID so I know in which content of the panel it belongs.
Now the big problem is I don't know how to get things from the panel.
Let's say the panel was filled with labels and radiobuttons (labels for questions and radiobuttons for the answering score 0-10) and I scored every question as asked, how do I for example select every radiobutton from the panel with an id that ends with '5' and get it's value ?
Name of the panel = pnlMain
my code : http://pastebin.com/gv8ycMY4
Thanks
I Hope you guys could help me out here because I'm really stuck on this.
grtz,
Nico
You could itterate through the panel's controls to get each radiobuttonlist, then itterate through those controls to get the radio buttons.
foreach (Control RBL in pnlMain.Controls)
{
if (RBL is RadioButtonList)
{
foreach (ListItem LI in (RBL as RadioButtonList).Items)
{
if (LI.Text.EndsWith("5") && LI.Selected)
{
// Do something with the radiobutton
}
}
}
}
Found the answer finally;
If you want to keep these dynamic controls after postback etc :
F.E. me, i used a SelectedIndexChange to fill up my panel with dynamic controls.
I made another method named laden() in wich i wrote my code to display these dynamic controls.
If you want these controls to pass postbacks etc simply place the following peace of code into the Page_Load method:
if (Page.IsPostBack) {
laden();
}
Wich means, if you get a postback, it will Re-load these controls after postback.
Even if you got data inserted in a textbox or radiobuttons wich where selected, it will still be the same as before the postback, no data lost.
enjoy.