I have a complex problem and all I find don't help me. So I have a WindowsFormApp
and in Form2
I have a CheckedListBox
who is like this :
|_|Simulink
|_|Aerospace Blockset
|_|Bioinformatics
and many others rows.
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
I want, when I check one of this checkBoxes
to get the name: Simulink
, for exemple, if Simulink
is checked or Aer...
and verify in a text file instaler.ini
who is here :
string installerfilename = Path + "installer.ini";
var link = (Path + "installer.ini").ToString();
var lines = File.ReadAllLines(link);
where I have that name: Simulink
after #product=
, like this : #product=Simulink
and there delete #
, and add a #
where I have product=all
=> #product=all
(this is for all cases ). You can help me to create a loop for all checkboxes
, because all I tried was to make an event for checkboxes
but is to complicated, and in that mode I have a lot of code ?
Use Regex.Replace and string.replace to update your product text files.
private void chklbproduct_SelectedIndexChanged(object sender, EventArgs e)
{
string installerfilename = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory, "Installer.txt");
IEnumerable<string> inilines = File.ReadAllLines(installerfilename).AsEnumerable();
string selectedItem = chklbproduct.SelectedItem.ToString();
bool IsChecked = chklbproduct.CheckedItems.Contains(selectedItem);
if (IsChecked)
inilines = inilines.Select(line => line == string.Format("#product={0}", selectedItem)
? line.Replace(line, string.Format("#product={0}", selectedItem), string.Format(@"product={0}", selectedItem))
: line);
else
inilines = inilines.Select(line => (line == string.Format("#product={0}", selectedItem) || line == string.Format(@"product={0}", selectedItem))
? line.Replace(line, string.Format(@".*product={0}", selectedItem), string.Format(@"#product={0}", selectedItem))
: line);
if (chklbproduct.CheckedItems.Count == 0)
inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"product=all"));
else
inilines = inilines.Select(line => Regex.Replace(line, @".*product=all", @"#product=all"));
string strWrite = string.Join(Environment.NewLine, inilines.ToArray());
File.WriteAllText(installerfilename, strWrite);
}
Assign each checkBox a name by clicking on it in the designer and then filling in the name property in the properties pane. You can now easily reference the checkboxes in code behind
Now in code behind you can create a list from all checkboxes yourself. Be sure to do this after calling the InitializeComponent
method in the constructor.
public class MyForm
{
List<CheckBox> checkboxes;
public MyForm()
{
// Call InitializeComponent first
// or all the checkboxes will be null
InitializeComponent();
checkboxes = new List<CheckBox>();
checkboxes.add(myCheckBox1);
checkboxes.add(myCheckBox2);
// etc...
}
}
You can now loop over your checkboxes and perform the necessary actions, for example when a button is clicked:
public void Button1_Clicked(...)
{
// Loop over all checkboxes that are checked
foreach(var checkbox in checkboxes.Where(x => x.Checked))
{
switch(checkbox.Name)
{
case "myCheckBox1":
// MyCheckBox1 is checked, do something
break;
case "myCheckBox2":
// MyCheckBox2 is checked, do something
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.