Disable/enable controls

G

Guest

On a form, when I want to disable a textbox, but leave the text readable, I set the ReadOnly flag to true. Works great.

However, I don't have this option with combo-boxes. When a combo-box has Enabled = False, the text becomes unreadable. Is there a way to override the complete 'grey-out' effect by changing Forecolor/backcolor/font?

--Billgs
 
K

Klaus H. Probst

Well, you could use something like this, but it has a wacky effect. It sets
the text area to be read-only but for some reason it grays it and still
allows the user to pick an item so it's almost like a dropdown list instead
of a normal combo.

using System.Runtime.InteropServices;
using System.Text;
using System.Drawing;

namespace System.Windows.Forms
{

public struct ComboBoxUtils
{

[StructLayout(LayoutKind.Sequential)]
private struct COMBOBOXINFO
{
public int cbSize;
public Rectangle rcItem;
public Rectangle rcButton;
public int stateButton;
public IntPtr hwndCombo;
public IntPtr hwndItem;
public IntPtr hwndList;
}

private static int EM_SETREADONLY = 0xCF;

[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int SendMessage(IntPtr hWnd, int Msg, int wParam,
IntPtr lParam);

[DllImport("user32", CharSet = CharSet.Auto)]
private static extern int GetComboBoxInfo(IntPtr hwndCombo, ref
COMBOBOXINFO pcbi);

public static void SetReadonly(ComboBox combo, bool readOnly)
{
COMBOBOXINFO cbbi = new COMBOBOXINFO();

cbbi.cbSize = Marshal.SizeOf(cbbi);
int result = GetComboBoxInfo(combo.Handle, ref cbbi);

if (result != 0)
SendMessage(cbbi.hwndItem, EM_SETREADONLY, ( readOnly ? 1 : 0),
IntPtr.Zero);

}

}

}

Placing a combo inside a panel and then disabling the panel itself has the
effect of also disabling the combo. Weird.

I recommend VB6. Just put it in a picture box and disable it <g>


--
____________________
Klaus H. Probst, MVP
http://www.vbbox.com/

Billgsd said:
On a form, when I want to disable a textbox, but leave the text readable,
I set the ReadOnly flag to true. Works great.
However, I don't have this option with combo-boxes. When a combo-box has
Enabled = False, the text becomes unreadable. Is there a way to override
the complete 'grey-out' effect by changing Forecolor/backcolor/font?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top