Saving checkbox information to TXT files

1

I'm developing an O.S. software where I've broken down the main problems in several checkboxes. All data entered on the screen will be sent to a text file, which will then be viewed by the boss in Excel.

My problem is that I'm not able to capture the information of only the checkboxes that have been marked.

My question is: How to capture the information regarding only the checkboxes that were marked and play in a text file?

PS: To identify the checkbox, next to each one I added a Label with the description of the problem that the checkbox represents, I do not know if it is the correct way. I added a code that I created from a Net tutorial, I need to capture the information about each checkbox that is marked

private void BtSalvar_Click(object sender, EventArgs e)
    {
        String path = @"C:\Users\Core i3\Desktop\OS.Soluctions.txt";

        StreamWriter arq = new StreamWriter(path, true);
        arq.WriteLine("");
        arq.Write(TbNumeroOS.Text.ToUpper() + "," + maskedTextBox1.Text.ToUpper() + "," + maskedTextBox3.Text.ToUpper() + "," + TbSuporte.Text.ToUpper() + "," + maskedTextBox5.Text.ToUpper() + "," + maskedTextBox4.Text.ToUpper() + "," + CbListTec.Text.ToUpper() + "," + CbStatus.Text.ToUpper()
            + "," + label12.Text.ToUpper() + "," + label13.Text.ToUpper() + "," + label14.Text.ToUpper() + "," + label15.Text.ToUpper() + "," + label16.Text.ToUpper());


        TbNumeroOS.Clear();
        TbSuporte.Clear();
        maskedTextBox1.Clear();
        maskedTextBox3.Clear();
        maskedTextBox5.Clear();
        maskedTextBox4.Clear();

        arq.Close();
        arq.Dispose();
        Console.WriteLine("Text Appended");
        MessageBox.Show("O.S. ADCIONADA COM SUCESSO", "ATENÇÃO!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void CbSupUsuario_CheckedChanged(object sender, EventArgs e)
    {
        if (CbSupUsuario.Checked == true)
            label12.Text = "Suporte ao Usuário";

    }

    private void CbMaqEquip_CheckedChanged(object sender, EventArgs e)
    {
        if (CbMaqEquip.Checked == true)
            label13.Text = "Máquina/Equipamento";
        else
            label13.Text = "";
    }

    private void CbFaltaInter_CheckedChanged(object sender, EventArgs e)
    {
        if (CbFaltaInter.Checked == true)
            label14.Text = "Falta de Internet";
        else
            label14.Text = "";
    }

    private void CbConfigEquip_CheckedChanged(object sender, EventArgs e)
    {
        if (CbConfigEquip.Checked == true)
            label15.Text = "Configuracao de Equipamento";
        else
            label15.Text = "";
    }

    private void CbAtuaSoftWare_CheckedChanged(object sender, EventArgs e)
    {
        if (CbAtuaSoftWare.Checked == true)
            label16.Text = "Atualizacao de Software";
        else
            label16.Text = "";
    }
    
asked by anonymous 04.11.2016 / 13:04

2 answers

2

Loop the parent control of checkboxes and get tagged information

string conteudo = "";

foreach(var control in parent.Controls)
{
    var chkbox = control as CheckBox; //Se o control não for um checkbox, retorna null

    if(chkbox != null) 
    {
        using (var writer = new StreamWriter("C:\temp\arquivo.txt", false, Encoding.UTF8))
        {             
            writer.WriteLine($"{chkbox.Text},");
        }
    }
}

Notes : parent is the "parent" control of the checkboxes, if you did not put them inside a parent control (container, parent with this , this refers to the form itself, then loop goes through all controls within the form .

    
04.11.2016 / 13:28
1

As I do not know the component (on this platform) using the same logic I use in Delphi, I can give you a solution!

As it comes to logic, it should work in any language.

{faço um faço em todos os componentes do form}
for i := 0 to Components.Count - 1 do
begin {se o componente for um ChekBox}
  if (Components[i].ClassType = TCheckBox) then
  begin
    if (TCheckBox(Components[i]).Checked = True) then
    begin
      {escreve aqui no arquivo}     
    end;
  end;
end;

A simple loop can solve your problem;

Now you can also do everything recursively:

if (CheckBoxX.Checek = True) then
  {escreve aqui no arquivo}  
if (CheckBoxY.Checek = True) then
  {escreve aqui no arquivo}  
if (CheckBoxZ.Checek = True) then
  {escreve aqui no arquivo}  
if (CheckBoxH.Checek = True) then
  {escreve aqui no arquivo}  

The bad news is that whenever you implement new options you should always implement the writing of the file.

    
04.11.2016 / 13:18