Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Building a WFA WPF app and I have a file I am reading a list of names and a value associated with the name. I want to be able to:

1) display the list to a user
2) have them click on the appropriate name, this allows them to edit the list item

I am not that familiar with GUI's though and am having a hard time wrapping my head around event's. Was wondering how I would dynamically create events that tied to the associated objects information displayed in that text box. Also then for formatting not sure how that would exactly work for my window I want to place them all in. I could use a database but I feel that would be overkill (the list would be rather small). Was thinking to use a hashmap to store the objects.

-Clarification-

I have read the list from an excel file and created a list of objects(my own class I have written). What I want to do is populate a list of the names associated with each object (I think just text boxes should do the job). The real meat of my question is handling the user selecting them (Thinking I open a new form that allows them to edit the values the class holds, modifying the object directly I should not have to return it since the object will be the same). I am not used to handling events themselves, much less for dynamically created graphical elements like I am planning to do.
Posted
Updated 18-Dec-11 8:22am
v3
Comments
Sergey Alexandrovich Kryukov 17-Dec-11 19:27pm    
Everything is done in a more or less trivial ways using WPF API. So, what's the problem?
--SA
BillWoodruff 18-Dec-11 10:01am    
I find your question confusing: you seem to be asking a simple question, at first, which is how to "read something" (you don't say if that means reading from a file, or not), and populate a "list of names." And you want the user to be able (at run-time, I assume) to edit the names: that should not be difficult.

What you don't say is what the "values" associated with the names are, and what exactly you mean by associating "events" with them.

Please clarify.
Sergey Alexandrovich Kryukov 19-Dec-11 1:09am    
Thank you, Bill.
OP clarified the question, I answered -- please see.
--SA

1 solution

After clarification, it's more or less simple.

Here is the technique to use:

You can better use the ListBox and populate it with your objects. Now, attention: not with the names of your objects, but with the instances of the objects of your classes themselves. This control allows objects of any types to be added as list items. In this way, each list item will carry complete set of data to be used when the user clicks on the item, selects if or whatever else. Now, the only problem is: what text will be shown as the text of list item? The answer is simple: whatever ToString returns. So, you should modify all your classes to be used in this list: properly override System.Object.ToString(). Let's see:

C#
class MyListItemData {
    internal MyListItemData(string name, string description /* ,... */) {
        this.fName = name;
        this.fDescription = description;
        //...
    } 
    internal string Name { get { return fName; } }
    internal string Description { get { return fDescription; } }
    public override string ToString() { return fName; } //this will be shown in UI 
    //...
    string fName, fDescription;
    //...
} 


Good. Now let's see how it can be used in event handlers. For example, let's implement simple functionality: showing description of each item in selection.

C#
myListBox.SelectedIndexChanged += (sender, eventArgs) => {
       ListBox listBox = (ListBox)sender; //must be myListBox
       int selected = listBox.SelectedIndex;
       if (selected < 0)
           ShowSelectedItemDescription(null);
       else {
           MyListItemData data = (MyListItemData)(listBox.Items[selected]); //one more type cast, the last one
           ShowSelectedItemDescription(data);
       }      
}

//...

void ShowSelectedItemDescription(MyListItemData data) {
    string value;
    if (data == null)
        value = string.Empty;
    else
        value = data.Description;
    myLabel.Text = value;
    //... ?
}


You can process any other list box events in this manner. An important thing is: you extract the data associated with the item. One hint: you can do more: you can do some action based on the item content. For this purpose, you can include some delegate instances in your class used to add as the list item.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900