0

对 C# 来说相当新,所以如果我遗漏了一些简单的东西或试图以错误的方式做这件事,请原谅我。

我正在创建另一个表单来补充我的主表单,它需要在单击第二个表单的按钮时从主表单中提取一些信息。Main for 上的信息存储在复选框和文本框中。

我的文本框工作正常,但无法弄清楚如何将复选框标记数据连同格式一起拉出。主窗体工作正常,除了我无法弄清楚如何将复选框数据带过来。

这是我目前用来在我的主窗体上显示复选框 TAG 数据的代码。

 //Statements to write checkboxes to stringbuilder
string checkBoxesLine = "\u2022 LIGHTS ";

foreach (Control control in pnlCheckBoxes.Controls)
{
    if (control is CheckBox)
    {
        CheckBox checkBox = (CheckBox)control;

        if (checkBox.Checked && checkBox.Tag is string)
        {
            string checkBoxId = (string)checkBox.Tag;
            checkBoxesLine += string.Format("{0}, ", checkBoxId);
        }
    }
}

这是我用来打开新表单并将复选框标记数据和 textbox.text 数据移动到新表单的按钮。

private void code_blue_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    trouble_shooting = tshooting_text.Text;
    services_offered = services_offered_text.Text;
    other_notes = other_notes_text.Text;
    cust_modem = cust_modem_text.Text;
    //Opens CODE BLUE form and pushes text to it.
    code_blue_form CBForm = new code_blue_form();
    CBForm.cust_name_cb = cust_name_text.Text;
    CBForm.cust_cbr_cb = cust_callback_text.Text;
    CBForm.cust_wtn_cb = cust_btn_text.Text;
    CBForm.cust_notes_cb = cust_modem + "\r\n" + trouble_shooting + "\r\n" + services_offered + "\r\n" + other_notes;

    CBForm.Show();
}

这是我的第二种表单的代码,以及我如何获取信息以填充该表单上的文本框。

public partial class code_blue_form : Form
{
    public string cust_name_cb;
    public string cust_wtn_cb;
    public string cust_cbr_cb;
    public string cust_notes_cb;
    public string cust_modem_cb;
    public code_blue_form()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cb_name_text.Text = cust_name_cb;
        cb_wtn_text.Text = cust_wtn_cb;
        cb_cbr_text.Text = cust_cbr_cb;
        cb_notes_text.Text = cust_notes_cb;
    }
  }
}

长文请见谅!对此的任何想法/方向将不胜感激。谢谢!

4

1 回答 1

1

我不会使用您的代码立即回答。我发现代码有异味。如果我是你,我会这样做(但如果你坚持使用相同的设计,那么你可以相应地调整我的代码,没什么大不了的,最重要的是你知道该怎么做):

void code_blue_link_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
    var checkBoxIds = GetCheckBoxIds();

    cust_modem = cust_modem_text.Text;
    trouble_shooting = tshooting_text.Text;
    services_offered = services_offered_text.Text;
    other_notes = other_notes_text.Text;

    //take off underscore convetion and replace with camelCase convention.
    CodeBlueForm cbForm = new CodeBlueForm(checkBoxIds, cust_name_text.Text, 
                                           cust_callback_text.Text, 
                                           cust_btn_text.Text,
                                           cust_modem + "\r\n" + 
                                           trouble_shooting + "\r\n" + 
                                           services_offered + "\r\n" + 
                                           other_notes);

    cbForm.Show();
}

private List<int> GetCheckBoxIds()//even better naming required like GetFruitIds() 
{                                 //(whatever the id is of) or even better Tag  
                                  //checkBoxes with the Fruit object iself and not ids.
    List<int> checkBoxIds = new List<int>(); //So you can call GetFruits();
    foreach (Control control in pnlCheckBoxes.Controls)
    {
        if (control is CheckBox)
        {
            CheckBox checkBox = (CheckBox)control;

            if (checkBox.Checked && checkBox.Tag is int) //tag them as ints. 
                checkBoxIds.Add((int)checkBox.Tag);      //I hope ids are integers.
        }
    }

    return checkBoxIds;
}

public partial class CodeBlueForm : Form
{
    List<int> checkBoxIds = new List<int>():
    string cust_cbr_cb; //should be private.
    string cust_name_cb;
    string cust_wtn_cb;
    string cust_notes_cb;
    string cust_modem_cb;

    public CodeBlueForm(List<int> ids, string cust_name_cb, string cust_wtn_cb, 
                        string cust_notes_cb, string cust_modem_cb)
    {
        InitializeComponent();

        this.checkBoxIds = ids;
        this.cust_name_cb = cust_name_cb;
        this.cust_wtn_cb = cust_wtn_cb;
        this.cust_notes_cb = cust_notes_cb;
        this.cust_modem_cb = cust_modem_cb;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        cb_name_text.Text = cust_name_cb; 
        cb_wtn_text.Text = cust_wtn_cb;
        cb_cbr_text.Text = cust_cbr_cb;
        cb_notes_text.Text = cust_notes_cb;

        string checkBoxesLine = "\u2022 LIGHTS ";        
        // if you dont require a lot of string formatting, then just:
        checkBoxesLine += string.Join(", ", checkBoxIds);

        // or go with your classical:
        //foreach (int id in checkBoxIds)
        //    checkBoxesLine += string.Format("{0}, ", checkBoxIds);

        //and do what u want with checkboxline here.
    }
}
于 2012-09-04T19:22:50.957 回答