0

I am working on a prototype project in c# WinForms, that has 3 forms as follows:

enter image description here

1-MasterForm 2-Layout 3-UpdateLayout i also have a table in my database, that table simply have 2 column one for the layout_no, another for layout_status. if the layout is busy i need to change its color to red, if not busy i keep it to green color.

Ny problem after I update the layout to busy is, that it doesn't refresh in the MasterForm.

Here's my code for UpdateLayout Form:

public void refreshLayout()
{
    try
    {
        using (SQLiteConnection con = new SQLiteConnection(ConnectToDB))
        {
            con.Open();
            using (SQLiteDataAdapter da = new SQLiteDataAdapter("SELECT layout_no, is_busy FROM layout_table", con))
            {
                DataTable dt = new DataTable();
                da.Fill(dt);
                int totalrows = dt.Rows.Count;
                for (int i = 0; i < totalrows; i++)
                {
                    int status = Convert.ToInt32(dt.Rows[i]["is_busy"]);
                    Button ButtonAllLayout = new Button();
                    ButtonAllLayout.Name = "Layout'" + (i + 1) + "'";
                    ButtonAllLayout.Text = dt.Rows[i]["Layout_no"].ToString();
                    styleButton();//apply the button location...etc
                    this.Controls.Add(ButtonAllLayout);
                    if (status == 1)
                    {
                        ButtonAllLayout.BackColor = Color.Red;
                    }
                    else
                    {
                        ButtonAllLayout.BackColor = Color.YellowGreen;
                    }
                }
            }
        }
    }
    catch (Exception)
    {
        throw;
    }
}

private void button1_Click(object sender, EventArgs e)
{
    refreshLayout();
}

Here's my code for MasterForm:

public MasterForm()
{
    InitializeComponent();
}

private void BtnLayout_Click(object sender, EventArgs e)
{
    Layout l1 = new Layout();
    l1.refreshLayout();
    splitContainerMain.Panel2.Show();
    splitContainerMain.Panel2.Controls.Add(l1);
    l1.Show();
}

private void BtnSetting_Click(object sender, EventArgs e)
{
    Settings s1 = new Layout();
    splitContainerMain.Panel2.Show();
    splitContainerMain.Panel2.Controls.Add(s1);
    s1.Show();
}

private void BtnMain_Click(object sender, EventArgs e)
{
    Main m1 = new Main();
    splitContainerMain.Panel2.Show();
    splitContainerMain.Panel2.Controls.Add(m1);
    m1.Show();
}

Can you please guide me how to do this?

Thanks in advance.

7
  • Have you debugged your code? Is the status definitely updating from your DB? An aside note: is there any particular reason you're just re-throwing the exception without doing anything else? Have a read-up on handling exceptions :) Commented Oct 12, 2016 at 11:56
  • 1
    What is the point of doing catch (Exception) { throw; }? Commented Oct 12, 2016 at 12:01
  • What I said @Enigmativity ^^ :D Commented Oct 12, 2016 at 12:02
  • @GeoffJames - Yep, that. Sorry to repeat. Commented Oct 12, 2016 at 12:03
  • @Enigmativity Haha, it's cool. We good :p Commented Oct 12, 2016 at 12:04

3 Answers 3

1

First you should give the parent info for UpdateLayout forms

UpdateLayout frm = new UpdateLayout();
frm.Owner = this; //Layout form
frm.Show();

After that you can call public refreshLayout method of Layout form,

from UpdateLayout form's button event

private void button1_Click(object sender, EventArgs e)
{
    (this.Owner as UpdateLayout).refreshLayout();
}

Also you need to clear buttons previously created

at the begining of refreshLayout method

this.Controls.Clear();

Or implement new method other than refreshLayout to change only changed button's backColor

Sign up to request clarification or add additional context in comments.

Comments

0

As far as I see your buttons are all added with Size(0,0) and Location(0,0). So I think they are there but simply not visible because of size and location.

Try to add something like this:

int startX = 0;
int startY = 0;

for (int i = 0; i < totalrows; i++)
{
    int status = Convert.ToInt32(dt.Rows[i]["is_busy"]);
    Button ButtonAllLayout = new Button();
    ButtonAllLayout.Name = "Layout'" + (i + 1) + "'";
    ButtonAllLayout.Text = dt.Rows[i]["Layout_no"].ToString();
    ButtonAllLayout.Size = new Size(80, 20);
    ButtonAllLayout.Location = new Point(startX, startY);
    startY += 23;
    this.Controls.Add(ButtonAllLayout);
    ...
}

7 Comments

Thanks for your answer.... the issue not with the location, i have set the location and everything works great if i am clicking the layout button from the MasterForm, but when i try to invoke the same code from UpdateLayout it doesn't refresh and doesn
Wouldn't the button's size be automatic, if it's not set, anyway?
Thanks for your answer.... the issue not with the location, i have set the location and everything works great if i am clicking the layout button from the MasterForm, but when i try to invoke the same code from UpdateLayout it doesn't refresh and doesn't load correctly until i click again on the Layout button from MasterForm
But you add the buttons always to your UpdateLayout, not to the MasterForm: this.Controls.Add(...) while this is your UpdateLayout-Form.
may you please have a look at the image that i attached ?
|
0

To apply custom BackColor to your button you should set UseVisualStyleBackColor = false for your ButtonAllLayout.

By the way, in production code, it's not a good idea to open DB connection on GUI thread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.