tags:

views:

3667

answers:

5

Hello there. I'm writing an C# App (WinForm) with a ListBox having content added by the user. Now, I could have a ordinary button under the ListBox to remove items, but I would like to have the button right next to the content, thus being inside of the ListBox.

Like this:

  • Content 1 | X
  • Content 2 | X
  • ...
  • Content 5 | X

The problem is that I lack experience in .NET so I have no clue on how this would be possible with all the automated controls going on. I've googled it, but came up with no meaningful results.

Any hints, clues or snippets for achieving this are welcome! :)

A: 

I don't know why you would want to do specifically that? I would put a button at the bottom that deletes any selected items in the listbox. That is deemed the usual way of doing such a thing unless you want to use jquery and put an onClick event on the listbox that sends a call to delete the item if it is stored in a database or remove the item from the list on the page.

Ian Roke
Well, I'm not usual and I tend to go for the more advanced option. The thing is that a button (or iconbutton) on the side is more pleasing to the eye and also reduces clickcount by one click per removal.
Robelirobban
+1  A: 

Assuming it's a WinForms app

You'd need a custom control for that. I would check around vendors like http://www.devexpress.com/Products/NET/Controls/WinForms/Editors/editors/ListBoxes.xml maybe someone knows of a control that specifically does that.

Chad Grant
Alright, just what I needed to hear, custom control it is :)
Robelirobban
You can make your own control, but it is not a trivial task
Chad Grant
+1  A: 

using System; using System.Collections.Generic; using System.Windows.Forms;

namespace WindowsFormsApplication11 { public partial class Form1 : Form { List _items = new List();

    public Form1()
    {
        InitializeComponent();

        _items.Add("One");
        _items.Add("Two");
        _items.Add("Three");

        listBox1.DataSource = _items;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // The Add button was clicked.
        _items.Add("New item " + DateTime.Now.Second);

        // Change the DataSource.
        listBox1.DataSource = null;
        listBox1.DataSource = _items;
    }

    private void button2_Click(object sender, EventArgs e)
    {
        // The Remove button was clicked.
        int selectedIndex = listBox1.SelectedIndex;

        try
        {
            // Remove the item in the List.
            _items.RemoveAt(selectedIndex);
        }
        catch
        {
        }

        listBox1.DataSource = null;
        listBox1.DataSource = _items;
    }
}

}

private void button1_Click(object sender, EventArgs e) { // The Add button was clicked. // ...

button2.Enabled = true;

}

private void button2_Click(object sender, EventArgs e) { // The Remove button was clicked. // ....

if (listBox1.Items.Count == 0)
{
    button2.Enabled = false;
}

}

Should read the question better ;)
Chad Grant
Yeah, I don't think you read my question very thorough :)
Robelirobban
+2  A: 

Instead of ListBox you can use ListView, ListView has the ability to add custom column types.

Shay Erlichmen
ListBox and ListView are miles apart man. Maybe he should throw in a tree control for good measure
Chad Grant
ListView in report mode without the headers with full row select and not grid lines look and behave the same as ListBox.It more easy to add buttons (or any other control for that matter) as a sub item, better then trying to create a custom control.
Shay Erlichmen
+2  A: 

So one could make a custom control but for my app it isn't really worth the trouble.

What I did was to create a DataGrid, made it resemble a ListView but with its own flare. I did this because the DataGrid already has a buttoncontrol built in to its cells.

Yes I know, kind of fugly "hack", but it works like a charm! :)

Props to Shay Erlichmen who led me into thinking outsite my ListBox. See what I did there? ;)

Robelirobban
Yes, DataGrids work great for this, though they are more complicated than a ListBox. You can make it look like a table in HTML or some similar style.
Greg Ogle