Search code examples
c#.netvisual-studiowinformsdesktop-application

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


Everyone, I have two forms 1st is MainForm and 2nd is DropdownUserControl (user control) now what I want is that when I click SaveButton it should save all the parameters (Alldropdown and textBoxes) into the database, and change button colour of Confirm from red to green, and also retrieve that colour from the database to the confirm button.

EVERY THING IS WORKING FINE BUT I COULD'NT FIND TO SAVE COLOR which is in MainForm TO DATABASE AND SHOW that color to Confirm Button which is in the DropdownUserControl.

I tried but could not find any solution. Thanks in advance.

LEFT IS USER CONTROL & RIGHT IS MAIN FORM OPTIONS

Here is the code for saving parameters Click method of SAVE Button

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

To retrive Data I have used the folowing code

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

                }
            }
        }
    }
}

Solution

  • Not sure what the controls on the right-side are? Your custom controls? Or 3rd-party controls? I assumed that they are custom controls.

    Here is a demo maybe you can refer to.

    I have created the user control(DropdownUserControl):

    enter image description here

    And another user control(AnotherUserControl):

    enter image description here

    To get the "Confirm button" instance, we need to define a property in 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;
        }
    }
    

    Then modify the btnSave_Click in DropdownUserControl.cs.

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

    Now, I added the DropdownUserControl and four AnotherUserControls. And here is the test result.

    enter image description here

    As to save the color to database and retrieve it, you can save the rgb value as a string in to datatable, such as "0,128,0".

    Then subscribe to Form_Load event. Similar to the code in "btnSave_Click", Judge whether it is AnotherUserControl, check the buttonID, and query the database to get the corresponding "rgb string".