Vb.net 使组合框在禁用时可见

Vb.net 使组合框在禁用时可见,vb.net,combobox,Vb.net,Combobox,我正在禁用VB.net中的combobox。 但在禁用模式下,它无法正常显示。 我试着改变背景色和前景色,但都不起作用 代码: cmbbox.BackColor = Color.FromName("Window") or cmbbox.ForeColor = Color.FromName("Window") 请帮忙 亲爱的亚当: 我正在将我的组件设置为enable false。但是我想使其可见。您可以引用链接。这就是我想要的,但在VB.Net中:要实现禁用组合框而不使其褪色,请首先将组合框的下

我正在禁用VB.net中的combobox。 但在禁用模式下,它无法正常显示。 我试着改变背景色和前景色,但都不起作用

代码:

cmbbox.BackColor = Color.FromName("Window")
or
cmbbox.ForeColor = Color.FromName("Window")
请帮忙

亲爱的亚当:
我正在将我的组件设置为enable false。但是我想使其可见。您可以引用链接。这就是我想要的,但在VB.Net中:

要实现禁用组合框而不使其褪色,请首先将组合框的下拉样式更改为DropDownList,然后调整事件以实现目标

下面是一段代码,您可以通过它实现相同的功能:

Public Class Form1
Dim selectindex As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ComboBox1.Items.Add("1")
    ComboBox1.Items.Add("2")
    ComboBox1.Items.Add("3")
    ComboBox1.Items.Add("4")
    selectindex = 3
    ComboBox1.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
    ComboBox1.SelectedIndex = selectindex
End Sub

Private Sub ComboBox1_SelectionChangeCommitted(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectionChangeCommitted
    ComboBox1.SelectedIndex = selectindex
End Sub
End Class
创建一个新的表单Form1并向表单中添加一个组合框,然后添加上面的代码以获得一个只读组合框。

看看哪个有一个只读组合框的解决方案,代码都是VB.NET

他们的代码版本如下。您需要将它放在您自己的类中,该类继承了
System.Windows.Forms.ComboBox

    Private _ReadOnly As Boolean = False

    Public Property [ReadOnly]() As Boolean    
        Get
            Return _ReadOnly
        End Get
        Set(ByVal Value As Boolean)
            _ReadOnly = Value
        End Set
    End Property

    Public Overrides Function PreProcessMessage(ByRef msg As Message) As Boolean
        'Prevent keyboard entry if control is ReadOnly  
        If _ReadOnly = True Then
            'Check if its a keydown message
            If msg.Msg = &H100 Then
                'Get the key that was pressed 
                Dim key As Int32 = msg.WParam.ToInt32
                'Ignore navigation keys 
                If key = Keys.Tab Or key = Keys.Left Or key = Keys.Right Then
                    'Do nothing    
                Else
                    Return True
                End If
            End If
        End If
        'Call base method so delegates receive event 
        Return MyBase.PreProcessMessage(msg)
    End Function

    Protected Overrides Sub WndProc(ByRef m As Message)
        'Prevent list displaying if ReadOnly
        If _ReadOnly = True Then
            If m.Msg = &H201 OrElse m.Msg = &H203 Then
                Return
            End If
        End If
        'Call base method so delegates receive event 
        MyBase.WndProc(m)
    End Sub

不久前,我一直在寻找同样的方法,并最终完成了以下工作。你可能不喜欢,但我会分享它以防万一。我使用
TableLayoutPanel
在表单上排列控件,然后交换所需控件的位置

例如,我创建了以下项目:

  • 表格布局面板1
    (两列三行)
  • TextBox1
    Read-only=True,BackColor=White
  • ComboBox1
    Visible=False,DropDownStyle=DropDownList,FlatStyle=Popup
  • 按钮1
    (命名为要更改)
  • 按钮2
    (命名为完成)->Visible=False
  • 这是我的密码:

    Public Class Form1
        Private Sub SwapControls(tlp As TableLayoutPanel, ctr1 As Control, ctr2 As Control)
            Dim ctl1pos As TableLayoutPanelCellPosition = tlp.GetPositionFromControl(ctr1)
            ctr1.Visible = False
            tlp.SetCellPosition(ctr1, tlp.GetPositionFromControl(ctr2))
            ctr2.Visible = True
            tlp.SetCellPosition(ctr2, ctl1pos)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            SwapControls(TableLayoutPanel1, TextBox1, ComboBox1)
            SwapControls(TableLayoutPanel1, Button1, Button2)
            Label1.Select()
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            SwapControls(TableLayoutPanel1, ComboBox1, TextBox1)
            SwapControls(TableLayoutPanel1, Button2, Button1)
            Label1.Select()
        End Sub
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label1.Select()
            ComboBox1.SelectedIndex = 0
    
            TextBox1.Text = ComboBox1.SelectedItem
        End Sub
    
        Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
            TextBox1.Text = ComboBox1.SelectedItem
        End Sub
    End Class
    

    您具体更改了组合框的哪些属性?实际上,当您禁用它时,它的外观就是这样。您是在修改
    组合框的
    已启用
    还是
    可见
    属性?将
    Visible
    设置为false将不允许用户查看表单上的控件。将
    Enabled
    设置为false将使控件可见,但不可用并“灰显”。从你的帖子来看,问题并不完全清楚,但除非你有其他要求,否则我认为你不应该修改控件的颜色。你是否尝试过将你提到的任何项目转换为VB.Net?是的,我尝试过。但无法转换