4
I’m trying to get a combobox
read only (as the option ReadOnly
of textbox
), tampering with the properties of the object, but I’m not succeeding.
The combobox is in a common windows form. It will be used to select specific options during a people registration. When registering, they will work normally. But there will also be the option to search a registration, so there are no duplicates and the searched registration can be opened to check the completion.
I would like, when opening a register that has already been made, the fields that are comboboxes are not editable as in comboBox.Enabled=False
, but that the option that was registered is selectable, to be copied if necessary, as shown below:
Editing:
Let me give you an example to make it clear.
I’ll create Mr. Xisto’s record. During the creation of the registration, the sex combo (for example) when being clicked allows you to enter something or, if the user prefers, you can select the option from the list that is shown by clicking the down arrow. After filling in all fields, the registration is finalized and saved in the database.
When I search Mr. Xisto’s record, I can open it and check the data that was typed. When I check the sex combo, the information that was registered will be imported from the database.
If I click this combo to show the options or try to write something, nothing will happen (as it happens when it is disabled). But the information that was imported from the database can be highlighted (turning blue, as in the image) to be copied elsewhere.
Edition 02:
Following @Gabriel’s reasoning, I’m almost getting to leave as I’d like.
Already leaving combos as "read only".
I did a separate project, to test and not give problem and I arrived at the code below.
The combos are already changing colors, not showing the DropDown
, not allowing typing, but allowing you to select what’s in it. I just need to do the ctrl+c
function, when they are like "read only".
With all these locks I put, it would still be possible to enable the ctrl+c
?.
Edition 03:
I managed to implement the ctrl+c
.I am putting down the final version of the code that leaves the ComboBox
as "read only".
bool consulta;
void Button1Click(object sender, EventArgs e)
{
if (consulta==true)
{
consulta = false;
mudaCorComboEdropDownHeight(Color.White,106);
}
else
{
consulta = true;
mudaCorComboEdropDownHeight(Color.WhiteSmoke,1);
}
}
void MainFormLoad(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is ComboBox )
{
c.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
if (c is GroupBox)
{
foreach (Control c2 in c.Controls)
{
if (c2 is ComboBox )
{
c2.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c2.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
if (c2 is TabControl)
{
foreach (Control c3 in c2.Controls)
{
if (c3 is TabPage )
{
foreach (Control c4 in c3.Controls)
{
if (c4 is ComboBox )
{
c4.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c4.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
if (c4 is GroupBox)
{
foreach (Control c5 in c4.Controls)
{
if (c5 is ComboBox )
{
c5.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c5.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
}
}
}
}
if (c3 is ComboBox )
{
c3.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c3.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
}
}
}
}
if (c is TabControl)
{
foreach (Control c2 in c.Controls)
{
if (c2 is TabPage )
{
foreach (Control c3 in c2.Controls)
{
if (c3 is ComboBox )
{
c3.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c3.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
}
}
if (c2 is ComboBox )
{
c2.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c2.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
if (c2 is GroupBox)
{
foreach (Control c3 in c2.Controls)
{
if (c3 is ComboBox )
{
c3.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c3.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
}
}
}
}
}
}
void BloqueiaTodosCombobox(object sender, KeyPressEventArgs e)
{
if(consulta == true)
{
e.Handled = true;
}
else
{
e.Handled = false;
}
}
void BloqueiaSetasCombobox(object sender, KeyEventArgs f)
{
if (!(f.KeyCode==Keys.Left | f.KeyCode==Keys.Right | f.KeyCode==Keys.Home | f.KeyCode==Keys.End | (f.KeyCode == Keys.C && f.Modifiers== Keys.Control)))
{
f.Handled=true;
}
else
{
f.Handled=false;
if (f.KeyCode == Keys.C && f.Modifiers== Keys.Control)
{
Clipboard.SetText(ActiveControl.Text);
}
}
}
void bloqueiaDropDown(ComboBox combo, int alturaDropDown)
{
combo.DropDownHeight=alturaDropDown;
}
void mudaCorComboEdropDownHeight(Color cor, int alturaDropDown)
{
foreach (Control c in this.Controls)
{
if (c is ComboBox )
{
c.BackColor=cor;
bloqueiaDropDown ((ComboBox)c, alturaDropDown);
}
if (c is GroupBox)
{
foreach (Control c2 in c.Controls)
{
if (c2 is ComboBox )
{
c2.BackColor=cor;
bloqueiaDropDown ((ComboBox)c2, alturaDropDown);
}
if (c2 is TabControl)
{
foreach (Control c3 in c2.Controls)
{
if (c3 is TabPage )
{
foreach (Control c4 in c3.Controls)
{
if (c4 is ComboBox )
{
c4.BackColor=cor;
bloqueiaDropDown ((ComboBox)c4, alturaDropDown);
}
if (c4 is GroupBox)
{
foreach (Control c5 in c4.Controls)
{
if (c5 is ComboBox )
{
c5.BackColor=cor;
bloqueiaDropDown ((ComboBox)c5, alturaDropDown);
}
}
}
}
}
if (c3 is ComboBox )
{
c3.BackColor=cor;
bloqueiaDropDown ((ComboBox)c3, alturaDropDown);
}
}
}
}
}
if (c is TabControl)
{
foreach (Control c2 in c.Controls)
{
if (c2 is TabPage )
{
foreach (Control c3 in c2.Controls)
{
if (c3 is ComboBox )
{
c3.BackColor=cor;
bloqueiaDropDown ((ComboBox)c3, alturaDropDown);
}
}
}
if (c2 is ComboBox )
{
c2.BackColor=cor;
bloqueiaDropDown ((ComboBox)c2, alturaDropDown);
}
if (c2 is GroupBox)
{
foreach (Control c3 in c2.Controls)
{
if (c3 is ComboBox )
{
c3.BackColor=cor;
bloqueiaDropDown ((ComboBox)c3, alturaDropDown);
}
}
}
}
}
}
}
Edition 04:
As the code posted is very specific to my case, I will post below a more generic explanation so that other people can better understand and use.
Once the Form is loaded, a loop is executed to assign a method to all Events KeyPress
and KeyDown
of all the ComboBox
. If the ComboBox
is within another control, as GroupBox
, TabControl e TabPage
or some other, the loop should be chained.
foreach (Control c in this.Controls)
{
if (c is ComboBox )
{
//associa o Evento KeyPress da ComboBox localizada com
// o Método 'BloqueiaTodosCombobox'
c.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
//associa o Evento KeyDown da ComboBox localizada com
// o Método 'BloqueiaSetasCombobox'
c.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
if (c is GroupBox)
{
//Caso o ComboBox esteja dentro de um GroupBox,
//fazer o mesmo 'foreach' para buscar o ComboBox
}
if (c is TabControl)
{
foreach (Control c2 in c.Controls)
{
if (c2 is TabPage )
{
//Caso o ComboBox esteja dentro de um TabPage do TabControl,
//fazer o mesmo 'foreach' para buscar o ComboBox.
//Basta ir encadeando os foreach para percorrer todos os Controles
//necessários, a fim de buscar o ComboBox.
}
if (c2 is ComboBox )
{
c2.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
c2.KeyDown += new KeyEventHandler (BloqueiaSetasCombobox);
}
}
}
}
Because of the locks that were programmed, in order to not allow anything to be typed when it is read-only, Ctrl+C stopped working. So it was necessary to do it manually.
//Caso tenha sido apertado Ctrl+C
if (f.KeyCode == Keys.C && f.Modifiers== Keys.Control)
{
//Manda o conteúdo do Controle ativo (o que está clicado)
//para o Clipboard, para que possa ser colado com o Ctrl+V
//em outro lugar que for necessário
Clipboard.SetText(ActiveControl.Text);
}
But you want to prevent the person from choosing any element in the
comboBox
? or prevent the person from writing options that do not exist ?– Isac
When it is read-only I want you not to be able to write anything at all, just select what was chosen at the time of registration. At the time of registration there is no problem to write something that is not in the combo. When I say select I don’t mean change the contents of the combo, I mean just mark the word as in the image, so I can copy
– Ricardo Alves
I still don’t quite understand what you’re trying to do. But
Enabled
indicates whether control is active or not, which is hardly what it wants, since withEnabled
tofalse
you can not even choose options. To make the options non-enterable, forcing the user to choose the ones that exist, just change theDropDownStyle
forDropDownList
– Isac
I gave the example of
enabled=false
just to show how I’d like it to look. The image I attached shows how I want it to look. The image combo, if you click on it or the arrow to appear the options, nothing happens. If you try to type, also nothing happens. But it is possible to highlight the text, as with any text when you want to use thectrl+c
. Thetextbox
has the propertyReadOnly
which is the perfect example of how I want the combo to be– Ricardo Alves