Vb.net 将textbox的enabled属性设置为false,并将前景色设置为白色
我正在使用windows窗体。textbox1.enabled=false的默认前景色为灰色,如何将其更改为白色?如果WinForms执行此操作:Vb.net 将textbox的enabled属性设置为false,并将前景色设置为白色,vb.net,Vb.net,我正在使用windows窗体。textbox1.enabled=false的默认前景色为灰色,如何将其更改为白色?如果WinForms执行此操作: Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs) TextBox1.ForeColor = Color.White End Sub 然后在Form_Load上执行以下操作: TextBox1.Enabled = false; 如果WinForms
Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
TextBox1.ForeColor = Color.White
End Sub
然后在Form_Load上执行以下操作:
TextBox1.Enabled = false;
如果WinForms执行此操作:
Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
TextBox1.ForeColor = Color.White
End Sub
然后在Form_Load上执行以下操作:
TextBox1.Enabled = false;
如果WinForms执行此操作:
Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
TextBox1.ForeColor = Color.White
End Sub
然后在Form_Load上执行以下操作:
TextBox1.Enabled = false;
如果WinForms执行此操作:
Protected Sub TextBox1_EnabledChanged(sender As Object, e As EventArgs)
TextBox1.ForeColor = Color.White
End Sub
然后在Form_Load上执行以下操作:
TextBox1.Enabled = false;
从我在专家交流上的文章: 下面是
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
启用:
残疾人士:
来自我在专家交流上的文章: 下面是
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
启用:
残疾人士:
来自我在专家交流上的文章: 下面是
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
启用:
残疾人士:
来自我在专家交流上的文章: 下面是
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
启用:
残疾人士:
哦,对不起。我的标签是vb.net:(我在工具箱中看不到新的文本框,它的名称是不是
WhiteForegroundTextbox
?只有在使用WebForms时才需要它,我在回答中指定了这一点。现在我看到您使用的是WinForms,您就使用上面的代码,因为WinForms有一个EnabledChanged事件。这对我不起作用:Windows 10,VS Community 2015。TextBox禁用,但颜色不会变为白色。您在WinForms中测试过吗?…是的,这应该适用于Windows 10和WinForms。您不能仅复制和粘贴EnabledChanged事件方法,您必须在设计器中选择文本框,然后进入属性,单击事件按钮(闪电)然后找到EnabledChanged事件并双击它,它会将该事件添加到您的代码中,然后您粘贴到您的TextBoxName.ForeColor=Color.White.噢,对不起。我的标记是vb.net:(我在工具箱中看不到新的文本框,它的名称是不是WhiteForegroundTextbox
?只有在使用WebForms时才需要它,我在回答中指定了这一点。现在我看到您使用的是WinForms,您就使用上面的代码,因为WinForms有一个EnabledChanged事件。这对我不起作用:Windows 10,VS Community 2015。TextBox禁用,但颜色不会变为白色。您在WinForms中测试过吗?…是的,这应该适用于Windows 10和WinForms。您不能仅复制和粘贴EnabledChanged事件方法,您必须在设计器中选择文本框,然后进入属性,单击事件按钮(闪电)然后找到EnabledChanged事件并双击它,它会将该事件添加到您的代码中,然后您粘贴到您的TextBoxName.ForeColor=Color.White.噢,对不起。我的标记是vb.net:(我在工具箱中看不到新的文本框,它的名称是不是WhiteForegroundTextbox
?只有在使用WebForms时才需要它,我在回答中指定了这一点。现在我看到您使用的是WinForms,您就使用上面的代码,因为WinForms有一个EnabledChanged事件。这对我不起作用:Windows 10,VS Community 2015。TextBox禁用,但颜色不会变为白色。您在WinForms中测试过吗?…是的,这应该适用于Windows 10和WinForms。您不能仅复制和粘贴EnabledChanged事件方法,您必须在设计器中选择文本框,然后进入属性,单击事件按钮(闪电)然后找到EnabledChanged事件并双击它,它会将该事件添加到您的代码中,然后您粘贴到您的TextBoxName.ForeColor=Color.White.噢,对不起。我的标记是vb.net:(我在工具箱中看不到新的文本框,它的名称是不是WhiteForegroundTextbox
?只有在使用WebForms时才需要它,我在回答中指定了这一点。现在我看到您使用的是WinForms,您就使用上面的代码,因为WinForms有一个EnabledChanged事件。这对我不起作用:Windows 10,VS Community 2015。TextBox禁用,但颜色不会变为白色。您在WinForms中测试过吗?…是的,这应该适用于Windows 10和WinForms。您不能仅复制和粘贴EnabledChanged事件方法,您必须在设计器中选择文本框,然后进入属性,单击事件按钮(闪电)然后找到EnabledChanged事件并双击它,它会将该事件添加到你的代码中,然后你粘贴到你的TextBoxName.ForeColor=Color.White.WinForms?WebForms?…还有什么?@Idle\u Mind,我正在使用windows窗体。谢谢。我在www.Experts-Exchange.com上写了一篇关于这个主题的文章:你找到了这个问题的答案吗有问题吗?@thewisegod,我还没有找到这个问题的答案。WinForms?WebForms?…还有什么?@Idle\u Mind,我正在使用windows窗体。谢谢。我已经在www.Experts-Exchange.com上写了一篇关于这个主题的文章:你找到这个问题的答案了吗?@thewisegod,我还没有找到这个问题的答案。WinForms?WebForms?。…还有什么?@Idle\u Mind,我在使用windows窗体。谢谢。我在www.Experts-Exchange.com上写了一篇关于这个主题的文章:你找到这个问题的答案了吗?@thewisegod,我还没有找到这个问题的答案。WinForms?WebForms?…还有什么?@Idle\u Mind,我在使用windows窗体。谢谢。我写了一篇关于t的文章请访问www.Experts-Exchange.com:你找到这个问题的答案了吗?@thewisegod,我还没有找到这个问题的答案。哇,太好了!非常感谢。哇,这个我