I have a list of Strings, List<String>.
I want to be able to open a form, showing the contents of this list, and allow the user to add, edit, and remove items from the list during run time.
I've been looking at ListView, but it isn't clicking for me. I'm not sure if that's because it isn't the right solution or that I don't get it.
What is the proper solution for what I want to do?
Chuck
You can use a list view and a context menu for your target:
try this code:
List<string> listofstring = new List<string>() {"A","B","C" };
private void Form1_Load(object sender, EventArgs e)
{
FillLstView();
}
private void Additem_Click(object sender, EventArgs e)
{
listofstring.Add("New Item");
FillLstView();
}
private void RemoveItem_Click(object sender, EventArgs e)
{
listofstring.RemoveAt(lstview.FocusedItem.Index);
EditItem.Enabled = false;
RemoveItem.Enabled = false;
FillLstView();
}
private void lstview_SelectedIndexChanged(object sender, EventArgs e)
{
RemoveItem.Enabled = true;
EditItem.Enabled = true;
}
private void EditItem_Click(object sender, EventArgs e)
{
string input = Microsoft.VisualBasic.Interaction.InputBox("Enter Edit", "Title", "Edited", 0, 0);
if (input != "")
{
listofstring[lstview.FocusedItem.Index] = input;
EditItem.Enabled = false;
RemoveItem.Enabled = false;
FillLstView();
}
}
private void FillLstView()
{
lstview.Clear();
foreach (var item in listofstring)
{
lstview.Items.Add(item);
}
}
Result
Download Project
Related
Now solved. Thanks for your answers!
This is my code right now:
//Listbox scripts is the name of my folder
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + #"Listbox scripts"))
{
string file2 = file.Split('\\').Last();
listBox1.Items.Add(file2);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
webBrowser1.Document.InvokeScript("SetText", new object[]
{
File.ReadAllText(string.Format("./Listbox scripts/{0}", listBox1.SelectedItem.ToString()))
});
}
I'm new to coding in C# and I have a textbox that has the names of text files in a directory and when I click on the text file in the listbox it's supposed to load the text from it into my textbox (named 'ScriptBox')
Here's my code:
private void Form1_Load(object sender, EventArgs e)
{
string User = System.Environment.MachineName;
textBox1.Text = "{CONSOLE} Welcome to Linst, " + User + "!";
directory = new DirectoryInfo(AppDomain.CurrentDomain.BaseDirectory + #"Scripts");
files = directory.GetFiles("*.txt");
foreach (FileInfo file in files)
{
listBox1.Items.Add(file.Name);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName); //these parts are the parts that dont work
}
Thanks in advance!
Add the below into your Form1.cs. What this is going to do is when a user clicks a listbox item, its going to call (raise an event) the "listBox1_MouseClick" method and set the text of the textbox to the text of the listbox item. I just quickly created an app and implemented the below and it works.
private void listBox1_MouseClick(object sender, MouseEventArgs e)
{
textBox1.Text = listBox1.Text;
}
And add the below to the Form1.Designer.cs where the rest of your list box properties are. The below is subscribing to an event, the listBox1_MouseClick method in Form1.cs, so when a user clicks on a listbox item, the listBox1_MouseClick method is going to run.
this.listBox1.MouseClick += new MouseEventHandler(this.listBox1_MouseClick);
I hope the above makes sense.
Your code is nice, and perfect but it just need a little validation check in list index selection
Try thing in your listbox_selectedIndexChanged
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex!=-1)
{
FileInfo selectedFile = files[listBox1.SelectedIndex];
ScriptBox.Text = File.ReadAllText(selectedFile.FullName);
}
}
I actually don't see a problem with your code, could it be a typo somewhere?
I did this and it worked for me:
private void Form1_Load(object sender, EventArgs e)
{
foreach (var file in System.IO.Directory.GetFiles(#"c:\"))
{
listBox1.Items.Add(file);
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
textBox1.Text = System.IO.File.ReadAllText(listBox1.SelectedItem.ToString());
}
}
So, I have a simple ListView that users can add information to and a delete button that is only capable of deleting one selected item at a time. I'm trying to make it so that when multiple items are selected and 'delete' is pressed it deletes those selected items instead of just one. Your help is appreciated!
Add recipient code:
private void addtoRecipients_Click(object sender, EventArgs e)
{
if (recipientEmailBox.Text != "")
{
string[] S = new string[4];
S[0] = recipientEmailBox.Text;
S[1] = recipientNameBox.Text;
S[2] = txtLocation.Text;
S[3] = txtSubject.Text;
ListViewItem I = new ListViewItem(S);
recipientBox.Items.Add(I);
UpdateNoOfEmails();
}
}
My Delete Button Code (only deletes one selection at the moment)
private void deleteEntryBTN_Click(object sender, EventArgs e)
{
try { recipientBox.Items.Remove(recipientBox.SelectedItems[0]); }
catch { }
UpdateNoOfEmails();
}
Clear All Recipients Code
private void clearBTN_Click(object sender, EventArgs e)
{
recipientBox.Items.Clear();
UpdateNoOfEmails();
}
In my case, the simplest way to do it was with a while loop. This is what my new delete button code looks like:
private void deleteEntryBTN_Click(object sender, EventArgs e)
{
try
{
while (recipientBox.SelectedItems.Count > 0)
{
recipientBox.Items.Remove(recipientBox.SelectedItems[0]);
}
}
catch { }
UpdateNoOfEmails();
}
I have a listbox in C# and want it to refresh after I added a new item(which gets opened with a new form dialog)
Here is my code which doesn't work.
private void showAllItems()
{
itemList = Db.getAllItems();
lb_itemList.DataSource = itemList;
}
private void showItemPreview(object sender, EventArgs e)
{
string curItem = lb_itemList.SelectedItem.ToString();
briefPreviewList = Db.getItemBriefPreview(curItem);
string itemInfos = string.Join(",", briefPreviewList.ToArray());
string[] infos = itemInfos.Split(',');
l_itemDB.Text = curItem;
l_CategoryDB.Text = infos[0];
}
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
CurrencyManager cm = (CurrencyManager)BindingContext[itemList];
cm.Refresh();
}
I assume when you insert a new item it gets stored into the database, if this is the case then all you need to do is reset the datasource:
private void b_addItem_Click(object sender, EventArgs e)
{
int uid = 1;
AddItem addItemForm = new AddItem(uid);
addItemForm.ShowDialog();
addItemForm.Dispose();
this.showAllItems();
}
nothing happen and nothing change when I use datagridview and SubmitChanges()
private void Form1_Load(object sender, EventArgs e)
{
this.book_infoDataGridView.DataSource = bookstore.book_info;
BindingSource bds=new BindingSource();
bds.DataSource = this.bookstore.book_info;
this.book_infoBindingSource = bds;
this.book_infobindingNavigator.BindingSource = bds;
}
private void save_Click(object sender, EventArgs e)
{
this.book_infoBindingSource.EndEdit();
bookstore.SubmitChanges();
}
I believe you're trying to .SubmitChanges() from your data in the DataGridView
private void save_Click(object sender, EventArgs e)
{
using (var bookstoreContext = new yourContext())
{
var b = (BookClass)bds.Current;
var book = bookstoreContext.Products.Single(c => c.BookId == b.BookId); //or .First()
book.BookName = b.BookName;
book.BookInfo = b.BookInfo;
bookstoreContext.SubmitChanges();
}
}
I have a TextBox in my winforms. When a user starts typing in it I will like to set the AcceptButton property to call another function. However it is calling another function which is called by a Button in my ToolStrip. To elaborate, here is my code below:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
if (locNameTxtBx.Text != "")
{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
else
{
MessageBox.Show("Please Enter Location");
}
}
searchBtn is a Button in the ToolStrip. So, when I try to run this code, I get this error
Cannot implicitly convert type 'System.Windows.Forms.ToolStripButton' to 'System.Windows.Forms.IButtonControl'. An explicit conversion exists (are you missing a cast?)
I have tried casting it as a ToolstripButton like this:
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = (ToolStripButton)searchBtn;
}
You could use two delegates and set the delegate to use in the locNameTxtBx_TextChanged method.
private delegate void ToUseDelegate();
ToUseDelegate delegateIfNoText = delegate{
MessageBox.Show("Please Enter Location");
}
ToUseDelegate delegateIfText = delegate{
List<SearchLocation> locationsArray = new List<SearchLocation>();
var location = locNameTxtBx.Text;
SearchLocation loc = new SearchLocation();
loc.Where = location;
locationsArray.Add(loc);
mapArea.VE_FindLocations(locationsArray, true, true, null);
mapArea.VE_SetZoomLevel(14);
}
ToUseDelegate delToUse = delegateIfNoText;
private void locNameTxtBx_TextChanged(object sender, EventArgs e)
{
this.AcceptButton = searchBtn;
if (locNameTxtBx.Text != ""){
delegateToUse = delegateIfNoText;
} else {
delegateToUse = delegateIfText;
}
}
private void searchBtn_Click_1(object sender, EventArgs e)
{
delegateToUse();
}