 |

|
You must also stop the user from pasting into the control by intercepting the right-mouse click event.
|
|
|
|

|
I can see that the download does not include at least one of the fixes described in the comments. Is there a place to get the latest code?
Thanks.
|
|
|
|

|
I'm new to C# so maybe I'm missing something, but I solved this problem by just adding a keypress event handler. It seems to work fine for me.
private void unitComboBox_KeyPress(object sender, KeyPressEventArgs e)
{
e.KeyChar = (char)0x00;
}
-Jeff
|
|
|
|

|
This is what I needed. Thanks a lot !!
Jose Luis.
|
|
|
|

|
Hi!
You have in your "Todo" 'Remove the White Border'. Well, I don't get it done? So how to remove that white Border as I want my ReadOnlyComboBox look like a Label?
thanks for help
|
|
|
|
|

|
I tried your version, Claudio, because I liked the idea of swapping in the read only text box. However, it does not work well with a TableFlowLayoutPanel. The extra control (the text box) gets added into its own cell upon "_textBox.Parent = ...". Unless there is a way to fix that problem, the combo needs to work by itself (or be incompatible with layout panels).
|
|
|
|

|
After I saw this article, I thought there might be a much easier way to accomplish this goal: set the Enabled property to false and the fore and back colors to their appropriate read only colors. If it worked, it would be really simple and the technique would work for any control. Unfortunately, the ComboBox ignores the fore and back colors when the ComboBox is disabled. Here's the code I tried: Private _readOnly As Boolean Private _savedEnabled As Boolean Private _savedBackColor As Color Private _savedForeColor As Color Public Property [ReadOnly]() As Boolean Get Return _readOnly End Get Set(ByVal Value As Boolean) 'if the property value changed If Value <> _readOnly Then If Value Then _savedEnabled = Me.Enabled _savedBackColor = Me.BackColor _savedForeColor = Me.ForeColor Me.Enabled = False Me.BackColor = SystemColors.Control Me.ForeColor = SystemColors.WindowText Else Me.Enabled = _savedEnabled Me.BackColor = _savedBackColor Me.ForeColor = _savedForeColor End If _readOnly = Value End If End Set End Property It's too bad this doesn't work. I wonder if there's anyway to force the fore and back colors when the ComboBox is disabled.
|
|
|
|

|
I seem to have discovered an issue. Hoping you can help me solve it. In the native ComboBox control, when it is set for 'DropDownList' style and you press a key, it jumps to the first entry with that letter (and then the second, third, fourth, etc.. entry as you keep pressing the same button). However, with this Read Only ComboBox, I seem to be able to type data into the edit part. Is there a way to regain the Native Combobox DropDownList functionality while retaining the Read-Only-ability of this control?
Thanks.
|
|
|
|

|
Figured it out. I was setting the Style at Design time and it was not being picked up by the base class. If you set it at run-time, it works correctly. Alternatively, you can do some processing in the Constructor.
|
|
|
|

|
Do you use my last uploaded source?
In last version, data typed into the edit part is discarded if ReadOnly property is set to true, even for 'DropDownList' style and typing first character of items.
However I’m not sure that I truly understand your question.
If I can help you more I will try if you give me more details.
|
|
|
|

|
Yeah. I did not explain my problem very well and then I figured it out.
I was setting the ComboBoxStyle to 'DropDownList' at Design-Time. The problem occurred when the ReadOnly ComboBox was set ReadOnly = False (also at Design-Time). The Base Class (which defaults ComboBoxStyle to 'DropDown') was not picking up that I had set the Style to 'DropDownList' at Design-Time. If you set the Style to 'DropDownList' at Run-Time, it works fine. I put an extra line in the constructor to pass the Style to the base class. This fixed the problem. There is probably a more robust solution (like checking the read-only property, etc.) but this works for me.
Thank you for your response.
|
|
|
|

|
You can also use this
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
|
|
|
|

|
Yes but this is not read only . This restricts the user to select only one value from a list o values. Read only restricts the user to change actual selected value.
|
|
|
|

|
Nice Control. It is just what my program needed. I don't think some people understand the purpose of this control (i.e. Giving people the ability to easily read the value in the ComboBox but not change the value), but it is perfect for what I'm doing.
Thank you again.
|
|
|
|
|

|
Hi, good article but I have one remark. If this control must be complete,
you must foresee all situations of binding. For example if you'r combo is bound by "Text" property to some datasource (for example datatable or dataview or ...) it will reflect on the data in datasource.
Boyan Botev - FreeStyle557
|
|
|
|

|
still the RMB menu option cut is enabled which allows you to delete tht contents which makes this not readonly
|
|
|
|

|
You are absolutely right;) … but if you’re look at my previous replies you’ll see that I specify the need to override right click event to block menu or display custom menu.
Anyway, next days, when I’ll have enough free time I will update my article with full functional code .
|
|
|
|

|
Why block the RMB menu; isn't there a way just to block the Cut & Undo options.
|
|
|
|

|
Why block the RMB menu; isn't there a way just to block the Cut & Undo options.
|
|
|
|

|
if you change it to DropDownList you will get the same result if the comboBox had a ReadOnly property... or is it?
|
|
|
|

|
No, DropDownList it's not the same as ReadOnly because you can change actual value by selecting from the list.
ReadOnly means that you can't change selected value.
|
|
|
|

|
using System;
using System.ComponentModel;
namespace YControls
{
///
/// ReadOnly ComboBox.
/// DropDownList can open, but selection can't be changed.
///
public class ReadOnlyCombo : System.Windows.Forms.ComboBox
{
private bool readOnly;
[Description("DropDownList can open, but selection can't be changed.")]
[Category("Behavior")]
[DefaultValue(false)]
public bool ReadOnly
{
get { return readOnly; }
set { readOnly = value;}
}
private int selectedIndex;
protected override void OnSelectedIndexChanged(System.EventArgs e)
{
if(this.ReadOnly)
SelectedIndex = selectedIndex;
else
{
base.OnSelectedIndexChanged (e);
selectedIndex = SelectedIndex;
}
}
}
}
|
|
|
|

|
The combo box still drops down when pressing F4 and changes on PageDown/PageUp.
OnKeyDown has to be changed as followed
protected override void OnKeyDown(KeyEventArgs e) {
if (this.ReadOnly
&& ( e.KeyCode == Keys.Up
|| e.KeyCode == Keys.Down
|| e.KeyCode == Keys.Delete
|| e.KeyCode == Keys.F4
|| e.KeyCode == Keys.PageDown
|| e.KeyCode == Keys.PageUp)) {
e.Handled = true;
} else {
base.OnKeyDown (e);
}
}
|
|
|
|

|
You are absolutely right. Also needs to handle Home and End keys.
protected override void OnKeyDown(KeyEventArgs e)
{
if(this.ReadOnly &&
(e.KeyCode == Keys.Up ||
e.KeyCode == Keys.Down ||
e.KeyCode == Keys.PageUp ||
e.KeyCode == Keys.PageDown ||
e.KeyCode == Keys.Delete ||
e.KeyCode == Keys.F4 ||
e.KeyCode == Keys.End ||
e.KeyCode == Keys.Home))
e.Handled = true;
else
base.OnKeyDown (e);
}
Dan Anatoli
|
|
|
|

|
and left and right keys in the drop down list mode if ( this.ReadOnly && ( e.KeyCode == Keys.Up || e.KeyCode == Keys.Down || e.KeyCode == Keys.Delete || Keys.PageUp == e.KeyCode || Keys.PageDown == e.KeyCode || Keys.F4 == e.KeyCode )) e.Handled = true; else if ( this.ReadOnly && (( Keys.Left == e.KeyCode ) || ( Keys.Right == e.KeyCode )) && this.DropDownStyle == ComboBoxStyle.DropDownList ) e.Handled = true; else base.OnKeyDown( e ); edokan
|
|
|
|

|
right click on the text and choose Cut from the context menu...
markus
|
|
|
|
|

|
The following will make a functional ComboBox with readonly.
1) Create a control based on the ComboBox.
2) Define in the class the following:
[DllImport("USER32.DLL", EntryPoint="EnableWindow")]
public static extern int EnableWindow( int hwnd, int enable );
3) Add a Boolean set/get called ReadOnly with the following:
private bool m_ReadOnly = false;
public bool ReadOnly
{
get { return m_ReadOnly; }
set
{
m_ReadOnly = value;
EnableWindow( this.Handle.ToInt32(), value ? 1 : 0);
}
}
That's it.. don't worry about using the P/Invoke functionality, you're already using System.Windows.Forms which is not cross platform.
Regards,
Ray
|
|
|
|

|
Ray Hayes wrote:
The following will make a functional ComboBox with readonly.
1) Create a control based on the ComboBox.
2) Define in the class the following:
[DllImport("USER32.DLL", EntryPoint="EnableWindow")]
public static extern int EnableWindow( int hwnd, int enable );
3) Add a Boolean set/get called ReadOnly with the following:
private bool m_ReadOnly = false;
public bool ReadOnly
{
get { return m_ReadOnly; }
set
{
m_ReadOnly = value;
EnableWindow( this.Handle.ToInt32(), value ? 1 : 0);
}
}
That's it.. don't worry about using the P/Invoke functionality, you're already using System.Windows.Forms which is not cross platform.
Regards,
Ray
There is already a property for what you want to do: Enabled.
Your sample is the hard way to mimic Enabled property of ComboBox control.
The subject of my article is how to make an enabled combobox to readonly: Enabled = true and ReadOnly = true.
Dan Anatoli
|
|
|
|

|
myReadOnlyComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList
The "ComboBoxStyle.DropDownList" style makes it.
|
|
|
|

|
I already thought of your way to solve the problem. i dont know why this article has been written for. but instead of pinvoke you can just use
CBox.Enabled = false;
thats it
|
|
|
|

|
i could not make it work.....
it is me, can anyone write an idiots guide to how to make it possible to add such an readonly ComboBox to a form?
i made a .cs file but when i tryied to use it in a different form, i didn't know how to do it.
i made an objekt af classen NewComboBox and gave it cooridnates but nothing happend.....
any ideas?
please.....this could be very useful to me....
thanx
|
|
|
|

|
I sent a sample to CodeProject about ReadOnly Combobox. Soon you'l be able to download it.
Dan Anatoli
|
|
|
|
|

|
just as :
the treeview
the listview
the textbox
the scrollbar
ok some might have a onpaint in .net but none of them can be used to anything since all those controls are drawn by the system ... treeviews and listviews support custom drawing if you do some major api fiddling but none supports it right out of the box in .net
//Roger
|
|
|
|

|
It's because you can override theses controls Paint event not for the controls themselves but for their items..
Good luck
|
|
|
|
|
|

|
Why not just disable it then if you don't want the user to change the selected value?
Your way, the user may be confused since he/she would have no visible clue as to why they cannot change the value. Disabling it would make that clear.
Another question is whether or not the combobox would be the right control to use in the way you are suggesting.
|
|
|
|

|
>>Why not just disable it then if you don't want the user to change the selected value?
Because of the aspect of disabled controls and because you can't select an disabled control. What if you want to show the data in readonly maner and if a user want to change some data must click on a button (if have rights)? Disabled controls is not so easy to read for some person. You like a form with 90% controls disabled, or some read only and some disabled?
In this case, please tell me for what reason the TextBox control has both properties: Enabled and ReadOnly?
>>Your way, the user may be confused since he/she would have no visible clue as to why they cannot change the value. Disabling it would make that clear.
It's so easy to implemet a change in BackColor/ForeColor in case of ReadOnly=true. Simple override the BackColor and ForeColor properties in derived control for your needs.
>>Another question is whether or not the combobox would be the right control to use in the way you are suggesting.
I want to display data and also I want to allow to some people to change this data. Application needs IDs but user needs to see details. It's absolutely the right control for my needs but it's not the perfect.
|
|
|
|

|
I still don't think your design is a good design... Why the Combo box appear to be changeable but user can't change it??? They would think that there is something wrong with the program, and not because they don't have permission.
Revisit your design.
|
|
|
|

|
Anonymous wrote:
I still don't think your design is a good design...
I respect your opinion but for my needs this design, including back/fore color reflecting read only state, works very well. My article is only about how to create a ReadOnly property for ComboBox control, just like ReadOnly property of a TextBox. I provided code only to solve the absence of ReadOnly property.
Anonymous wrote:
Why the Combo box appear to be changeable but user can't change it???
You can go further and modify ReadOnly property to reflect the state of the control. ComboBox control can be enhanced for your needs.
Anonymous wrote:
They would think that there is something wrong with the program, and not because they don't have permission.
My actual enhanced ComboBox reflects read only state. In my forms read only combo boxes don’t appears to be changeable and looks like text boxes. All read only controls have the same color, different than editable controls.
In some forms contexts I don’t like disabled controls. This is the reason for what I need read only property for ComboBox control.
|
|
|
|
 |