Convert Data Items from a Control to Another
c# move of copy all data items from a control to anotherIn C# copying or moving a data from a control is very necessary. Most using data showing controls are textbox, list box, combo box etc. Here are some codes to move data from one control to another.
Here all codes indicate only copying data from one control to another. To move, you have to clear the source data.

  • To copy a text from a text box to list box or combo box? 
Write this code inside in an event like button click [Button events and properties here]
Learn how to add items or elements to a listbox here.

c# move copy item from text box to list box
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace list_box_to_combo_box
{
   public partial class Form1 : Form
     {
      public Form1()
        {
           InitializeComponent();
        }
private void button3_Click(object sender, EventArgs e)
     {
        listBox2.Items.Add(textBox1.Text);
     }
      }
}

  • To copy all items from a list box to another list box:

c# move copy item from list box to another list box

    private void button1_Click(object sender, EventArgs e)
    {
    int count = listBox1.Items.Count;
      for (int i = 0; i < count; i++)
         {
             ListBox2.Items.Add(listBox1.Items[i]);
          }
    }
    (You can use combo box instead of list box. Then replace listbox with combobox) 

    • If you want to copy a selected item from a list box to another list box, then you have to write code like this, 

    c# move copy selected item from list box to another list box
       private void button2_Click(object sender, EventArgs e)
         {
             ListBox2.Items.Add(listBox1.SelectedItem.ToString());
         }
    (You can use combo box instead of list box. Then replace ListBox with ComboBox)

    • If you want to copy selected items from a combo box to another list box by button click, then you have to write code like this, 
    c# move copy item from combo box text to another list box
      private void button4_Click(object sender, EventArgs e)
         {
             listBox2.Items.Add(comboBox1.Text);
         }
    Here selected item will move to another listbox only when you press the button ‘Button4’ .

    • Also you can copy an item from combo box to list box when selecting an item; instant updation. Here no need to click on a button. Here code will write in ‘SelectedIndexChanged’ event.To type code in this event, just double click on combo box or go to event list of combo box and double click on ‘SelectedIndexChanged’. 
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
            {
                   listBox2.Items.Add(comboBox1.Text);
            }
     Clear control elements or data

    How to clear a text box:
    TextBox1.Text = “”;
    How to clear all items from a list box:
    listBox1.Items.Clear();
    How to clear all items from a combo box:
    comboBox2.Items.Clear();
    [Write all above codes in any event like button click].

    More C# programs here




    No comments:

    Post a Comment