ok, I have a listBox where I can type something in a textBox and add it to the listBox.
But when I have added it, how do I make a undo button?
And one more question. if i type something new, it starts on a new line. is it possible to avoid that in any way?
Loading
VulpesPosted Dec 10, 2011, 4:24 PM
private void btnundo_Click(object sender, EventArgs e)
{
if (listBox1.Items.Count > 0)
{
listBox1.Items.RemoveAt(listBox1.Items.Count - 1);
}
}
DavidPosted Dec 10, 2011, 3:07 PM
Satyapriya NayakPosted Dec 10, 2011, 1:32 PM
Try this...
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnadd_Click(object sender, EventArgs e)
{
listBox1.SelectedIndex = listBox1.Items.Add(textBox1.Text);
textBox1.Clear();
}
private void btnundo_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndices.Count > 0)
{
for (int i = listBox1.SelectedIndices.Count - 1; i >= 0; i--)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndices[i]);
}
}
}
}
}
Thanks
If this post helps you mark it as answer