我正在使用Windows窗体。 textbox1.enabled = false的默认前景是灰色的,我怎样才能将它改为白色?将文本框的启用属性设置为false并将forecolor设置为白色
回答
如果WinForms的做到这一点:在专家交换
TextBox1.Enabled = false;
哦,对不起。我的标签是vb.net。 :( –
我不能在工具箱中看到新的文本框,它的名称是否是'WhiteForegroundTextbox'? –
如果您使用的是WebForms,我只需要这个,我在我的答案中指定了这一点,现在我看到您正在使用WinForms您使用上面的代码,因为WinForms有一个EnabledChanged事件。 – thewisegod
从我的文章:
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
。
启用:
禁用:
哇,太棒了!非常感谢。 –
- 1. 将LinkLabel1.Enabled属性设置为false(vb.net)
- 2. 如何将DataTrigger设置为文本框“文本”属性?
- 3. 将属性设置为com.google.appengine.api.datastore.Entity
- 4. 将属性设置为类
- 5. 将自动启动设置为false的驼峰路由设置为false
- 6. EF 5将不会将IsIdentity的PK属性设置为false
- 7. BaseAdapter:将hasStableIds()设置为false?
- 8. 将Page.IsValid设置为False
- 9. 将WCF设置为nillable = false
- 10. 将RGB白色设置为透明?
- 11. xslt:将只读属性设置为文本框
- 12. 将禁用的属性设置为true或false switchery
- 13. 将文本框设置为文件名
- 14. 将属性设置为最后位置
- 15. 滚动文本框并设置属性
- 16. VB.NET禁用ListView而不将Enable属性设置为false
- 17. 将文档(表单) - EditMode设置为false
- 18. Pascal - 将背景设置为白色(不是灰色,纯白色)
- 19. 将评分值设置为文本框
- 20. 将文本框设置为必填
- 21. 将焦点设置为文本框
- 22. 将最小值设置为文本框
- 23. 将用户属性设置为firebase-admin
- 24. 获取将ModelState的IsValid设置为false的验证属性
- 25. mustUnderstand属性不只是设置为false。
- 26. UseCompatibleTextRendering属性时,它被设置为false
- 27. 将文本设置为textView
- 28. 将文本设置为ContentView
- 29. 为什么将html属性设置为false会将其视为意外标记?
- 30. 将Asp.net ImageButton设置为visible = false。仍然显示灰色边框
的WinForms? WebForms?...别的东西? –
@Idle_Mind,我使用Windows窗体。谢谢。 –
我已经在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) –