单击另一个按钮时如何更改按钮的颜色?

时间:2017-07-21 02:08:48

标签: winforms visual-studio-2010

我想要做的是当我点击btn1(将启用组合框)时,我将在组合框中选择一种颜色(例如红色),然后单击btnSave。 btn1的颜色将变为红色(永久)。

1 个答案:

答案 0 :(得分:0)

我假设使用vb

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim dt As New DataTable
    dt.Columns.Add("no", GetType(String))
    dt.Columns.Add("color", GetType(String))
    dt.Rows.Add("1", "Red")
    dt.Rows.Add("2", "Blue")

    ComboBox1.DataSource = dt
    ComboBox1.DisplayMember = dt.Columns("color").ToString
    ComboBox1.ValueMember = dt.Columns("no").ToString
    ComboBox1.Enabled = False
End Sub


Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ComboBox1.Enabled = True
End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    If ComboBox1.Enabled <> False Then
        Button1.BackColor = Color.FromName(ComboBox1.Text)
    End If
End Sub

CMIIW