Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Update:

My question:
I add all the diffrent notes in into a combobox (with the foreach loop). When I select a "note" from the combobox I want to display for example the title or content of the specific note that I selected in the combobox. But how can I acces my note outside the foreach loop?


Hello,

How do I get the a specific note.Title to work outside the foreach loop??

See my code below.

NoteFinder.class
class NoteFinder
{
    private List<Notes> allNotes;

    public List<Notes> GetAllNotesPerUser()
    {
        allNotes = new List<Notes>();

        MySqlDataReader reader = Database.ReaderMeatMill("select * from notes where who = '" + Static.ID + "'");

        while (reader.Read())
        {
            Notes note = new Notes();

            note.id = reader.GetInt32("id");
            note.who = reader.GetInt32("who");
            note.title = Encrypt.DecryptData(reader.GetString("title"));
            note.content = Encrypt.DecryptData(reader.GetString("content"));

            allNotes.Add(note);
        }

        return allNotes;
    }
}


frmMain.form
private void notesBox_SelectedIndexChanged(object sender, EventArgs e)
{
    // noteTitleBox.Text = note.Title; // How do I get this working?
}

// ------------
// Methodes:
// ------------
public override void Refresh()
{
    noteComboBox.Items.Clear();
    noteTitleBox.Clear();
    contentTextBox.Clear();

    // Combobox weer vullen.
    foreach (Notes note in nFinder.GetAllNotesPerUser())
    {
        noteComboBox.Items.Add(note.title);
    }
}




Thanks.
Posted
Updated 13-Jun-11 3:01am
v2

First off you need to declare a form level Notes variable, call it selectedNote. Then you need to actually bind the ComboBox to the List<notes> like this

C#
noteComboBox.Items.Clear();
            NoteFinder nFinder = new NoteFinder();
            noteComboBox.DataSource = nFinder.GetAllNotesPerUser();
            noteComboBox.DisplayMember = "title";


then in your selection changed event you can just cast the selected item as a note and then assign the title like this

C#
private void noteComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            ComboBox cbo = sender as ComboBox;
            selectedNote = cbo.SelectedItem as Notes;
            noteTitleBox.Text = selectedNote.title;
        } 


Hope this helps
 
Share this answer
 
Comments
kZR 13-Jun-11 9:25am    
Thanks, did the job!
Wayne Gaylard 13-Jun-11 9:30am    
Pleasure!
parmar_punit 13-Jun-11 10:23am    
good one... my 5
I don't really know what you're asking, but if you add a ToString() method to your Note object, and then just add the note itself to the combobox, you have access to the entire note object from the combobox (because the Items collection is a collection of Note objects instead of just strings).

C#
public class Note
{
    public string Title { get; set; }

    public override string ToString()
    {
        return Title;
    }
}


Again, your question isn't all that clear.
 
Share this answer
 
Comments
kZR 13-Jun-11 9:02am    
Updated question. :)
#realJSOP 13-Jun-11 9:07am    
My answer is correct then. Instead of using ComboBox.SelectedValue, use ComboBox.SelectedItem and cast it to a Note object. At that point, you have access to the entire Note object and not just the title.
kZR 13-Jun-11 9:08am    
But how can I accomplish this?

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