0

嗨,我是 c# 的新手,我正在尝试将 12 个复选框保存到以所选日期命名的 TEXT 文件中。

我有要输出的复选框,但似乎没有以日期命名的文件。我似乎也在这方面走了很长一段路。如果任何机构现在有任何更短的路线或我出错的地方,那就太好了。

    public void timepicker_ValueChanged(object sender, EventArgs e)
    {
        string date = timepicker.Value.ToString("dd-MM-yyyy");           
    }

    string amorpm = "";

    if (rb_am.Checked) 
    {
       am = rb_am.Text;
    }
    else if (rb_pm.Checked) 
    {
       pm = rb_pm.Text;
    }

    SteamWriter am = File.CreateText("seats_file.txt");
    am.WriteLine ("am morning run");
    am.close;

    SteamWrite pm = File.CreateText("seats_file.txt");
    pm.WriteLne ("pm afternoon nrun");
    pm.Close;

    public void btn_saveseats_Click(object sender, EventArgs e)
    {
        {
           String date = timepicker.Value.ToString("dd-mm-yyyy");
          //String fileName = String.Format("{0}File.txt", date);

        try

            if (File.Exists(date))

            {
               (MessageBox.Show("Warning: file already exists. Contents will be replaced - do you want to continue?", "File Demo", MessageBoxButtons.YesNo, MessageBoxIcon.Question,
                     MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly) == DialogResult.Yes)
                {

                    //write lines of text to file

                    StreamWriter date = File.CreateText(date);
                    date.WriteLine(date);
                    date.Close();

                    if (cbseat1.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat1 = File.CreateText("seats_file.txt");
                        seat1.WriteLine("seat 1 taken");
                        seat1.Close();
                    }

                    if (cbseat2.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat2 = File.AppendText("seats_file.txt");
                        seat2.WriteLine("seat 2 taken.Text");
                        seat2.Close();
                    }

                    if (cbseat3.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat3 = File.AppendText("seats_file.txt");
                        seat3.WriteLine("seat 3 taken");
                        seat3.Close();
                    }

                    if (cbseat4.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat4 = File.AppendText("seats_file.txt");
                        seat4.WriteLine("seat 4 taken");
                        seat4.Close();
                    }

                    if (cbseat5.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat5 = File.AppendText("seats_file.txt");
                        seat5.WriteLine("seat 5 taken");
                        seat5.Close();
                    }

                    if (cbseat6.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat6 = File.AppendText("seats_file.txt");
                        seat6.WriteLine("seat 6 taken");
                        seat6.Close();
                    }

                    if (cbseat7.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat7 = File.AppendText("seats_file.txt");
                        seat7.WriteLine("seat 7 taken");
                        seat7.Close();
                    }

                    if (cbseat8.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat8 = File.AppendText("seats_file.txt");
                        seat8.WriteLine("seat 8 taken");
                        seat8.Close();
                    }

                    if (cbseat9.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat9 = File.AppendText("seats_file.txt");
                        seat9.WriteLine("seat 9 taken");
                        seat9.Close();
                    }

                    if (cbseat10.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat10 = File.AppendText("seats_file.txt");
                        seat10.WriteLine("seat 10 taken");
                        seat10.Close();
                    }

                    if (cbseat11.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat11 = File.AppendText("seats_file.txt");
                        seat11.WriteLine("seat 11 taken");
                        seat11.Close();
                    }

                    if (cbseat12.Checked)
                    {
                        // Do something when CheckBox is checked
                        StreamWriter seat12 = File.AppendText("seats_file.txt");
                        seat12.WriteLine("seat 12 taken");
                        seat12.Close();
                    }

                    MessageBox.Show("saved");

                }
            }
        }
    }
}

}

4

2 回答 2

2

是的,你走错了方向,需要重写你的代码,这里有更好的代码版本:

public void btn_saveseats_Click(object sender, EventArgs e){

    String date = timepicker.Value.ToString("dd-mm-yyyy");
    String fileName = String.Format("{0}File.txt", date);

    using (StreamWriter writer = new StreamWriter(fileName)){
        writer.WriteLine(date);
        if(cbseat1.Checked)
            writer.WriteLine(cbseat1.Text);
        if(cbseat2.Checked)
            writer.WriteLine(cbseat2.Text);
        if(cbseat3.Checked)
            writer.WriteLine(cbseat3.Text);
        if(cbseat4.Checked)
            writer.WriteLine(cbseat4.Text);
        if(cbseat5.Checked)
            writer.WriteLine(cbseat5.Text);
        if(cbseat6.Checked)
            writer.WriteLine(cbseat6.Text);
        if(cbseat7.Checked)
            writer.WriteLine(cbseat7.Text);
        if(cbseat8.Checked)
            writer.WriteLine(cbseat8.Text);
        if(cbseat9.Checked)
            writer.WriteLine(cbseat9.Text);
        if(cbseat10.Checked)
            writer.WriteLine(cbseat10.Text);
        if(cbseat11.Checked)
            writer.WriteLine(cbseat11.Text);
        if(cbseat12.Checked)
            writer.WriteLine(cbseat12.Text);
    }
    MessageBox.Show("saved");
}

例如,我编写了可以用任何其他字符串替换的复选框文本。
using块在写入文件后处理实例,因此您无需显式调用writer.Dispose();.

更好的方法:

将您的复选框包装在任何容器控件(如PanelGroupBox )中,然后使用其Controls属性枚举其子控件,检查其类型是否为Checkbox,如果是则检查它是否被选中。this.Controls如果您的复选框是Form的直接子级,则可以使用:

foreach(Control ctrl in pnl.Controls){
    if(ctrl is Checkbox){
        Checkbox chk = ctrl as Checkbox;
        if(chk.Checked){
            switch(chk.Name){
                case "chkSeat1":
                    writer.WriteLine(chk.Text);
                    break;
                case "chkSeat2":
                    writer.WriteLine(chk.Text);
                    break;
                case "chkSeat3":
                    writer.WriteLine(chk.Text);
                    break;

                ...
            }
        }
    }
}
于 2012-11-23T16:05:30.650 回答
0

一些可以帮助您更快完成繁重工作的东西:

    public void WriteCBValuesToFile()
    {
        foreach (Control control in this.Controls) // 'this' is the form... In your case it might be a groupBox, panel, etc.
        {
            if (control is CheckBox)
            {
                CheckBox cb = (CheckBox)control;

                if (cb.Name == "Whatever")
                { 
                    //Whatever...
                }
            }
        }
    }
于 2012-11-23T15:58:50.147 回答