get panel at specific positions in C# - c#

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

Related

Iterating through 2 dynamic lists

I'm in the middle of creating a piece of software for the company that I have internship at, now I'm having some trouble iterating through 2 dynamic lists.
The first list is a list of grids (WPF) and the other one is list of Column Definitions (WPF) I'd like to add 3 columns to each grid in the GridList, but I'm not quite sure on how I'd go about doing so.
If you need some code examples then I'll happily add them, but I don't see it nessecary for this kind of question.
When I say Dynamic List, I mean a list that doesn't have a set size, so in my case it depends on which day it is.
Create an Object of list, and add your grids to it:
var grids = new List<Grid>
{
new Grid(),
new Grid(),
new Grid()
};
Iterate through the grids and add what you want to them:
foreach (var grid in grids)
{
grid.ColumnDefinitions.Add(new ColumnDefinition());
}

how to identify the first, second and third element when the object names are the same in CodedUI tests

I am writing CodedUI tests for a web application. There are three text boxes with the same name and I would like to know how do we call these text boxes ? Please advise ?
Use this:
var control = new HtmlControl(parent)
control.SearchProperties.Add([Control Type], [Control Name]);
var specificControl = control.FindMatchingControls()[index]
In the above code, what it does is find the three controls you mentioned with the same name, and then index them in a collection. By taking a piece of that collection with "[index]", you can isolate a single control.
This is what it looks like in practice in a WPF app:
//Identify the cell and minimize button 2017
WpfCell currentyearCell = new WpfCell(workWindow);
currentyearCell.SearchProperties.Add(WpfCell.PropertyNames.Value, DateTime.Now.AddYears(0).ToString("yyyy"));
currentyearCell = currentyearCell.FindMatchingControls()[0] as WpfCell;
If 3 elements has same property and if you will provide a search properties
Control.searchproperties.add("","")
And you intend to select 2 element.Than by this approach it will automatically identify the first element.
Just go for next sibling search configuration.
So it will go the next element or we can use children element[pass the index]

how to delete all combobox in short codes

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>();

How to get values from dynamically created elements using jquery in code behind

Hi and thanks for taking the time to asnwer my question.
I have the following problem. I have a form and a button which says "add new activities".
Whenever the button is clicked I add a new set of elements, namely 2 drop down menus + text area. How can I get the values of these newly created elements in code behind since I cannot know their ids up front?
If there is something unclear about my question, please let me know.
Thanks again!
But you must be setting id's (more importantly - name attributes) of new elements using certain pattern. Use the same pattern in a loop in server-side code to get values from Request.Form. Provide a hidden input where you put the total count of items added for the server-side to know the upper bound of loop counter.
You should set the ids when you create the elements if you plan to access them again
So if you this is your text area:
var textarea = document.createElement('textarea');
You can set the id to like this:
textarea.id = "taId";

Reading back ListBox values and setting default

I'm new to this site, and basically I have created a Windows Form using C#.
On this form I have 3 values.
I have a button to access the values in my ListBox.
How could I make it so when the form loads, the first element in my list box is highlighted?
I was also wondering is there a way to start the indexing off for 1, if the first element in my list is selected? rather than 0?
I hope I have stated this as clearly as I should,
To select the first index, add this line to the OnLoad event of the form:
myListBox.SelectedIndex = 0;
As for your second question about changing the indexing range, the answer is no. You could use 1 to whatever, but then you would have to use -1 after. Like so:
myListbox.Items.Remove(Your_Value - 1);

Categories