【问题标题】:Making combobox visible when it is disabled禁用时使组合框可见
【发布时间】:2012-02-06 05:07:45
【问题描述】:

我正在禁用 VB.net 中的组合框。 但在禁用模式下,它无法正常显示。 我尝试同时更改 BackColor 和 ForeColor 但它不起作用。

代码:

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

请帮忙

亲爱的亚当: 我正在让我的组件启用 false。但我想让它可见。你可以引用链接。这正是我想要的,但在 VB.Net 中:A combobox that looks decent when it is disabled

【问题讨论】:

  • 你具体改变了组合框的什么属性?
  • 嗯,这就是你实际禁用它时的样子。
  • 您是否正在修改ComboBoxEnabledVisible 属性?将 Visible 设置为 false 将不允许用户查看表单上的控件。将 Enabled 设置为 false 会使控件可见,但无法使用并“变灰”。从您的帖子中,尚不完全清楚问题出在哪里,但是除非您有其他要求,否则我会说您不必修改控件的颜色。
  • 您是否尝试过将您提到的任何项目转换为 VB.Net?
  • 是的,我试过了。但无法转换

标签: vb.net combobox


【解决方案1】:

要实现禁用组合框而不使其褪色,首先将组合框的下拉样式更改为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并在表单中添加一个combobox,然后添加上面的代码得到一个readonly的combobox。

【讨论】:

    【解决方案2】:

    看看this thread,它有一个只读组合框的解决方案,代码都是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
    

    【讨论】:

      【解决方案3】:

      不久前我一直在寻找相同的东西,最后做了以下事情。你可能不喜欢它,但我会分享它以防万一。我正在使用TableLayoutPanel 在表单上安排我的控件,然后我正在交换所需控件的位置。

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

      Form1 Design

      1. TableLayoutPanel1(两列三行)
      2. TextBox1 只读 = True,BackColor = 白色
      3. ComboBox1 Visible = False,DropDownStyle = DropDownList,FlatStyle = Popup
      4. Button1(将其命名为更改)
      5. Button2(将其命名为 Done)-> Visible = False

      Runtime - Screenshots

      这是我的代码:

      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
      

      【讨论】:

        猜你喜欢
        • 2021-01-22
        • 2021-11-03
        • 1970-01-01
        • 1970-01-01
        • 2019-02-03
        • 2015-02-10
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多