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]);
}
}
}
Related
I am able to add accounts of different types through another form to the list statements which I can then add to the list box lstaccounts. However, when I select an account in my list box and press the view statement button I get that exception on the 'lblBank.Text = ShowStatement((IStatement)lstAccounts.SelectedItem);' line... any idea why this could be happening?
public List<IStatement> statements;
private int accountCounter = 0;
private int statementsViewed = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
statements = new List<IStatement>();
}
private void btnView_Click(object sender, EventArgs e)
{
if (lstAccounts.SelectedIndex > -1)
{
lblBank.Text = ShowStatement((IStatement)lstAccounts.SelectedItem);
statementsViewed += 1;
lblTotal.Text = statementsViewed.ToString();
}
}
private string ShowStatement(IStatement item)
{
String msg = "Account Number: " + item.AccountNumber + "\n"
+ "Name: " + item.AccountName + "\n" + item.PrintStatement();
return msg;
}
private void btnAddAccount_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
if(form2.ShowDialog() == DialogResult.OK)
{
if (form2.txtValue.Text == "" && form2.txtMinMonthly.Text == "")
{
BankAccount b = form2.GetBankAccount();
statements.Add(b);
}
else if (form2.txtMinMonthly.Text == "")
{
InsurancePolicy i = form2.GetInsurancePolicy();
statements.Add(i);
}
else
{
PlatinumCurrent p = form2.GetPlatinumCurrent();
statements.Add(p);
}
foreach (IStatement os in statements)
{
lstAccounts.Items.Add(os.ToString());
accountCounter += 1;
}
}
}
The exact error I'm getting is: System.InvalidCastException: 'Unable to cast object of type 'System.String' to type 'TelephoneBanking.IStatement'.'
You added it as .ToString() in your btnAddAccount_Click. Try add the item, not the item.ToString().
private void btnAddAccount_Click(object sender, EventArgs e)
{
...
foreach (IStatement os in statements)
{
lstAccounts.Items.Add(os); // ###### removed os.ToString() ######
accountCounter += 1;
}
...
I'm making a windows form, in it I've read some text files, I've got a combobox and what I want is when I select the option "Games" in the combobox is for the richtextbox2 to be filled with the text that is in the read file "game.txt".
And the same for the other options and text files. I've tried this if statement but I get an error stating that "I cannot convert string to bool".
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void richTextBox2_TextChanged(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
//Fill combobox with names.
comboBox1.Items.Add("Games");
comboBox1.Items.Add("Operating");
comboBox1.Items.Add("Information");
try
{
//read text from text files.
string game = File.ReadAllText(#"game.txt");
string operate = File.ReadAllText(#"operate.txt");
string information = File.ReadAllText(#"info.txt");
//if you select Games option in combobox, fill text box with text from read file "game.txt".
if (comboBox1.Text = "Games")
{
richTextBox2.Text = game;
}
}
catch (Exception ex)
{
//display error if files not found.
MessageBox.Show(" " + ex.Message);
}
In C# to check if one value is equal to another you need to use == operator.
So your if statement should look like:
if (comboBox1.Text == "Games")
EDIT:
Above will make your code compile. In order to make your program works as expected you need to move your try/catch block to the SelectedChangedIndex of your ComboBox, it can look like this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
//if you select Games option in combobox, fill text box with text from read file "game.txt".
if (comboBox1.Text == "Games")
{
richTextBox2.Text = File.ReadAllText(#"game.txt");
}
else if (comboBox1.Text == "Operate")
{
richTextBox2.Text = File.ReadAllText(#"operate.txt");
}
else if (comboBox1.Text == "Information")
{
richTextBox2.Text = File.ReadAllText(#"info.txt");
}
}
catch (Exception ex)
{
//display error if files not found.
MessageBox.Show(" " + ex.Message);
}
}
//Fill the default comboBox item.
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.Items.Add("Games");
comboBox1.Items.Add("Operating");
comboBox1.Items.Add("Information");
}
public void Write(string category)
{
switch (category)
{
//if you select Games option in combobox, do work Games() method.
case "Games": Games(); break;
case "Operating": Operating(); break;
case "Information": Information(); break;
default:
break;
}
}
//if you select Games , read file "game.txt" with together Read() method.
public void Games()
{
richTextBox1.Text = Read("games");
}
//if you select Operating , read file "operating.txt" with together Read() method.
public void Operating()
{
richTextBox1.Text = Read("operating");
}
//if you select Information , read file "information.txt" with together Read() method.
public void Information()
{
richTextBox1.Text = Read("information");
}
//File reading process
public string Read(string fileName)
{
string data = File.ReadAllText(Application.StartupPath + #"\" + fileName + ".txt");
return data;
}
//Selected item process
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Write(comboBox1.SelectedItem.ToString());
}
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);
}
}
}
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.
I have a program which is able to retrieve various registry values using C# codes which was compiled and created using VS 2010.
However the problem arises when I tried to display the results retrieved from the Windows Registry into a rich text box within a form.
The form only shows 1 line which is the last value in the Array that contains the results.
Please do give some advice on the codes. Thanks!
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;
using Microsoft.Win32;
namespace Syscrawl
{
public partial class FTK_Menu_Browsing_History : Form
{
public FTK_Menu_Browsing_History()
{
InitializeComponent();
}
private void buttonFileHistory_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_File_History mfh = new FTK_Menu_File_History();
mfh.ShowDialog();
this.Close();
}
private void buttonEncryptedFiles_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Encrypted_Files mef = new FTK_Menu_Encrypted_Files();
mef.ShowDialog();
this.Close();
}
private void buttonRecentlyAccessedFiles_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Recently_Accessed_Files mraf = new FTK_Menu_Recently_Accessed_Files();
mraf.ShowDialog();
this.Close();
}
private void buttonRegistryHistory_Click(object sender, EventArgs e)
{
this.Hide();
FTK_Menu_Registry_History mrh = new FTK_Menu_Registry_History();
mrh.ShowDialog();
this.Close();
}
private void buttonMainMenu_Click(object sender, EventArgs e)
{
this.Hide();
Menu m = new Menu();
m.ShowDialog();
this.Close();
}
private void buttonLogOut_Click(object sender, EventArgs e)
{
this.Hide();
Syscrawl_Login sl = new Syscrawl_Login();
sl.ShowDialog();
this.Close();
}
private void FTK_Menu_Browsing_History_Load(object sender, EventArgs e)
{
try
{
RegistryKey rk = Registry.CurrentUser;
rk = rk.OpenSubKey("Software\\Microsoft\\Internet Explorer\\TypedURLs",
false);
PrintKeys(rk);
rk.Close();
}
catch (Exception MyError)
{
richTextBoxBrowsing.Text="An error has occurred: " + MyError.Message;
}
}
void PrintKeys(RegistryKey rk)
{
if (rk == null)
{
richTextBoxBrowsing.Text="Couldn't open the desired subkey.";
return;
}
richTextBoxBrowsing.Text = "Subkeys of " + rk.Name;
try
{
string[] valnames = rk.GetValueNames();
int i = 0;
foreach (string s in valnames)
{
string val = (string)rk.GetValue(valnames[i++]);
richTextBoxBrowsing.Text="-----------------------------------------------";
richTextBoxBrowsing.Text=s + " contains " + val;
}
}
catch (Exception MyError)
{
richTextBoxBrowsing.Text = "An errors has occurred: " + MyError.Message;
}
}
private void richTextBoxBrowsing_TextChanged(object sender, EventArgs e)
{
}
}
}
By saying:
richTextBoxBrowsing.Text=
in each iteration of your loop, you keep on overwriting the text. So only the last call to the Text property gets printed.
You need to set the richTextBoxBrowsing.TextMode property to multiline, and then instead call:
richTextBoxBrowsing.AppendText(s + " contains " + val + "\n");
Oh, and by the way, use: string val = rk.GetValue(s).ToString();
so you can remove the int i = 0; declaration
You should use richTextBoxBrowsing.AppendText(...)
You're changing the entire contents with each call, instead of appending to it:
richTextBoxBrowsing.Text=s + " contains " + val;
should be
richTextBoxBrowsing.AppendText(s+" contains " + val+Environment.NewLine);
Confirm that you set
richTextBoxBrowsing.TextMode to
MultiLine
Change richTextBoxBrowsing.Text+=s + " contains " + val; in foreach loop
Use Debug.WriteLine to debug return
value
I think your loop is not correct.
In _void PrintKeys()_ your Loop overwerites your RichTextBox.Text-Value try something like:
String temp = "-----------------------------------------------";
foreach (string s in valnames)
{
String val = (String)rk.GetValue(valnames[i++]);
temp=temp+s + " contains " + val;
}
RichTextBox.text = temp;