Reputation: 67
I know the command in WPF but what is its equivalent in WinForms?
cboclient.IsHitTestVisible = false;
cboclient.Focusable = false;
Using this command the combo-box is not disabled but the user can't open it for reading the data. How I can accomplish this in WinForms? thanks
Details: I have 3 combobox on my form when the form initially loads only the third combobox can not be opened for reading data. When the user selects a value in the 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 the third combobox. Because it will give the user the a false expression.
Upvotes: 0
Views: 243
Reputation: 1668
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;
Upvotes: 0
Reputation: 63327
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;
Upvotes: 1
Reputation: 2136
You can use if statement on OnSelectionChangedSelectionChanged event.
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//here your if statement
}
Upvotes: 0