how to see details about an item present on listbox....i want to know the code?

Recommended Answers

All 7 Replies

yes i meant the first one but a little bit broader like....

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Get the currently selected item in the ListBox.
string curItem = listBox1.SelectedItem.ToString();

after getting item from listbox i want to show the details of that item on another form in form2 and the details of that item is situated on my database table.Now what is the code for that Form2 loading?

About which details are you talking about? There is nothing like it.

yes i meant the first one but a little bit broader like....

private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
// Get the currently selected item in the ListBox.
string curItem = listBox1.SelectedItem.ToString();

after getting item from listbox i want to show the details of that item on another form in form2 and the details of that item is situated on my database table.Now what is the code for that Form2 loading?

That depends on how you write the second form. Typically you'll want to create a method in the "Form2" class where you pass it the newly selected object. Then the second form can do the database lookup and display whatever details you want.

How is your application structured? I mean, when do your forms get created, and does one (or both) know about the other?

there is a name list in listbox and for each name there is details about that name i have kept on visual studio 2008 database.now how i show the details in another form when i click that item or name in listbox,hope u understand my term..

there is a name list in listbox and for each name there is details about that name i have kept on visual studio 2008 database.now how i show the details in another form when i click that item or name in listbox,hope u understand my term..

I think I understand what you're doing... let me know if I'm off.

The form that has your list needs to have a reference to the form that shows the details. When the SelectedIndexChanged event happens on your listbox, the event handler should set the details for the second form.

Here's a basic example of one way the forms might work together. There are other ways to manage this, depending on how you want it to work, but this should be sufficient to demonstrate the idea:

using System;
using System.Windows.Forms;

class Program
{
    static void Main()
    {
        Application.Run(new ListForm());
    }
}

class ListForm : Form
{
    ListBox listBox;
    
    DetailsForm detailsForm;
    
    public ListForm()
    {
        listBox = new ListBox
        {
            Dock = DockStyle.Fill
        };
        listBox.SelectedIndexChanged += ListBoxSelectionChanged;
        Controls.Add(listBox);
        
        Load += OnLoad;
    }

    void OnLoad(object sender, EventArgs e)
    {
        // Your own list-loading code goes here.
        listBox.Items.Add("uno");
        listBox.Items.Add("dos");
        listBox.Items.Add("tres");
        listBox.Items.Add("cuatro");
    }

    void ListBoxSelectionChanged(object sender, EventArgs e)
    {
        // Here's one place you could load the details for the selected item.
        string details = (string)listBox.SelectedItem;
        
        // This section manages whether or not to create a new details
        // form, setting the detail information on it, and showing the
        // form if it's not visible.
        if((detailsForm == null) || detailsForm.IsDisposed)
        {
            detailsForm = new DetailsForm();
            detailsForm.SetDetails(details);
            detailsForm.Show();
        }
        else
        {
            detailsForm.SetDetails(details);
        
            if(detailsForm.WindowState == FormWindowState.Minimized)
            {
                detailsForm.WindowState = FormWindowState.Normal;
            }
        
            detailsForm.BringToFront();
        }
    }
}

class DetailsForm : Form
{
    TextBox textBox;
    
    public DetailsForm()
    {
        textBox = new TextBox
        {
            Dock = DockStyle.Fill,
            Multiline = true,
            ReadOnly = true
        };
        
        Controls.Add(textBox);
    }
    
    public void SetDetails(string details)
    {
        // Your actual 'details' parameter will be more than just a string;
        // set whatever controls you need on the details form here.
        textBox.Text = String.Format("Details for '{0}'!", details);
    }
}

Does that clarify things for you?

The easiest way is to fill DataTable (from dataBase). Then use DataSource property of listBox to bind data to it. Use DisplyMember to show the names, and Id for ValueMember.
So next is, when you click on the item (name) you get the id of person of selected item, and based on that, you can get all the other data of that person from dataTable.
Then you only pass these data (as an array of objects maybe) where ever you want ( to other form as you wanted).
Is this clear?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.