Reference control using string with value of name - c#

Is there a way in C# to reference a control, in my case a TextBox, by using the value of a string variable? I am using the code below to make a single method that multiple control can use for the 'LostFocus' event. The sender TextBox then needs to calculate results based on the contents of other TextBoxes. The problem is that there are about 12 rows of TextBoxes, and while this code works to reuse the event method, I can't think of a way to reference the correct boxes that are not the sender. All of the boxes have similar names (ex - miCellSaturation, miCellRecords, orSaturation, orRecords), so my thinking was that if I can isolate part of the TextBox name with a Substring command, and then concatenate that with another string to form the complete TextBox name, this would work. I can do all that, but I don't know of a way to use the concatenated string to reference that box. Would this require iterating through all the boxes until it matches the correct name?
TextBox box = (TextBox)sender;
string boxName = box.Name;
if(boxName.EndsWith("Saturation"))
{
}

Not sure to understand you problem correctly, but if you need to find the references to a particular type of control with its name ending with a predefined string, then you could use
var list = YourForm.Controls.OfType<TextBox>()
.Where(x => x.Name.EndsWith("YourString"));
foreach(TextBox t in list)
{
Console.WriteLine(t.Name);
......
}
This could work only if your searched controls are directly included in the controls collection of the form. If these textboxes are included in some control container then you need to apply these lines to the appropriate control container instead of the form

Related

Why am I unable to click this selectable option in my settings?

http://puu.sh/mVDoM/069787ca8d.png
I am using teststack white and I tried using:
ListViewRow presetter = p7window.Get<ListViewRow>(SearchCriteria.ByText("Presetter"));
presetter.Click();
TableRow presetter = p7window.Get<TableRow>(SearchCriteria.ByText("Presetter"));
presetter.Click();
ListItem presetter = p7window.Get<ListItem>(SearchCriteria.ByText("Presetter"));
presetter.Click();
I also tried using "Name row 1" and "row 1" for the string that is in the argument.
When you specify and call it to find by text, it is saying the "text" property, not that it might contain text with that value. TableRow doesn't usually have text property, but rather uses things like "Value" "RowIndex" etc.
Also, you should be aware of the place of the element in the tree, you might have to pass it to a parent control.
i.e.
List list = p7window.Get<List>(SearchCriteria.ByValue("Presetter"));
ListItem li = list.Get<ListItem>(SearchCriteria.ByValue("Presetter"));
Try to get AutomationElement for it.
***.GetElement(SearchCriteria.ByText("Presetter"));
If it's not null find a point by getClicablePoint() and perform click by mouse like Mouse.Instance.Click()
If the element is not reachable by any SearachCriteria - try to use native MS UI Automation:
Get the closest root control to yours.
Take Autoelement property from it.
Find whole children list of it like AutomationElement.FindAll(Treescrope.Descendals, PropertyCondition.TrueCondion) _
Find your element in Autoelements Array by any property you need.
Get invoke pattern from it and use Invoke()

Choosing a textBox to be edited from GUI (WinForms)

Let's say I have 10 textBoxes in my form. They are named textBox1, textBox2 etc. And I'd like to be able to choose the textBox I want to edit - for example I have a comboBox with numers 1-10 and if I pick, let's say "5", then the text of the textBox5 is being changed (typed in an additional, eleventh, textBox for example).
I know it sounds weird but I need to learn how to choose controls and edit them from the GUI.
You should use the combobox (cmb in my code) SelectedIndexChanged,
private void cmb_SelectedIndexChanged(object sender, EventArgs e)
{
var numberFromComboBox = cmb.Text;
var txtBoxToEdit = Controls.OfType<TextBox>()
.Where(c => c.Name.EndsWith(numberFromComboBox))
.FirstOrDefault();
if(txtBoxToEdit != null)
{
txtBoxToEdit.Text = "was selected";
}
}
Update
To understand what the code does, one needs to understand a bit of Linq.
All Controls (ComboBoxes, DataGridViews, TextBoxes, etc.) are stored in the Controls collection.
But we only want the TextBoxe's that is directly on the form:
List<TextBox> listOfTxtBox = Controls.OfType<TextBox>();
This listOfTextBox now contains all the textboxes. But we only need one that matches the number we selected in the combobox (cmb).
To do that, we "filter" our collection of textboxes with the Where method.
In my expression it starts with - c => c.Name.EndsWith(numberFromComboBox) gives all the Textboxes that has a name (TextBox.Name) that ends with the number from our ComboBox.
The Last part is FirstOrDefault(), it simply takes the first item in our (now filtred) collection. If there is no items in the collection (for whatever reason) FirstOrDefault will return null
Hopes this helps to clearfi what the code does

How to refer to an object name through a variable?

I have some code that will open a sqllite database, get some street names, and populate a list box that is then attached to a combo-box so it can suggest the names as the user types them. I want to make a procedure that I can call to do this since it will be used on may different combo-boxes. I can pass it the name of the control, but how do I modify attributes of a contol while referring to it as a variable?
Something like:
string A = "cbSTreets"
[A].text = "Sets the Text for the combo box."
If someone could just point me in the right direction I would appreciate it.
Instead of passing it as the name of a control, why not just pass the control itself (especially if they're all comboboxes and the same type?)
Alternatively if for some reason you can't pass the control itself, you could set something up with a datastructure like a dictionary if you need to use the string name:
Dictionary<string, ComboBox> comboBoxes = new Dictionary<string, ComboBox>();
//Use the string you want to refer to the combobox and the combobox item itself
comboBoxes.Add("bcbsTreets", comboBox1);
//Use the name as defined in the previous step to get the reference to the combobox and
//set the text
comboBoxes["bcbsTreets"].Text = "whatever";
You could try something like this:
private void SetText(List<string> streets, ComboBox cb)
{
cb.DataSource = streets;
}
Of course, you'll need to fill the streets from your database and pass in the ComboBox control you want to populate. You can use another collection if you like, but ComboBox datasources need to implement the IList interface.
This is only possible via reflection and should be avoided!
You should do this the normal way.
For the solution how to do it with reflection, look here: How to access class member by string in C#?

Label.property reference

I inserted a label (called Label1) in a "table cell" which in its turn is inserted into a datalist.
Why is it that in this situation, I can't use, for example, Label1.Text inside the Page_Load method, however when I insert the label outside the datalist, any reference to Label1 works perfectly?
Is there a workaround to this, as I need the label in the table cell as described and also to be able to modify its caption (.Text property) from the code-behind file, where I plan to assign it the value of a stored procedure.
Hello and thank you very much in advance.
Label control is created for every row a DataList has, and it's ID is prefixed by every container that it is inside of (so its not called Label1 anymore) - you need to handle OnItemCreated or OnItemDataBound event, then find and cast it as Label Label1 = (Label)e.Item.FindControl("Label1"), and only after that can you access it.
If a control is inside a datalist, then it will be repeated for each item in the list. Consequently, it doesn't make sense to refer to the control outside of the datalist, as ASP.NET would not know which instance to refer to - and if there is no data, there won't even be any instances!
You can access the label given an item in the datalist, however:
DataListItem item = dataList.Items[0]; // or whichever item you want
Label label1 = (Label)item.FindControl("Label1");

Getting a value in a textbox from a dropdown list

I have a textbox named textbox1 and a dropdownlist. Dropdownlist contains the list of branches and i want that when i select a branch from dropdown i want to get the respective branch code to be generated in a textbox from the Database or from c# coding. How will it be possible ?
Am I missing something, is it more complex that just putting an event handler on the dropdown so that when it's value changes it calls your method that can do whatever it needs to do to generate the string for the textbox?
Skipping passed the "database" portion of your question (and assuming your data isn't bound to the DropDownList)..
One way in ASP.NET, to accomplish this, I believe, is to have your code print DropDownList.SelectedValue's value in the textbox during an event.
Here is a link showing how to bind an event to DropDownList in jQuery

Categories

Resources