Click here to Skip to main content
15,902,189 members
Home / Discussions / C#
   

C#

 
GeneralRe: Annoying ListBox problem Pin
OriginalGriff22-Oct-10 5:14
mveOriginalGriff22-Oct-10 5:14 
GeneralRe: Annoying ListBox problem Pin
Luc Pattyn22-Oct-10 5:17
sitebuilderLuc Pattyn22-Oct-10 5:17 
GeneralRe: Annoying ListBox problem Pin
OriginalGriff22-Oct-10 5:24
mveOriginalGriff22-Oct-10 5:24 
GeneralRe: Annoying ListBox problem Pin
Luc Pattyn22-Oct-10 5:19
sitebuilderLuc Pattyn22-Oct-10 5:19 
GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 5:30
Henry Minute22-Oct-10 5:30 
GeneralRe: Annoying ListBox problem Pin
Luc Pattyn22-Oct-10 5:59
sitebuilderLuc Pattyn22-Oct-10 5:59 
GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 6:06
Henry Minute22-Oct-10 6:06 
GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 6:48
Henry Minute22-Oct-10 6:48 
Sorry for the brevity of my last reply but I was half way through preparing my Dinner.

More context.

This is a UserControl containing a ListBox, a TextBox and three Buttons (Add, Edit, Delete)
When user selects an item in the ListBox it is copied to the TextBox where they can edit it and hit Edit button to change content of LB. Replace text in TextBox hit Add button to add new text to LB. Hit Delete deletes all selected items from LB

C#
    private List<string> data;
    private bool adding = false;

    public EntryEditor()
    {
        InitializeComponent();
    }

    private void RefreshDataBoundControls()
    {
        CurrencyManager cm = (CurrencyManager)BindingContext[this.data];
        cm.Refresh();
    }

    #region EntryEditor PROPERTIES .........................................
    public List<string> Data
    {
        get
        {
            return this.data;
        }

        set
        {
            if (this.data != value)
            {
                this.data = value;
                this.lboxEntries.DataSource = value;
            }
        }
    }
    #endregion

    private void lboxEntries_SelectedValueChanged(object sender, EventArgs e)
    {
        this.btnEdit.Enabled = false;
        this.btnDelete.Enabled = false;
        if (!this.adding)
        {
            this.txtEditEntry.Text = "";
        }
        switch (this.lboxEntries.SelectedItems.Count)
        {
            case 0:
                break;
            case 1:
                if (this.lboxEntries.SelectedIndex >= 0)
                {
                    this.txtEditEntry.Text = this.lboxEntries.SelectedValue.ToString();
                    this.btnEdit.Enabled = true;
                    this.btnDelete.Enabled = true;
                }
                break;
            default:
                this.btnDelete.Enabled = true;
                break;
        }
    }

    private void btnEdit_Click(object sender, EventArgs e)
    {
        this.data[this.lboxEntries.SelectedIndex] = this.txtEditEntry.Text;
        RefreshDataBoundControls();
    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        string[] selectedItems = new string[this.lboxEntries.SelectedItems.Count];
        this.lboxEntries.SelectedItems.CopyTo(selectedItems, 0);
        foreach (string s in selectedItems)
        {
            this.data.Remove(s);
            RefreshDataBoundControls();
        }
    }

    private void btnAdd_Click(object sender, EventArgs e)
    {
        this.adding = true;
        try
        {
            //this.lboxEntries.SelectionMode = SelectionMode.One;
            //this.lboxEntries.SelectedIndex = -1;
            this.lboxEntries.ClearSelected();
            this.lboxEntries.Update();
            this.data.Add(this.txtEditEntry.Text);
            RefreshDataBoundControls();
            this.lboxEntries.SelectedIndex = data.Count - 1;
            //this.lboxEntries.SelectionMode = SelectionMode.MultiExtended;
        }
        finally
        {
            this.adding = false;
        }
    }
}


btnAdd_Click is the problem area.
Henry Minute

Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”

GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 7:08
Henry Minute22-Oct-10 7:08 
GeneralRe: Annoying ListBox problem Pin
Luc Pattyn22-Oct-10 7:33
sitebuilderLuc Pattyn22-Oct-10 7:33 
GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 7:41
Henry Minute22-Oct-10 7:41 
GeneralRe: Annoying ListBox problem Pin
Luc Pattyn22-Oct-10 14:36
sitebuilderLuc Pattyn22-Oct-10 14:36 
GeneralRe: Annoying ListBox problem Pin
Henry Minute22-Oct-10 5:31
Henry Minute22-Oct-10 5:31 
QuestionCrystal Report Formula needed Pin
Nikhil Bhivgade22-Oct-10 1:29
professionalNikhil Bhivgade22-Oct-10 1:29 
AnswerRe: Crystal Report Formula needed [modified] Pin
Sivaraman Dhamodharan22-Oct-10 2:51
Sivaraman Dhamodharan22-Oct-10 2:51 
GeneralRe: Crystal Report Formula needed Pin
Nikhil Bhivgade22-Oct-10 21:35
professionalNikhil Bhivgade22-Oct-10 21:35 
QuestionHost WPF application on WEB Pin
VisualLive22-Oct-10 0:15
VisualLive22-Oct-10 0:15 
AnswerRe: Host WPF application on WEB Pin
Simon P Stevens22-Oct-10 1:13
Simon P Stevens22-Oct-10 1:13 
AnswerRe: Host WPF application on WEB Pin
#realJSOP22-Oct-10 1:41
professional#realJSOP22-Oct-10 1:41 
AnswerRe: Host WPF application on WEB Pin
Nish Nishant22-Oct-10 2:02
sitebuilderNish Nishant22-Oct-10 2:02 
GeneralRe: Host WPF application on WEB Pin
Abhinav S22-Oct-10 2:47
Abhinav S22-Oct-10 2:47 
GeneralRe: Host WPF application on WEB Pin
Nish Nishant22-Oct-10 4:15
sitebuilderNish Nishant22-Oct-10 4:15 
AnswerRe: Host WPF application on WEB Pin
Abhinav S22-Oct-10 2:46
Abhinav S22-Oct-10 2:46 
GeneralRe: Host WPF application on WEB Pin
VisualLive23-Oct-10 19:17
VisualLive23-Oct-10 19:17 
QuestionFind Label in Gridview Footer Row in javascript Pin
SatyaKeerthi1521-Oct-10 23:33
SatyaKeerthi1521-Oct-10 23:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.