Set button to visible in different class

74 Views Asked by At

So I have 5 buttons to set to the visibilty to false. However I cant do it in the class for the designer I need to do it in this class. I'm not sure if I should pass the buttons or if theres a better way? But this is where the data grid loads

    public static void CostSQL(string connstring, string passableQuery, IEdmVault7 vault, int ID, IEdmFolder5 Folder)
    {
        try
        {



            SqlConnection con = new SqlConnection(connstring);
            con.Open();

            //concat later to optimize code for universal use by passing variable quote
            SqlCommand cmd = new SqlCommand(passableQuery, con);
            DataTable rTbl = new DataTable();
            
            rTbl.Load(cmd.ExecuteReader());
            SQLResultSelector RSel = new SQLResultSelector(rTbl, vault, ID, "c", Folder);
            RSel.ShowDialog();
            



        }
        catch (Exception ex)
        {
            MessageBox.Show("Something happend during your connection to SQL error : " + ex.Message);


        }


    }
2

There are 2 best solutions below

0
Karen Payne On

Consider an event where if a condition is met, set visibility of buttons.

A conceptual example, pass true or false based on if weekday.

internal class SomeClass
{
    public delegate void OnCondition(bool condition);
    public static event OnCondition Condition;

    public static void CostSql()
    {
        Condition?.Invoke(DateTime.Now.DayOfWeek is not 
            (DayOfWeek.Sunday or DayOfWeek.Saturday));
    }
}

Then in the form subscribe to the event, in form load perhaps.

SomeClass.Condition += SomeClass_Condition;

Code in the event, decide what to do...

private void SomeClass_Condition(bool condition)
{
    // here is where you set visibility of buttons
    if (condition)
    {
        
    }
    else
    {
        
    }
}
0
Enigmativity On

I've taken the answer you've posted and tried to refactor the code into something that isn't as bad.

public static void SomeCallingCode(string connstring, string passableQuery, IEdmVault7 vault, int ID, IEdmFolder5 Folder)
{
    DataTable rTbl = CostSQL(connstring, passableQuery);
    if (rTbl != null)
    {
        SQLResultSelector RSel = new SQLResultSelector(rTbl, vault, ID, "c", Folder);
        RSel.ClearButtons(); // why they need clearing in a new Form is beyond me
        RSel.ShowDialog();
    }
}

public static DataTable CostSQL(string connstring, string passableQuery)
{
    try
    {
        using (SqlConnection con = new SqlConnection(connstring))
        {
            con.Open();
            //concat later to optimize code for universal use by passing variable quote
            using (SqlCommand cmd = new SqlCommand(passableQuery, con))
            {
                DataTable rTbl = new DataTable();
                rTbl.Load(cmd.ExecuteReader());
                return rTbl;
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show("Something happend during your connection to SQL error : " + ex.Message);
        return null;
    }
}

You should separate out your database calls from your UI code.

What I'm still unsure about is how your posted answer actually solves your question. It's unclear why you need to clear buttons on a newly created form.