문제

I need to add checked items from one checklistbox to another. I should not add an item more than once into the second checklistbox. The design has two checklistboxes and the following buttons: >,>>,<,<< I have tried the code below but not able to get any display.Please let me know where i am going wrong:

namespace TwoListBox
{
    public partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();
        }

        public delegate void OneArrowForwardClickHandler();

        public event OneArrowForwardClickHandler OneArrowForward;

        private void button1_Click(object sender, EventArgs e)
        {
            OneArrowForward += new OneArrowForwardClickHandler(OneForward);
        }

        public void OneForward()
        {
            foreach (Object cs in checkedListBox1.CheckedItems)
            {
                if (!checkedListBox2.Items.Contains(cs))
                    checkedListBox2.Items.Add(cs.ToString());
                else
                {
                    MessageBox.Show("Item is already added");
                    continue;
                }
            }
        }

        public delegate void TwoArrowForwardClickHandler();

        public event TwoArrowForwardClickHandler TwoArrowForward;

        private void button2_Click(object sender, EventArgs e)
        {
            TwoArrowForward += new TwoArrowForwardClickHandler(TwoForward);
        }

        public void TwoForward()
        {
            foreach (Object cs in checkedListBox1.Items)
            {
                if (!checkedListBox2.Items.Contains(cs))
                    checkedListBox2.Items.Add(cs.ToString());
                else
                    MessageBox.Show("Items were already added");
            }
        }

        public delegate void OneArrowBackwardClickHandler();

        public event OneArrowBackwardClickHandler OneArrowBackward;

        private void button3_Click(object sender, EventArgs e)
        {
            OneArrowBackward += new OneArrowBackwardClickHandler(OneBackward);

        }

        public void OneBackward()
        {
            foreach (Object cs in checkedListBox1.CheckedItems)
            {
                checkedListBox2.Items.Remove(cs);
            }
        }

        public delegate void TwoArrowBackwardClickHandler();

        public event TwoArrowBackwardClickHandler TwoArrowBackward; 

        private void button4_Click(object sender, EventArgs e)
        {
            TwoArrowBackward +=new TwoArrowBackwardClickHandler(TwoBackward);
        }

        public void TwoBackward()
        {
            foreach (Object cs in checkedListBox1.Items)
            {
                checkedListBox2.Items.Remove(cs);
            }

        }

        private void button5_Click(object sender, EventArgs e)
        {
            string s = "The selected items are:";
            for (int i = 0; i < checkedListBox2.Items.Count - 1; i++)
            {
                s += checkedListBox2.CheckedItems[i].ToString() + "\n";
            }
            MessageBox.Show(s);
        }

    }
}

Note: > means add checked items ,>> means add all items,< means delete selected items,<< means delete all items

도움이 되었습니까?

해결책

In your button click events your only adding an event handler to the events. Have you tried calling the function directly?

Instead of creating an event handler, call each of the functions similar to how you would call OneForward() below from your button1's click event:

    private void button1_Click(object sender, EventArgs e)
    {
        OneForward();
    }

Response to comment

If you mean that you want to inform the parent that your button was clicked and the OneForward had completed you can the following line at the end of your OneForward function:

OneArrowForward(this, new EventArgs());

But if you want to call OneForward from the parent, you will have to make it public then from the parent you would simply call:

myUserControlInstance.OneForward();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top