0

C#はかなり新しいので、単純なものが見つからない場合や、これを間違った方法で実行しようとしている場合は、ご容赦ください。

メインフォームを補完する別のフォームを作成しています。2番目のフォームのボタンクリックでメインフォームから情報の一部を取得する必要があります。メインの情報は、チェックボックスとテキストボックスに保存されます。

テキストボックスは正常に機能していますが、チェックボックスのタグデータをフォーマットとともにプルする方法がわかりません。チェックボックスデータを持ち込む方法がわからないことを除いて、メインフォームはそのまま正常に動作しています。

これは、メインフォームにチェックボックスの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();
}

2番目のフォームのコードと、そのフォームのテキストボックスに入力するための情報を取得する方法を次に示します。

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

urコードを使用してすぐに答えるつもりはありません。コードの臭いがします。もし私があなたなら私はこれをするでしょう(しかしあなたが同じデザインで行くことに固執しているなら、あなたはそれに応じて私のコードを微調整することができます、大したことではありません、肝心なのはあなたがどのように行うかについての考えを得るということです):

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 に答える