简体   繁体   中英

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. I have su class to save questions in the main form:

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.

Then add this part of code in Form1 class:

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.Instance.AnyMember

This is a pattern called Singleton Pattern.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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