How to get elements from an object in C#? - c#

I am using the AutoCompleteBox in WPF, I populate the suggestions with a List that consists of four fields. When the user selects an item and I reach my eventHandler, i can see that
MyAutoCompleteBox.SelectedItem
is an object that has my four values, if i hover this text in the debugger i can see the four values listed, however i don't know how to access these values in the code.
I tried
List<Codes> selected = MyAutoCompleteBox.SelectedItem as List<Codes>;
where Codes is my List. selected returns as null and empty every time. Is there a way to get to these values? Thanks!

If you want the listing of items used as the backing collection for the AutoCompleteBox try...AutoCompleteBox.ItemsSource.

Can you try:
Codes selected = MyAutoCompleteBox.SelectedItem as Codes;
or
Codes[] selected = MyAutoCompleteBox.SelectedItem as Codes[];

It means that you cannot convert whatever MyAutoCompleteBox.SelectedItem is to a List.

Related

How to make a listbox in c# not duplicate items on button click?

Well, i'm using ASP.NET c# listbox. I also have a button, the idea is the following: whenever I click the button an item x has to be shown in the listbox. However, I click the button many times, and instead of replacing the x value in the list for the new value (pretend it's y), it shows the following:
x
y
The expected result would be:
y
I mean, i don't want values to be on top of eachother, just one at a time.
You should be having a code behind file where the selected items (selected by clicking the button) should be getting added to the listBox object.
When you run a loop to add items to this listBox object, use the Contains method to check if the listBox object already contains the current item. Add only if it does not contain.
At code behind :
Before that you need to create list and store all the listbox value. if listbox doesn't have value then keep list as null.
Every time you need to check whether the list contain the value or not if list contain value then you don't need to add, if not contain then you need to add.
in Jquery :
on button click first get all the listbox list item value and check that in that list whether that list contain the value or not which you want to add.
May be this is help full for you.

Combobox.SelectedValue wont select the actual value

I have a problem that I'm struggling with.
I fill a combobox this way:
RDTA_cb_provincias.DataSource = provincias;
RDTA_cb_provincias.DisplayMember = IdiomaBD.ObjCI.ToString().Equals("eu-ES")
? "TTEXNOMBRPROV_EU"
: "TTEXNOMBRPROV_ES";
RDTA_cb_provincias.ValueMember = "TCODIDTERRITORIO";
There's some stuff depending on the language of the user, but i guess that's not important here.
Well, when i want to select depending on the value member i do this:
RDTA_cb_provincias.SelectedValue = provincia2.TCODIDTERRITORIO;
But for some reason it won't show up the actual item I want to show. While debugging i can see that RDTA_cb_provincias.SelectedValue matches provincia2.TCODIDTERRITORIO.
For example, I've set SelectedValue to 51, but when i see the SelectedItem inside the ComboBox once I've changed the value, It shows up an object that has 47 as its TCODIDTERRITORIO.
What can it be? Or how can I bypass this problem (Maybe iterating the whole ComboBox item list until i find the one i want to select?)
Thanks in advance!
You should use SelectedItem instead:
RDTA_cb_provincias.SelectedItem = provincia2;
Or you can use SelectedIndex as follows:
RDTA_cb_provincias.SelectedIndex = RDTA_cb_provincias.FindStringExact(provincia2.TCODIDTERRITORIO)

WinForms ListView - get source object from selected string item

I am building a simple language learning helper application in WinForms. One of its modules is a dictionary. It consists of "Sets" and words are stored in a Set. User can create a new Set of words and store some of them in it.
I'm printing all the words from selected one or several Sets in a ListView with columns. Upon checking a Set or Sets in CheckedListBox the List clears and prints words (as string variables).
Trouble comes when there are few Sets checked with their words listed, and user wants to edit one of the listed words. I cannot use indexes (such as index of List item equals to word item in a Set), as those List string items are from different Sets.
Is there any way get a source object from a ListView item? I have not added objects to the list but only some of their variables, but are they somehow connected?
Thank you for your help.
Cheers!
EDIT: Explaining why to me setting a Tag is not a solution:
All the Sets are stored in a List<DictionarySet> dictionarySets. Every Set containts a List<Word> words where words are stored.
How do I fill the ListView:
private void UpdateList()
{
wordsListView.Items.Clear();
List<Word> currentSetWordList;
foreach (DictionarySet ds in setCheckedListBox.CheckedItems) //List<DictionarySets> dictionarySets inserted, DisplayMember set to its Name property
{
currentSetWordList = ds.words;
foreach (Word w in currentSetWordList)
{
ListViewItem newItem = new ListViewItem("--"); //for now a string, later an enum
newItem.Tag = ds;
newItem.SubItems.Add(w.GermanTranslation); //string property
newItem.SubItems.Add(w.PolishTranslation); //string property
wordsListView.Items.Add(newItem);
}
}
}
In this case the program loops through each Set and its word list and prints the words. Their tag is DictionarySet everywhere.
You could use the Tag property on the ListViewItem. The Tag property:
Gets or sets an object that contains data to associate with the item.
In short, when you create each item of the list view, you could add your object as the tag. This way you'll be able to get it back when the user selects an item.
See MSDN for details.

C# Combobox Displaying Blank Items

I'm coding a combobox in C# and for some reason the items in the drop down don't have text. When I have selected an item, it is shown in the combo box text field (the drop down list is always blank whenever I click the drop down button). The datasource seems bound properly because the proper values are being returned when I select items, and the size of the drop down list will change depending on how many items the datasource has. Everything looks fine except for the fact that it seems like my drop down is populated with a bunch of empty strings, which it clearly isn't since as soon as an item is selected the proper text will display.
This is the relevant code:
if (list.Count > 0)
{
cboCustomers.DisplayMember = "Name";
cboCustomers.DataSource = list;
cboCustomers.ValueMember = "ID";
cboCustomers.SelectedIndex = 0;
}
I have looked for an answer to this but can't find it anywhere...I'm sure it's something really simple, but I can't figure it out. The closest problem I found had an answer suggested to set the display member before the data source, which clearly didn't work.
The list is populated from a database query. This will run on keyUp, the idea is that the list is populated as the person is typing based on the info given. So if I wrote 'S' I'd get a combobox with a dropdown that had all the clients starting with 'S'.
Given you don't have any anomalies in your binding, you are probably being affected by DrawMode property of your ComboBox, which may be set to OwnerDrawFixed or OwnerDrawVariable. Set it to Normal and things should get better.
as soon as an item is selected the proper text will display.
A foreground color the same as the background color will produce the same results you are seeing.

Working thru sample code

I am trying to work thru some sample code.
The code I am having a problem with is:
private ListControl GetSelectedList()
{
return (ListControl)FindControl(ddlControls.SelectedItem.Value);
}
ddlControls is a DropDownListBoxControl collection
What does the ddlControls.SelectedItem.Value return (its a numeric value, but I don't know what it represents)?
2nd question: What is return (ListControl)FindControl(ddlControls.SelectedItem.Value); ?
Thanks
SelectedItem.Value, as the name suggests, is the value of the currently selected item in the drop-down list. For example, if this were a list of months and someone had selected "September", this property has the value "September".
what is return (ListControl)FindControl(ddlControls.SelectedItem.Value);
FindControl is a method that looks up controls by their id. Using our example from before, it would try to find a control with the name "September". The (ListControl) at the beginning is a cast; the GetSelectedList() method is implicitly assuming that every possible answer you could get from ddlControls is the name of another ListControl. (This might not be a good idea depending on the circumstances.)
The result -- that is, the control whose id is the same as the currently selected value in ddlControls -- is then returned, and that's the result of GetSelectedList().
DropDownList Class in MSDN has the answer to the first question. In particular, it links to ListControl.SelectedItem, which is defined as:
If the list control allows only a single selection, use this property to get the individual properties of the selected item. If the list control allows multiple selections, use this property to get the properties of the lowest indexed item selected from the list control.
Similarly, Control.FindControl gives the answer to your second question. It's defined as:
Searches the current naming container for a server control with the specified id parameter.

Categories