如何使用 C# 中的 SQlite 将按钮的颜色从一种形式更改为另一种形式

How to change color of button From one form to another with SQlite in c#

各位,我有两个 form 第一个是 MainForm 第二个是 DropdownUserControl (用户控制) 现在我想要的是当我点击 SaveButton 它应该保存所有 参数 (Alldropdown and textBoxes) 到数据库中,并将 Confirm 的按钮颜色从红色更改为绿色,同时从数据库中检索该颜色到确认按钮。

一切正常,但我找不到保存颜色 的颜色 MainForm 到数据库并将该颜色显示到确认 按钮位于 DropdownUserControl

我尝试了但找不到任何解决方案。提前致谢。

这里是SAVE按钮

保存参数点击方法的代码
using (SQLiteConnection conn = new SQLiteConnection(AppSetting.ConnectionString()))
{

    string commandString = "INSERT INTO Information ( [DropDownButtonsNumbers], [Market], [SubMarket], [BackLay],[@BetType],[TickoffSet], [FillorKill] ) VALUES ( @DropDownButtonsNumbers, @Market, @SubMarket,@BackLay, @BetType, @TickoffSet,@FillorKill)";
    using (SQLiteCommand cmd = new SQLiteCommand(commandString, conn))
    {
        conn.Open();

        cmd.Parameters.AddWithValue("@DropDownButtonsNumbers", ButtonComboBox.GetItemText(ButtonComboBox.SelectedItem));
        cmd.Parameters.AddWithValue("@Market", MarketComboBox.GetItemText(MarketComboBox.SelectedItem));
        cmd.Parameters.AddWithValue("@SubMarket", SubMarketComboBox.GetItemText(SubMarketComboBox.SelectedItem));
        cmd.Parameters.AddWithValue("@BackLay", BackLayComboBox.GetItemText(BackLayComboBox.SelectedItem));
        cmd.Parameters.AddWithValue("@BetType", BetTypeComboBox.GetItemText(BetTypeComboBox.SelectedItem));
        cmd.Parameters.AddWithValue("@TickoffSet", TickOffsetTextBox.Text);
        cmd.Parameters.AddWithValue("@FillorKill", FillorKillTextBox.Text);

        cmd.ExecuteNonQuery();
        MessageBox.Show("Saved Successfully", "validation", MessageBoxButtons.OK, MessageBoxIcon.Information);

    }
    conn.Close();
}

为了检索数据,我使用了以下代码

public void ShowDataInLabelsForRecordOne()
{

    using (SQLiteConnection c = new SQLiteConnection(AppSetting.ConnectionString()))
    {
        string commandString = "SELECT DropDownButtonsNumbers, Market,SubMarket,BackLay,BetType,TickoffSet,FillorKill  FROM  InformationOfParameters WHERE DropDownButtonsNumbers=1";

        c.Open();
        using (SQLiteCommand cmd = new SQLiteCommand(commandString, c))
        {
            using (SQLiteDataReader rdr = cmd.ExecuteReader())
            {
                while (rdr.Read())
                {
                    // ButtonComboBox.DisplayMember = "DropDownButtonsNumbers";
                    MarketComboBox.DisplayMember = "Market";
                    SubMarketComboBox.DisplayMember = "SubMarket";
                    BackLayComboBox.DisplayMember = "BackLay";
                    BetTypeComboBox.DisplayMember = "BetType";

                    TickOffsetTextBox.Text = rdr["TickoffSet"].ToString();
                    FillorKillTextBox.Text = rdr["FillorKill"].ToString();

                }
            }
        }
    }
}

不确定右侧的控件是什么?您的自定义控件?还是第三方控件?我假设它们是自定义控件。

这里有一个demo,大家可以参考一下

我已经创建了用户控件(DropdownUserControl):

还有另一个用户控件(AnotherUserControl):

要获取“确认按钮”实例,我们需要在AnotherUserControl.cs中定义一个属性。

public partial class AnotherUserControl : UserControl
{
    public AnotherUserControl()
    {
        InitializeComponent();
    }

    // Show some text
    [Description("Test text displayed in the textbox"), Category("Data")]
    public string label1text
    {
        get => label1.Text;
        set => label1.Text = value;
    }

    // Show Button Id 1-30
    [Description("ID"), Category("Data")]
    public string labelID
    {
        get => label2.Text;
        set => label2.Text = value;
    }

    // Get Confirm button instance
    public Button BTN
    {
        get => btnConfirm;
        set => btnConfirm = value;
    }
}

然后修改DropdownUserControl.cs中的btnSave_Click.

private void btnSave_Click(object sender, EventArgs e)
{
    // Form1 is the Main Form in your description 
    Form1 form1 = this.Parent as Form1;
    foreach (Control control in form1.Controls)
    {
        //  Check if is AnotherUserControl and the button ID
        if (control is AnotherUserControl && ((AnotherUserControl)control).labelID == comboBoxbutton.Text)
        {
            AnotherUserControl uc = control as AnotherUserControl;
            uc.BTN.BackColor = Color.FromArgb(0, 128, 0); // change Confirm button color to green
        }
    }
}

现在,我添加了 DropdownUserControl 和四个 AnotherUserControl。这是测试结果。

关于将颜色保存到数据库并检索,可以将rgb值作为字符串保存到数据表中,如"0,128,0".

然后订阅Form_Load事件。类似“btnSave_Click”中的代码,判断是否为AnotherUserControl,查看buttonID,查询数据库得到对应的“rgb string”。