Thread: Dynamic Button Text

  1. #1
    Registered User
    Join Date
    Jun 2015
    Posts
    8

    Dynamic Button Text

    I have two forms, one form I enter information into that populates a db... on the other, I need to text on the buttons to change based on the information in the 'Description' field. This change should happen when I hit the save button on the other form. Here is my code:

    Code:
    private void ButtonTextChange()
            {
                SqlConnection conn = new SqlConnection("Data  Source=DAHLENO0744\\;Initial Catalog=USPCNew;Integrated  Security=True;Pooling=False");
                try
                {
                    conn.Open();
                    int myint = 1;
                    string sql = "select a.* from (SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 1";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 2";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 3";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 4";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 5";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 6";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 7";
                    sql += " union SELECT ButtonNum, Description FROM StationSetup WHERE ButtonNum = 8) as A order by ButtonNum";
    
                    SqlCommand cmd = new SqlCommand(sql, conn);
    
                    SqlDataReader reader = cmd.ExecuteReader();
    
                    while (reader.Read())
                    {
                        switch (myint)
                        {
                            case 1:
                                button_1_M.Text = reader[1].ToString();
                                button_1_C.Text = reader[1].ToString();
                                break;
    
                            case 2:
                                button_2_M.Text = reader[1].ToString();
                                button_2_C.Text = reader[1].ToString();
                                break;
    
                            case 3:
                                button_3_M.Text = reader[1].ToString();
                                button_3_C.Text = reader[1].ToString();
                                break;
    
                            case 4:
                                button_4_M.Text = reader[1].ToString();
                                button_4_C.Text = reader[1].ToString();
                                break;
    
                            case 5:
                                button_5_M.Text = reader[1].ToString();
                                button_5_C.Text = reader[1].ToString();
                                break;
    
                            case 6:
                                button_6_M.Text = reader[1].ToString();
                                button_6_C.Text = reader[1].ToString();
                                break;
    
                            case 7:
                                button_7_M.Text = reader[1].ToString();
                                button_7_C.Text = reader[1].ToString();
                                break;
    
                            case 8:
                                button_8_M.Text = reader[1].ToString();
                                button_8_C.Text = reader[1].ToString();
                                break;
    
                        }
                        myint++;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show("There is an error" + ex);
                }
                finally
                {
                    conn.Close();
                }
            }
    What am I doing wrong?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    First of all, that's a really nutso query. Can't it just be simplified down to:

    Code:
    select ButtonNum, Description from StationSetup where ButtonNum in (1,2,3,4,5,6,7,8) order by ButtonNum
    Also, where are you running into problems? Calling this method from the other form? The other form will need to be able to reference an instance of this form somehow. Passing 'this' into the constructor of the other form from this one and then storing it there in a field member until it's needed when you hit the save button is one way to do that.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2015
    Posts
    8
    Quote Originally Posted by itsme86 View Post
    First of all, that's a really nutso query. Can't it just be simplified down to:

    Code:
    select ButtonNum, Description from StationSetup where ButtonNum in (1,2,3,4,5,6,7,8) order by ButtonNum
    Also, where are you running into problems? Calling this method from the other form? The other form will need to be able to reference an instance of this form somehow. Passing 'this' into the constructor of the other form from this one and then storing it there in a field member until it's needed when you hit the save button is one way to do that.
    I was able to create a data binding within the form that fixed the issue I was having.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dynamic button controls [WIN32 API/c++/VS 2010]
    By William Putnam in forum Windows Programming
    Replies: 3
    Last Post: 09-10-2013, 09:13 PM
  2. Type text = Press button = Display text in Google?
    By Raze88 in forum C++ Programming
    Replies: 4
    Last Post: 03-20-2008, 08:39 AM
  3. button text?
    By Marcuzen-90 in forum Windows Programming
    Replies: 1
    Last Post: 01-05-2006, 04:48 PM
  4. text on owner-drawn button?
    By SyntaxBubble in forum Windows Programming
    Replies: 5
    Last Post: 02-17-2002, 01:08 PM
  5. & thing in button text
    By Unregistered in forum Windows Programming
    Replies: 3
    Last Post: 11-30-2001, 09:35 AM

Tags for this Thread