2015-09-17 20 views
0

我正在使用Windows窗体。 textbox1.enabled = false的默认前景是灰色的,我怎样才能将它改为白色?将文本框的启用属性设置为false并将forecolor设置为白色

+0

的WinForms? WebForms?...别的东西? –

+0

@Idle_Mind,我使用Windows窗体。谢谢。 –

+0

我已经在www.Experts-Exchange.com上撰写了一篇有关该主题的文章:[禁用VB.Net WinForms文本框中自定义颜色的新方法](http://www.experts-exchange.com/articles /10842/A-New-Approach-for-Custom-Colors-in-a-Disabled-VB-Net-WinForms-TextBox.html) –

回答

0

如果WinForms的做到这一点:在专家交换

TextBox1.Enabled = false; 
+0

哦,对不起。我的标签是vb.net。 :( –

+0

我不能在工具箱中看到新的文本框,它的名称是否是'WhiteForegroundTextbox'? –

+0

如果您使用的是WebForms,我只需要这个,我在我的答案中指定了这一点,现在我看到您正在使用WinForms您使用上面的代码,因为WinForms有一个EnabledChanged事件。 – thewisegod

1

从我的文章:

Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs) 
    TextBox1.ForeColor = Color.White 
End Sub 

,然后的Form_Load做到这一点

A New Approach for Custom Colors in a Disabled VB.Net WinForms TextBox

这里的DisTextBox等级:

Public Class DisTextBox 
    Inherits System.Windows.Forms.TextBox 

    Private _ForeColorBackup As Color 
    Private _BackColorBackup As Color 
    Private _ColorsSaved As Boolean = False 
    Private _SettingColors As Boolean = False 

    Private _BackColorDisabled As Color = SystemColors.Control 
    Private _ForeColorDisabled As Color = SystemColors.WindowText 

    Private Const WM_ENABLE As Integer = &HA 

    Private Sub DisTextBox_VisibleChanged(sender As Object, e As System.EventArgs) Handles Me.VisibleChanged 
     If Not Me._ColorsSaved AndAlso Me.Visible Then 
      ' Save the ForeColor/BackColor so we can switch back to them later 
      _ForeColorBackup = Me.ForeColor 
      _BackColorBackup = Me.BackColor 
      _ColorsSaved = True 

      If Not Me.Enabled Then ' If the window starts out in a Disabled state... 
       ' Force the TextBox to initialize properly in an Enabled state, 
       ' then switch it back to a Disabled state 
       Me.Enabled = True 
       Me.Enabled = False 
      End If 

      SetColors() ' Change to the Enabled/Disabled colors specified by the user 
     End If 
    End Sub 

    Protected Overrides Sub OnForeColorChanged(e As System.EventArgs) 
     MyBase.OnForeColorChanged(e) 

     ' If the color is being set from OUTSIDE our control, 
     ' then save the current ForeColor and set the specified color 
     If Not _SettingColors Then 
      _ForeColorBackup = Me.ForeColor 
      SetColors() 
     End If 
    End Sub 

    Protected Overrides Sub OnBackColorChanged(e As System.EventArgs) 
     MyBase.OnBackColorChanged(e) 

     ' If the color is being set from OUTSIDE our control, 
     ' then save the current BackColor and set the specified color 
     If Not _SettingColors Then 
      _BackColorBackup = Me.BackColor 
      SetColors() 
     End If 
    End Sub 

    Private Sub SetColors() 
     ' Don't change colors until the original ones have been saved, 
     ' since we would lose what the original Enabled colors are supposed to be 
     If _ColorsSaved Then 
      _SettingColors = True 
      If Me.Enabled Then 
       Me.ForeColor = Me._ForeColorBackup 
       Me.BackColor = Me._BackColorBackup 
      Else 
       Me.ForeColor = Me.ForeColorDisabled 
       Me.BackColor = Me.BackColorDisabled 
      End If 
      _SettingColors = False 
     End If 
    End Sub 

    Protected Overrides Sub OnEnabledChanged(e As System.EventArgs) 
     MyBase.OnEnabledChanged(e) 

     SetColors() ' change colors whenever the Enabled() state changes 
    End Sub 

    Public Property BackColorDisabled() As System.Drawing.Color 
     Get 
      Return _BackColorDisabled 
     End Get 
     Set(ByVal Value As System.Drawing.Color) 
      If Not Value.Equals(Color.Empty) Then 
       _BackColorDisabled = Value 
      End If 
      SetColors() 
     End Set 
    End Property 

    Public Property ForeColorDisabled() As System.Drawing.Color 
     Get 
      Return _ForeColorDisabled 
     End Get 
     Set(ByVal Value As System.Drawing.Color) 
      If Not Value.Equals(Color.Empty) Then 
       _ForeColorDisabled = Value 
      End If 
      SetColors() 
     End Set 
    End Property 

    Protected Overrides ReadOnly Property CreateParams As System.Windows.Forms.CreateParams 
     Get 
      Dim cp As System.Windows.Forms.CreateParams 
      If Not Me.Enabled Then ' If the window starts out in a disabled state... 
       ' Prevent window being initialized in a disabled state: 
       Me.Enabled = True ' temporary ENABLED state 
       cp = MyBase.CreateParams ' create window in ENABLED state 
       Me.Enabled = False ' toggle it back to DISABLED state 
      Else 
       cp = MyBase.CreateParams 
      End If 
      Return cp 
     End Get 
    End Property 

    Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) 
     Select Case m.Msg 
      Case WM_ENABLE 
       ' Prevent the message from reaching the control, 
       ' so the colors don't get changed by the default procedure. 
       Exit Sub ' <-- suppress WM_ENABLE message 

     End Select 

     MyBase.WndProc(m) 
    End Sub 

End Class 

编译,你应该得到你的工具箱顶部的新控件。放置一个DisTextBox控件和您的窗体并将BackColorDisabled属性设置为White。现在的背景色应保持白色,当你将其禁用:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click 
    TextBox1.Enabled = Not TextBox1.Enabled 
    DisTextBox1.Enabled = Not DisTextBox1.Enabled 

    Label1.Text = TextBox1.Enabled.ToString 
    Label2.Text = DisTextBox1.Enabled.ToString 
End Sub 

截图...之上定期TextBox;底部的DisTextBox

启用:

The DisTextBox while Enabled

禁用:

The DisTextBox while Disabled with the BackColorDisabled Property set to White

+0

哇,太棒了!非常感谢。 –