I am creating a program using WinForms so users can input info into textboxes on one form which then are saved into a Listbox on another form. I would like to be able to edit the items saved in the listbox by opening the original form on a button click. Really struggling with it as I can't think of the code and I can't seem to find a solution.
My Code:
private void btnAdd_Click(object sender, EventArgs e)
{
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.ShowDialog();
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
listBoxRooms.Items.Add(newRoomDisplayForm.value);
}
newRoomDisplayForm.Close();
}
private void btnRemove_Click(object sender, EventArgs e)
{
this.listBoxRooms.Items.RemoveAt(this.listBoxRooms.SelectedIndex);
}
private void btnEdit_Click(object sender, EventArgs e)
{
}
So i've got a Add and Remove button which work perfectly just need a solution to the edit button.
Thanks in advance
I'm guessing newRoomDisplayForm.value is a property or a public member inside the form. You just need to do something like this:
private void btnEdit_Click(object sender, EventArgs e)
{
if(listBoxRooms.SelectedIndex < 0) return;
var tmpValue = listBoxRooms.Items[listBoxRooms.SelectedIndex].ToString();
RoomDisplayForm newRoomDisplayForm = new RoomDisplayForm();
newRoomDisplayForm.value = tmpValue;
newRoomDisplayForm.ShowDialog();
//TODO: inside "newRoomDisplayForm" set the value to the textbox
// ie.: myValueTextBox.Text = this.value;
if(newRoomDisplayForm.DialogResult == DialogResult.OK)
{
// replace the selected item with the new value
listBoxRooms.Items[listBoxRooms.SelectedIndex] = newRoomDisplayForm.value;
}
}
Hope it helps!
You can simply remove the listitem in that specific position, create a new item and add it again. it's kind of replacement.
Related
I am working on a simple application that to add/delete string/s into an array and show that in ListBox.
My code shows only the latest value that was typed into the textBox and
private void Add_Click(object sender, EventArgs e)
{
string add = textBox1.Text;
List<string> ls = new List<string>();
ls.Add(add);
String[] terms = ls.ToArray();
List.Items.Clear();
foreach (var item in terms)
{
List.Items.Add(item);
}
}
private void Delete_Click(object sender, EventArgs e)
{
}
This code makes no sense. You are adding one single item to a list, then convert it to an array (still containg one item) and finally loop through this array, which of course adds one item to the previously cleared listbox. Therefore your listbox will always contain one single item. Why not simply add the item directly?
private void Add_Click(object sender, EventArgs e)
{
List.Items.Add(textBox1.Text);
}
private void Delete_Click(object sender, EventArgs e)
{
List.Items.Clear();
}
Also clear the listbox in Delete_Click instead of Add_Click.
If you prefer to keep the items in a separate collection, use a List<string>, and assign it to the DataSource property of the listbox.
Whenever you want the listbox to be updated, assign it null, then re-assign the list.
private List<string> ls = new List<string>();
private void Add_Click(object sender, EventArgs e)
{
string add = textBox1.Text;
// Avoid adding same item twice
if (!ls.Contains(add)) {
ls.Add(add);
RefreshListBox();
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Delete the selected items.
// Delete in reverse order, otherwise the indices of not yet deleted items will change
// and not reflect the indices returned by SelectedIndices collection anymore.
for (int i = List.SelectedIndices.Count - 1; i >= 0; i--) {
ls.RemoveAt(List.SelectedIndices[i]);
}
RefreshListBox();
}
private void RefreshListBox()
{
List.DataSource = null;
List.DataSource = ls;
}
The problem with code is quite simple. Instead of adding new item to list your code creates new list with one added item only. I am trying to interpret functions of program and they seem to be:
Enter new text into top level text box.
If Add button is clicked your item goes on top of the list (if it's bottom see end of my answer).
If item(s) is selected in list and Delete is clicked selected item(s) is/are deleted.
To achieve this you should first insert text on top of the list by using Add_Click code, than delete selected items using Delete_Click code. There is additional code to guard against inserting empty or white space only strings plus trimming of leading and trailing white space.
private void Add_Click(object sender, EventArgs e)
{
// Since you do not want to add empty or null
// strings check for it and skip adding if check fails
if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text)
{
// Good habit is to remove trailing and leading
// white space what Trim() method does
List.Items.Insert(0, textBox1.Text.Trim());
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Get all selected items indices first
var selectedIndices = List.SelectedIndices;
// Remove every selected item using it's index
foreach(int i in selectedIndices)
List.Items.RemoveAt(i);
}
To complete adding and deleting logic I would add Delete All button which would just call List.Items.Clear(). If you prefer to add text at the end just use Add method form #Olivier Jacot-Descombes answer.
You can use in C#:
private void Delete_Click(object sender, EventArgs e)
{
if(myList.Contains(textbox1.value))//if your list containt the delete value
{
myList.Remove(textbox1.value); //delete this value
}
else
{
//the list not containt this value
}
}
and you can use the same method for validate if a value exist when try to add
private void AddItem()
{
if (!String.IsNullEmptyOrWhiteSpace(textBox1.Text))
{
var newItem = textBox1.Text.Trim();
if (!List.Items.Contains(newItem))
{
List.Items.Add(newItem);
// Alternative if you want the item at the top of the list instead of the bottom
//List.Items.Insert(0, newItem);
//Prepare to enter another item
textBox1.Text = String.Empty;
textBox1.Focus();
}
}
}
private void Add_Click(object sender, EventArgs e)
{
AddItem();
}
Private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
AddItem();
}
}
private void Delete_Click(object sender, EventArgs e)
{
// Remove every selected item using it's index
foreach(var item in List.SelectedItems)
{
List.Items.Remove(item);
}
}
everybody!
I have collection of items in combobox's properties. And I want to add new item in my combobox by writing text in combobox and then use button event:
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}
But in next time of starting my programm - my added item doesn't exist in combobox. What do I wrong? I need all added items have been saved in my combobox for ever. May be problem in method InitializeComponents()? May be I have to add event before it?
Thank you very much.
ComboBox has no functionality to save and reload items.
You may store items into .NET Settings file on closing window and reload them on loading form:
private void Form1_Load(object sender, EventArgs e)
{
if (Settings.Default.cboCollection != null)
this.cbx_unix_dir.Items.AddRange(Settings.Default.cboCollection.ToArray());
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
ArrayList arraylist = new ArrayList(this.cbx_unix_dir.Items);
Settings.Default.cboCollection = arraylist;
Settings.Default.Save();
}
//A button to add items to the ComboBox
private void button2_Click_1(object sender, EventArgs e)
{
cbx_unix_dir.Items.Add(cbx_unix_dir.Text);
}
I have a form that loads 3 pre-defined scores in a list box. I want to convert a selected score into a string, and then output that string in a textbox. So far i think i've converted the item to a string, and tried setting it to the textbox but it doesn't seem to be working.
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
if (this.lstStudents.SelectedIndex >= 0)
{
string a = lstStudents.Items.Cast<string>().ToString();
txtDisplay.Text = a;
}
btnUpdate.Enabled = false;
Assuming your question is about Windows Forms, One way to get the selected item is to use code like this:
txtDisplay.Text =lstStudents.SelectedItem.ToString();
It is common to want to get the selected item that the user has selected, to do this, you need to place the above code in an event to look like this for example:
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = this.lstStudents.SelectedItem.ToString();
}
An event can be wired to the control either by code or via the VS IDE, you can't just copy and paste the above code. Ask me if you don't know how to do that.
If you want to grab the first item only, then Plutonix comment above applies. You don't need the IF statement.
Since this is the process at load time, why not try just :
private void frmStudentScores_Load(object sender, EventArgs e)
{
lstStudents.Items.Clear();
lstStudents.Items.Add("Joe Smith|93|92|98");
lstStudents.Items.Add("Mike Jones|56|61|33");
lstStudents.Items.Add("Rita Jackson|100|89|96");
lstStudents.SelectedIndex = 0;
txtDisplay.Text = lstStudents.Items[0].ToString();
btnUpdate.Enabled = false;
EDIT
then add at the listbox's event SelectedIndexChanged :
private void lstStudents_SelectedIndexChanged(object sender, EventArgs e)
{
txtDisplay.Text = lstStudents.Items[lstStudents.SelectedIndex].ToString();
}
When I save a new item on datagridview the statement
MessageBox.Show(this.tb_aprovacao_admissaoDataGridView.CurrentRow.Cells[0].Value.ToString());
shows the value -1. How can I change it to show the real number of ID?
Thanks to everyone.
Depends how you want to get the value..
Do you want to get the value after you click on the cell, or on the click of a button or?
If you want to do it on the cell click event, you can do it like this:
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
{
MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
}
}
To get it with a button:
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
}
I am trying to do something like: when a user select an item on a listbox, then the function listboxClicked will be triggered. However, the first click often does not able to trigger the function. It only triggers the function when I click the same item or another item for the second time.
May I know what's wrong with my code? Thank you.
My Code:
private void listbox_SelectedIndexChanged(object sender, EventArgs e)
{
listbox.MouseClick += listboxClicked;
}
private void listboxClicked(object sender, EventArgs e)
{
if (listbox.SelectedIndex != -1)
{
//do something
}
}
Try this one:
Listbox1_SelectedValueChanged(object sender, EventArgs e)
{
Listbox listbox = (Listbox)sender;
MessageBox.Show(listbox.SelectedItem.ToString());
}