Getting SubItem from listview - c#

I am trying to get Subitem from a list view, I did this but doesn't work:
curItem1=listView2->Items[i]->Text;
curItem2=listView2->SubItem[i]->Text;

ListViewItem selItem = listView1.Items[i];
string txt = selItem.SubItems[index].Text;
To get the Text is wrong in first line.

Try this... ListView1.SelectedItems(0).SubItems(i).Text

You want to access the Text property of a certain control inside the ListViews ItemTemplate. This is done similarly to this:
string curItem = ((TextBox)listView2.Items[i].FindControl("TextBox1")).Text;
Where 'TextBox1' is the control you are trying to get the Text property of. You also need to make sure that you are casting to the correct type of the Control you are trying to get the property for.

Related

Combobox with Textbox content issue

I am running into an issue when using a textbox as the content of a combobox in WPF. When I have some items in the combobox already and then enter an extension of one of those items in the textbox, the textbox clears itself when it matches one of the existing items. An example:
The combobox contains the following:
'test1'
'test2'
I then attempt to enter the value 'test23'. When I get to 'test2' the matching value in the combobox is highlighted. When I proceed to type the '3' in 'test23', the textbox is cleared and all I am left with is a '3'. Obviously this is not a desired behavior.
I've looked through the configurable properties on microsoft's documentation pages and I haven't been able to find a property or combination that allows me to disable this behavior. Does anyone know what's going on here as well how I might fix it? Thanks.
If your are trying to add extension text to your default text in combobox items, try in code behind to declare the text items as a stringbuilder and use the property Append as follow
Code behind:
System.Text.StringBuilder text = new System.Text.StringBuilder();
TextBox textitem = new TextBox();
text="set the default text you want";
textitem.text=text;
Combobox.items.add(textitem);
text.append("the extension text you want");
Hope it helps.

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

Get items from DataTemplate for ArtOfTest controls

I have this kind of object in code:
ArtOfTest.WebAii.Controls.Xaml.Wpf.ListBox
Items of it are in ItemTemplate. I have collection of it:
var listBoxItems = repairComapanyHintsList.Find.AllByType<ListBoxItem>();
and I want to get control from that DataTemplate. How can I do it? That solution doesn't work for DataTemplate (for other situations it's ok):
var textBlock = listBoxItem.Find.ByName("Name");
How can I get it? I tried this solution too, but that controls (from ArtOfTest) doesn't DependencyObject:
How can I find WPF controls by name or type?
I want to select one element depend of that TextBox text value.
I solved it, but it is not very good solution. It works for me and maybe it will help somebody.
I do it like that:
var listBoxItem = repairCompanyList.Find.AllByType<ListBoxItem>().FirstOrDefault(r => r.Text == "Name");
Assert.IsNotNull(listBoxItem, "Lack of expected list box item");
listBoxItem.User.Click();
I checked

Reference control using string with value of name

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

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#?

Categories

Resources