从另一个表单更改一个表单的数据

时间:2014-04-20 23:58:44

标签: c# winforms forms

我的程序是测试。它包含一些问题和一些答案。我希望能够添加一些问题。我创建了一个MenuStrip,在那里打开了另一个Form,在那里我为他们写了新的问题和答案。我有su课程以主要形式保存问题:

public class Question
{
    public Question(string q_text,  Dictionary<string, bool> ans)
    {
        text = q_text;
        answers = ans;
        isAnswered = false;
    }
    public string text { get; set; }
    public Dictionary<string, bool> answers { get; set; }// = new Dictionary<RadioButton, bool>();
    public bool isAnswered;
}

public Dictionary<int, Question> questions = new Dictionary<int, Question>();

我将它们设置为公开,但仍然存在一些问题。它无法在字典中看到现有问题。这是一种以其他形式添加新问题的方法:

private void button1_Click(object sender, EventArgs e)
{
    Form1 tmp = new Form1();
    Dictionary<string,bool> ans = new Dictionary<string,bool>();
    ans.Add(answer1.Text,checkBox1.Checked);
    ans.Add(answer2.Text,checkBox2.Checked);
    ans.Add(answer3.Text,checkBox3.Checked);
    ans.Add(answer4.Text,checkBox4.Checked);
    tmp.AddQuestion(textBox1.Text, ans);
}

主要形式的方法:

public void AddQuestion(string text, Dictionary<string, bool> ans)
        {
            Question q = new Question(text, ans);
            questions.Add(questions.Count + 1, q);
            GroupBox gb = new GroupBox();
            gb.Name = "GroupBox" + (questions.Count + 1);
            gb.Size = new Size(500, 200);
            gb.Location = new Point(40, loc);
            gb.BackColor = System.Drawing.Color.Aquamarine;

            Label q_text = new Label(); // текст питання
            q_text.Text = text;
            q_text.Font = new Font("Aria", 10, FontStyle.Bold);
            q_text.Location = new Point(10, 10);
            gb.Controls.Add(q_text);
            int iter = q_text.Location.Y + 30;
            if (CheckIfMuliple(questions.Count))
            {
                foreach (string key in ans.Keys)
                {
                    CheckBox rb = new CheckBox();
                    rb.Text = key;
                    rb.Size = new Size(120, 25);
                    rb.Location = new Point(q_text.Location.X + 10, iter);
                    iter += 30;
                    gb.Controls.Add(rb);
                }

            }
            else
                {
                    foreach (string key in ans.Keys)
                    {
                        RadioButton rb = new RadioButton();
                        rb.Text = key;
                        rb.Size = new Size(120, 25);
                        rb.Location = new Point(q_text.Location.X + 10, iter);
                        iter += 30;
                        gb.Controls.Add(rb);
                    }
                }
            this.Controls.Add(gb);   
        }

实施向主表单添加新问题的更好方法是什么?

1 个答案:

答案 0 :(得分:0)

首先将Form1构造函数Access修饰符更改为private。

然后在Form1类中添加这部分代码:

private static Form1 _Instance;
public static Form1 Instance
{
    get
    {
        if (_Instance == null)
            _Instance = new Form1();
        return _Instance;
    }
}

现在您可以轻松访问所有form1成员:

Form1.Instance.AnyMember

这是一种名为Singleton Pattern的模式。