2012-02-06 63 views
0

我在VB.net中禁用組合框。 但在禁用模式下,它無法正常顯示。 我試着改變BackColor和ForeColor,但它不起作用。使組合框在禁用時可見

代碼:

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

請幫

親愛的亞當: 我在做我的組件啓用false.But我想讓它viewable.You可以reffer的link.This是exacly我但在VB.Net:A combobox that looks decent when it is disabled

+0

什麼屬性的組合框的你具體改變? – 2012-02-06 05:15:59

+0

好吧,這就是你實際禁用它時的樣子。 – Isuru 2012-02-06 05:33:47

+1

您是否嘗試將您提及的任何項目轉換爲VB.Net? – 2012-02-06 06:54:28

回答

0

要實現禁用組合框沒有淡化它,首先將組合框的下拉樣式更改爲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和組合框添加到窗體,然後添加上面的代碼獲得一個只讀組合框。

0

看看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 
0

我一直在尋找同樣不久前,最終執行以下操作。你可能不喜歡它,但我會分享它以防萬一。我使用TableLayoutPanel來安排表單上的控件,然後交換所需控件的位置。

比如我創建了以下項目:

Form1 Design

  1. TableLayoutPanel1(兩列,三列)
  2. TextBox1只讀=真,背景色=白色
  3. ComboBox1 Visible = False,DropDownStyle = DropDownList,FlatStyle = Popup
  4. Button1 (將其命名爲更改)
  5. Button2(其命名爲Done) - >可見=假

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