I have a CheckedListBox with items from a database.
When I check an item in the CheckedListBox and after that I close the form and open the form again, the item is not checked any more, i.e. the "check" has not been saved.
How can I accomplish that if I check an item and then close the form and open it again, that the item is still checked?
I tried this:
void deliveries_FormClosing(object sender, FormClosingEventArgs e)
{
for (int i = 0; i < deliveries.ClbOrdersCheckDelivery.Items.Count; i++)
{
if (deliveries.ClbOrdersCheckDelivery.GetItemChecked(i) == true)
{
Properties.Settings.Default.CheckedItems = deliveries.ClbOrdersCheckDelivery.GetItemChecked(i);
}
}
}
]\
You need to write
Properties.Settings.Default.Save();
To save the settings.
Write it after the for loop.
EDIT
I tried the following code to save all checked items to settings file. It works. Please check.
private void button1_Click(object sender, EventArgs e)
{
Properties.Settings.Default.CheckedItems = string.Empty;
foreach (var item in checkedListBox1.CheckedItems)
{
Properties.Settings.Default.CheckedItems += item + "," ;
}
Properties.Settings.Default.Save();
}
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(Properties.Settings.Default.CheckedItems);
}
private void Form1_Load(object sender, EventArgs e)
{
var checkedItems = Properties.Settings.Default.CheckedItems.ToString().Split(',');
foreach (var item in checkedItems)
{
var index=checkedListBox1.FindString(item);
if(index>=0)
{
checkedListBox1.SetItemChecked(index, true);
}
}
}
Related
private void button1_Click(object sender, EventArgs e)
{
listBox1.Items.Add(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
string val = listBox1.Text.Trim();
if (listBox1.Items.Contains(val)) {
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
else
{
MessageBox.Show("There is no items present");
}
}
elements are entered from text box to list box, If entered the same data,. how to check? or msg box should display and
while deleting items from the list box if there is no items how to i get to know.
You can check if the value entered in the textbox is already in the listbox or not:
bool listContainsItem = Listbox.Items.Any(item => item.Value == textboxValue);
if(listContainsItem)
{
// ... item is in listbox, do your magic
}
else
{
// ... item is not in listbox, do some other magic
}
You can do this in the Onchange event of your textbox, or when clicking a button, ... give us more context so we can provide you a better solution.
You can use a HashSet as data source to make sure your list contains unique elements.
In example :
HashSet<string> ListBoxSource = new HashSet<string>();
private void button2_Click(object sender, EventArgs e)
{
string val = listBox1.Text.Trim();
// ListBoxSource.Add(val) Return true if val isn't present and perform the adding
if (ListBoxSource.Add(val))
{
// DataSource needs to be a IList or IListSource, hence the conversion to List
listBox1.DataSource = ListBoxSource.ToList();
}
else
{
MessageBox.Show("Item is already in list");
}
}
You may check for duplicated item by looping through each item in the list to be compared with name of item to be added when add button is clicked:
private void addBtn_Click(object sender, EventArgs e)
{
bool similarItem = false;
if (!String.IsNullOrEmpty(itemText.Text.Trim()))
{
foreach (string listItem in itemListBox.Items)
{
if (listItem == itemText.Text)
{
MessageBox.Show("Similar item detected");
similarItem = true;
break;
}
}
if(!similarItem)
itemListBox.Items.Add(itemText.Text);
}
}
To prompt user when delete button is clicked when there is no item, the selected index will be -1, u may use that as the condition to prompt user:
private void deleteBtn_Click(object sender, EventArgs e)
{
if (itemListBox.SelectedIndex > -1)
itemListBox.Items.RemoveAt(itemListBox.SelectedIndex);
else
MessageBox.Show("No item exist in the list box, operation fail");
}
I have a check list box that has 5 directories that my program will call later. I want the user to be able to check what directories they want to use with a checklistbox. So far they can chose each item they want and it will be added but i want unchecked boxs to be removed from the registry
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
foreach (object checkbox in checkedListBox1.CheckedItems)
{
textBox1.AppendText("Item is marked: " + checkbox.ToString() + "\n");
RegBUP.SetValue(checkbox);
}
}
And so people have a more general idea of what I am doing:
catchpath() returns the path from a directory, so if it gets desktop it returns the path to the desktop.
public static void SetValue(object title)
{
RegistryKey directs = regKey.OpenSubKey("Path to registry", true);
directs.SetValue(title.ToString(), catchpath(title), RegistryValueKind.String);
directs.Close();
}
// you can try this
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "";
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
if (checkedListBox1.GetItemChecked(i) == true)
{
textBox1.AppendText("Item is marked: " +checkedListBox1.Items[i].ToString()+ "\n");
RegBUP.SetValue(checkedListBox1.Items[i]);
}
else
{
RegBUP.DeleteValue(checkedListBox1.Items[i]);
}
}
}
I have been working on this project for a few days, it’s a C# Windows Visual Studio 2010 form and I have been posting different questions that relate to the same project; as I was told to post different questions instead on having them all in the same post. So this is the project: create a form with two ListBoxes—one contains at least four font names and the other contains at least four font sizes. Let the first item in each list be the default selection if the user fails to make a selection. Allow only one selection per ListBox. After the user clicks a button, display "Hello" in the selected font and size.
This time I’m having a problem getting the message in the textbox to display according to the font type and size that the user selected. Here is where I’m at in the coding:
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
this.listBox2.SelectedIndexChanged += new System.EventHandler(this.listBox2_SelectedIndexChanged);
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
textBox1.Text = listBox2.SelectedItem.ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Now I'm trying to elicit a call from a button clicked that will display the message "Hello" in the user’s choice of font and font size. Any suggestions would be greatly appreciated.
remove this method:
private void textBox1_TextChanged(object sender, EventArgs e)
{
textBox1.Text = "Hello!";
}
in the button_click event of your button, add this :
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
you might want to remove the selectedindexchanged methods in your code if you are going to use a button tho. depends on what you want.
edit:
public Form2()
{
InitializeComponent();
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox1.SelectedIndex = 0;
listBox2.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
if you just use the above code everything should work as you want it to. I tried it out myself and it's working fine for me
This was my final submission. Thanks for all of the advice guys.
public Form1()
{
InitializeComponent();
//populate listbox1
listBox1.Items.Add("Arial");
listBox1.Items.Add("Calibri");
listBox1.Items.Add("Times New Roman");
listBox1.Items.Add("Verdana");
listBox1.SelectedIndex = 0; // <--- set default selection for listBox1
//populate listbox2
listBox2.Items.Add("8");
listBox2.Items.Add("10");
listBox2.Items.Add("12");
listBox2.Items.Add("14");
listBox2.SelectedIndex = 0; // <--- set default selection for listBox2
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = "hello";
textBox1.Font = new Font(listBox1.SelectedItem.ToString(), Convert.ToInt32(listBox2.SelectedItem.ToString()));
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
I want to move items back and forth between a ComboBox and a ListBox using C# 2010 (form)
My code seems to work. However, when I move the items back to the ComboBox (from the ListBox) I have a space in between the items. If anyone has a suggestion on how to remove the space between the items in the ComboBox I would greatly appreciate it.
private void stateslistcomboBox_SelectedIndexChanged(object sender, EventArgs e)
{
stateslistBox.Items.Add(statescomboBox.SelectedItem);
statescomboBox.Items.RemoveAt(statescomboBox.SelectedIndex);
}
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
The statescomboBox.Items.Add(item); triggers Another SelectIndexChanged that adds an empty item.
Try
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
statescombobox.SelectIndexChanged -= stateslistBox_SelectedIndexChanged;
statescomboBox.Items.Add(item);
statescombobox.SelectIndexChanged += stateslistBox_SelectedIndexChanged;
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
alternatively, you can prevent empty items being added.
private void stateslistBox_SelectedIndexChanged(object sender, EventArgs e)
{
string item = "";
item = Convert.ToString(stateslistBox.SelectedItem);
if (!string.IsNullOrEmpty(item)
{
statescomboBox.Items.Add(item);
stateslistBox.Items.Remove(stateslistBox.SelectedItem);
}
}
I am trying to delete checked items in my list view my app. The app is simple it deletes temp files in the current user temp directory. When the app executes it loads all the temp files in the list view. I have check boxes enabled so that the user can check the items that he/she would like to delete
Thank you for your time.
Code:
private void button1_Click(object sender, EventArgs e)
{
if (listView1.CheckedItems.Count > 0)
{
foreach (var fName in Directory.GetFiles(tFile))
{
try
{
File.Delete(fName);
}
catch (Exception)
{
// Ignore the failure and continue
}
}
MessageBox.Show("Finished");
PaintListView(tFile);
}
else
{
MessageBox.Show("Please Check the files you want to delete");
}
}
private void selectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
item.Checked = true;
}
}
private void unselectAllToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (ListViewItem item in listView1.Items)
{
item.Checked = false;
}
}
private void listView1_ItemChecked(object sender, ItemCheckEventArgs e)
{
int c = listView1.CheckedItems.Count;
for (int i = 0; i < c; i++)
{
itemsChecked.Text = i.ToString();
}
//int listCount = listView1.CheckedItems.Count;
//itemsChecked.Text = listCount.ToString();
}
Place the code that you have commented inside a button Click event...for example,
private void btnSubmit_Click(object sender, EventArgs e)
{
int listCount = listView1.CheckedItems.Count;
itemsChecked.Text = listCount.ToString();
}
And then in this same event handler, inlcude the logic to delete the files that are checked by iterating through the listview contents.