简体   繁体   English

将一种形式的数据从另一种形式更改

[英]Change data of one form from another form

My program is a testing. 我的程序是一个测试。 It contains some questions and some answers for them. 它包含一些问题和针对它们的一些答案。 I want to make it possible to add some questions. 我想添加一些问题。 I created a MenuStrip , where it opens another Form , where I write new question and answers for them. 我创建了一个MenuStrip ,在其中打开了另一个Form ,在其中为他们编写新的问题和答案。 I have su class to save questions in the main 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>();

I set them to public, but there are still some problems. 我将它们设置为公开,但是仍然存在一些问题。 It can't see existing questions in the Dictionary. 它在字典中看不到现有的问题。 This is a method that adds new question in other form: 这是一种以其他形式添加新问题的方法:

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

Method in main form: 主要形式的方法:

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

What's a better way to implement adding new question to main form? 有什么更好的方法来实现在主表单中添加新问题?

First change your Form1 constructor Access modifier to private. 首先将您的Form1构造函数访问修饰符更改为private。

Then add this part of code in Form1 class: 然后将这部分代码添加到Form1类中:

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

Now you access all form1 member easily: 现在,您可以轻松访问所有form1成员:

Form1.Instance.AnyMember

This is a pattern called Singleton Pattern. 这是一个称为“单例模式”的模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM