如何调用通过form1中的数组从DB调用的值并使用它通过C#在form2中创建条件

how to call value that called from DB by array in form1 and used it to make condition in form2 by C#

我在我的 DB

中为 table 创建了 class
namespace WindowsFormsApp1.Database {
class Ques
{
    public int id;
    public string title;
    public Illnesses(int id,string title)
    {
        this.id = id;
        this.title = title;
    }
    public static Ques[] getAll()
    {
        Ques[] arr = new Ques[100];
        connection con1 = new connection();
        MySqlCommand command = new MySqlCommand("SELECT * FROM ques;", con1.mycon());
        MySqlDataReader reader = command.ExecuteReader();
        int counter = 0;
        while (reader.Read())
        {
            Ques= new Ques(reader.GetInt32(0), reader.GetString(1));
            arr[counter++] = temp;
        }
        con1.con.Close();
        return arr;
    } 
} }

form1 中,我通过 Array[=45= 在数据库中调用了我的 Table Rows ] 在 按钮名称中显示文本 当我 运行 程序和这个 工作完美 然后将我转移到 form2

namespace WindowsFormsApp1 {
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        Ques[] ques= Ques.getAll();
        button1.Text = ques[0].title;
        button2.Text = ques[1].title;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        this.Hide();
    }

    private void button2_Click(object sender, EventArgs e)
    {
        Form2 f2 = new Form2();
        f2.Show();
        this.Hide();
    }
} }

And This is The Run, 问题是 我需要在 form2 中使 if 条件 取决于(按钮 1、2 或行我在按钮中从 DB 调用)在 from1

我的Form2

中输出不同的结果

我的 Form2

namespace WindowsFormsApp1 {
public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();

        Form1 form1 = new Form1();
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Ques[] ques= Ques.getAll();

        if (???????????)
        {
            button1.Text = ques[2].title;
            label1.Text = "this is ans Q1";
        }
        else if (????????????)
        {
            button1.Text = ques[3].title;
            label1.Text = "this is ans Q2";
        }

    }

shape of form 2

那么我应该在form2的条件下写什么(问号的地方?????????)

谢谢大家帮忙:)

我们可以判断点击的是哪个按钮,按钮文字是否与问题文字一致来写条件

您可以在 form1 中定义一些 属性 或字段。然后你可以使用 Application.OpenForms 方法访问 属性.

关键代码:

form1.btn1text== "This is my q1" && form1.bt1clicked==true
form1.btn2text == "This is my q2" && form1.bt2clicked == true

这是您可以参考的代码示例。

表格 1:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public string btn1text { get; set; }
        public string btn2text { get; set; }
        public  bool bt1clicked = false;
        public  bool bt2clicked = false;
        private void button1_Click(object sender, EventArgs e)
        {
            bt1clicked = true;
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            bt2clicked = true;
            Form2 f2 = new Form2();
            f2.Show();
            this.Hide();
      
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Ques[] ques = Ques.getAll();
            button1.Text = ques[0].title;
            button2.Text = ques[1].title;
            btn1text = button1.Text;
            btn2text = button2.Text;
        }

表格 2:

private void Form2_Load(object sender, EventArgs e)
        {
            Form1 form1 = (Form1)Application.OpenForms["Form1"];
            
            Ques[] ques = Ques.getAll();

            if (form1.btn1text== "This is my q1" && form1.bt1clicked==true)
            {
                button1.Text = ques[2].title;
                label1.Text = "this is ans Q1";
            }
            else if (form1.btn2text == "This is my q2" && form1.bt2clicked == true)
            {
                button1.Text = ques[3].title;
                label1.Text = "this is ans Q2";
            }
        }

测试结果: