Saving checkbox information in TXT files

Asked

Viewed 221 times

1

I’m developing an O.S. software where I’ve divided the main problems into several checkboxes. All data populated on the screen will be sent to a text file, which soon after will be viewed by the boss in Excel.

My problem is that I am not able to capture information only from checkboxes that have been marked.

My question is: How to capture information relating only to checkboxes that have been 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 q the checkbox represents, I do not know if it is the most correct way. I added a code that I assembled from a Net tutorial, I need to capture the information for 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 = "";
    }
  • 1

    Winforms? WPF? ASP.NET Webforms? ASP.NET MVC?

  • I’m doing with Winforms

  • And what information do you need to get from the checkboxes? They are inside some container (a parent control)?

  • I’ve added more code so you can better understand the structure I’m trying to build

  • Your code tells me nothing. If you answered what I asked would be much easier.

  • They are without control, sorry for the delay to answer, is that I am learning in the fight, but confident

  • 1

    Young, it’s okay you don’t know/understand something. But trying to develop something without at least understanding the basis of the technology/language/framework is shooting in the foot. Also watch out when you come to ask for help, write some lines of text (confusing, by the way) and put a block of code is not enough in most cases. You need to explain to us well what you are trying to do and how you are trying to do it, what your expected behavior is and where your difficulty lies. Stop to notice how many questions I asked you, without them it is impossible to answer your question, this is reason to close it

  • I put an observation at the end of my answer. It’s all I can answer with the information you’ve given so far. You can comment if you have any questions or want to add information to your problem. For now, this is the most I can do.

  • I’m already meeting, the Checkboxes are outside the Parent Container, they are all inside a Panel. That’s what I was able to identify

  • Great. You just got confused about one thing. There is no parent container, I mean, there’s no parent control for the form, that’s a relative concept, your dashboard is the parent container of checkboxes. Now look at my answer, change the parent by panel name.

Show 5 more comments

2 answers

2

Loop the parent control of the checkboxes and get the information of the ones marked

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},");
        }
    }
}

Remarks: parent is the "parent" control of the checkboxes, if you have not placed them inside a parent control (container, or whatever you want to call it), exchange them parent for this, the this refers to the form itself, then the loop will go through all controls inside the form.

1

As I do not know the component (in this platform) using the same logic that I use in Delphi I can present 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 file writing.

  • In this second option that you mentioned I could take to receive in the Checkboxy itself and quote it , because in addition to the information of the checkbox, there are the textbox of the screen, in this case, Excel can only mount if you have separated by comma the information,

  • Yes, yes, the problem of the second is what I mentioned, whenever implementing new Checkbox will have to implement the writing! Dynamic saves code and facilitates maintenance!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.