简体   繁体   中英

How to disable read operation from combobox in winform?

I know the command in wpf but what is its equivalent in winform ?

        cboclient.IsHitTestVisible = false;
        cboclient.Focusable = false;

Using this command the combo-box is not disabled but user cant open it for reading the data .How i can accomplish this in winfrom? thanks

Details :I have 3 combobox on my form when the form initially loads the only third combobox can not be opened for reading data. When user select value in first two combobox then based on those two values the third combobox is enabled to display data from db .

Note : Here i don't want to disable third combobox. Because it will give user the false expression.

You can catch the message WM_MOUSEACTIVATE and discard it to prevent user from focusing the combobox by mouse and also prevent hittesting. Catch the message WM_SETFOCUS to prevent user from focusing the combobox by keyboard. Try this code:

public class ComboBoxEx : ComboBox
{    
    public ComboBoxEx(){
        IsHitTestVisible = true;
    }
    public bool IsHitTestVisible { get; set; }
    public bool ReadOnly { get; set; }
    protected override void WndProc(ref Message m)
    {            
        if (!IsHitTestVisible)
        {
            if (m.Msg == 0x21)//WM_MOUSEACTIVATE = 0x21
            {
                m.Result = (IntPtr)4;//no activation and discard mouse message
                return;
            }
            //WM_MOUSEMOVE = 0x200, WM_LBUTTONUP = 0x202
            if (m.Msg == 0x200 || m.Msg == 0x202) return;
        }
        //WM_SETFOCUS = 0x7
        if (ReadOnly && m.Msg == 0x7) return;
        base.WndProc(ref m);
    }
    //Discard key messages
    public override bool PreProcessMessage(ref Message msg)
    {
        if (ReadOnly) return true;
        return base.PreProcessMessage(ref msg);
    }
}
//Usage
comboBoxEx1.ReadOnly = true;
comboBoxEx1.IsHitTestVisible = false;

You can use following code:

cboclient.DropDownStyle = ComboBoxStyle.DropDownList;
cboclient.DropDownHeight = 1;
cboclient.DropDownWidth = 1;
cboclient.TabStop = false;

for displaying combobox as a readonly one you can use:

cboclient.FlatStyle = FlatStyle.Popup;

or

cboclient.FlatStyle = FlatStyle.Flat;

You can use if statement on OnSelectionChangedSelectionChanged event.

  private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
      //here your if statement
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM