Here is Another User Control
A pick to of The Control:
This is For Left/Right Side Buttons Docking
Add Button Coding
A pick to of The Control:
As you can see this one contains a Listbox and 7 buttons for the Add,Remove,up,down,,RemoveAll,Top,Bottom etc
1. Add a user control to project. Add a list box and 7 button as show in the figure.
Coding:
Properties
This is For Left/Right Side Buttons Docking
public bool _blnLeftSideButtons = false;
public bool LeftSideButtons
{
get { return _blnLeftSideButtons; }
set {
_blnLeftSideButtons = value;
if (_blnLeftSideButtons)
{
lstList.Dock = DockStyle.None;
flowLayoutPanel1.Dock = DockStyle.Left;
lstList.Dock = DockStyle.Fill;
Invalidate();
}
else
{
lstList.Dock = DockStyle.None;
flowLayoutPanel1.Dock = DockStyle.Right;
lstList.Dock = DockStyle.Fill;
Invalidate();
}
}
}
Add Button Coding
private void btnAdd_Click(object sender, EventArgs e)
{
ofD = new OpenFileDialog();
ofD.Multiselect = true;
ofD.Filter = "Image Files(*.gif,*.jpg,*.jpeg,*.bmp,*.png,*.wmf)|*.gif;*.jpg;*.jpeg;*.bmp;*.png;*.wmf";
if (ofD.ShowDialog() == DialogResult.OK)
{
if (ofD.FileNames.Length > 0)
{
for (int i = 0; i < ofD.FileNames.Length; i++)
{
lstList.Items.Add(Path.GetFileName( ofD.FileNames[i].ToString()));
}
}
}
}
Remove Button Coding
//Remove item
private void btnRemove_Click(object sender, EventArgs e)
{
if (lstList.Items.Count > 0 && lstList.SelectedIndex > 0)
{
lstList.Items.RemoveAt(lstList.SelectedIndex);
}
}
Move An Item Up
// item Up
private void btnUp_Click(object sender, EventArgs e)
{
try
{
if (lstList.SelectedItems.Count == 0) { return; }
int index = lstList.Items.IndexOf(lstList.SelectedItems[0]);
if ((index - 1) < 0) { return; }
object[] obj = new object[lstList.SelectedItems.Count];
for (int i = 0; i < lstList.SelectedItems.Count; i++)
{
obj[i] = lstList.SelectedItems[i];
}
lstList.SelectedIndices.Clear();
for (int j = 0; j < obj.Length; j++)
{
int temp_I = lstList.Items.IndexOf(obj[j]);
lstList.Items.RemoveAt(temp_I);
lstList.Items.Insert(temp_I - 1, obj[j]);
lstList.SelectedIndices.Add(temp_I - 1);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
Move An Item Down
//item down
private void btnDown_Click(object sender, EventArgs e)
{
try
{
if (lstList.SelectedItems.Count == 0) { return; }
int index = lstList.Items.IndexOf(lstList.SelectedItems[0]);
if (index == lstList.Items.Count - 1) { return; }
if ((index + 1) > lstList.Items.Count) { return; }
object[] obj = new object[lstList.SelectedItems.Count];
for (int i = 0; i < lstList.SelectedItems.Count; i++)
{
obj[i] = lstList.SelectedItems[i];
}
lstList.SelectedIndices.Clear();
for (int j = 0; j < obj.Length; j++)
{
int temp_J = lstList.Items.IndexOf(obj[j]);
lstList.Items.RemoveAt(temp_J);
lstList.Items.Insert(temp_J + 1, obj[j]);
lstList.SelectedIndices.Add(temp_J + 1);
}
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
Move An Item To Top
//Top
private void btnTop_Click(object sender, EventArgs e)
{
try
{
if (lstList.Items.Count == 0 ) { return; }
int index = lstList.SelectedIndices[0];
object obj = new object();
obj = lstList.SelectedItems[0];
lstList.Items.RemoveAt(index);
lstList.Items.Insert(0, obj);
lstList.SelectedIndices.Clear();
lstList.SelectedIndices.Add(0);
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
Move item to Bottom
//Bottom
private void btnBottom_Click(object sender, EventArgs e)
{
try
{
if (lstList.Items.Count == 0) { return; }
int index = lstList.SelectedIndices[0];
object obj = new object();
obj = lstList.SelectedItems[0];
lstList.Items.RemoveAt(index);
lstList.Items.Insert(lstList.Items.Count , obj);
lstList.SelectedIndices.Clear();
lstList.SelectedIndices.Add(lstList.Items.Count - 1);
}
catch (Exception x)
{
MessageBox.Show(x.Message);
}
}
Remove All Items:
//Remove All
private void btnRemoveAll_Click(object sender, EventArgs e)
{
if (lstList.Items.Count > 0)
{
if (MessageBox.Show("This Will Delete All The Items", "Are You Sure?", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
lstList.Items.Clear();
}
}
}
For the Source code mail me at: sandeepparekh9@gmail.com

0 comments:
Post a Comment