Combobox read only

Asked

Viewed 701 times

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:

inserir a descrição da imagem aqui

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 TabPageor 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 ?

  • 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

  • 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 with Enabled to false you can not even choose options. To make the options non-enterable, forcing the user to choose the ones that exist, just change the DropDownStyle for DropDownList

  • 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 the ctrl+c. The textbox has the property ReadOnly which is the perfect example of how I want the combo to be

2 answers

2

The Combobox there is a property called DropDownStyle, in it there is a value where you choose to DropDownList, thus, the user will not be able to type in its checkbox, nor copy its values.

this.dropDown1.DropDownStyle = ComboBoxStyle.DropDownList;

Updating

If you want curb a Combobox, so that the user cannot enter or change their values, consider removing all items except the selected one:

public void TravarComboBox(ref ComboBox cb) {
    string data = dropDown1.SelectedText;
    cb.DropDownStyle = ComboBoxStyle.DropDownList;
    cb.Items.Clear();
    cb.Items.Add(data);
    cb.SelectedIndex = 0;
}
...
TravarComboBox(ref comboBox1);

Thus, when the user tries to modify the Combobox, he cannot, because his field will only have a value in the list.

  • 1

    Excuse me, but could you clarify your answer? Because the combobox does not have the property ReadOnly and to change to DropDownList is combobox1.DropDownStyle=ComboBoxStyle.DropDownList

  • @Ricardoalves miss me, I ended up confusing the balls. Corrected, Obirgado!

1


Hello, if I understand correctly you want to take the option to write in Dropdown but want to be able to select its text property.

This property leaves readonly Dropdownlist = Dropdownlist, but not to select the text.

That’s all I know about copying the text, I can’t help. I can yes kkk forgot that:

Create a Keypress event in the combobox and change the Handled property to true within the event; e. Handled = true;

enum Condicao
    {
        pesquisa, cadastro
    }
    Condicao condicao = Condicao.pesquisa;
    void Muda()
    {
        foreach (Control c in this.Controls)
        {
            if (c is ComboBox)
            {
                c.KeyPress += new KeyPressEventHandler(BloqueiaTodosCombobox);
            }
        }
    }

    void BloqueiaTodosCombobox(object sender, KeyPressEventArgs e)
    {
        if(condicao == Condicao.pesquisa)
            e.Handled = true;

        else
            e.Handled = false;

    }
  • The e.Handled=True, within the event KeyPress Would it only serve to not allow something to be typed in the combo? I really want this to happen, but only in this specific case of register consultation. If I put it this way, it will never be possible to type anything (in the same way that would happen if I set the property DropDownStyle=DropDownList) ,correct? In this case, I no longer want it to happen. When the registration is being created, I want it to be typed in the combo.

  • Yes, try using a condition within the Keypress event to switch between registering and searching ex: if ( ! registration) { e.handled = true;} Else e.handled = false;

  • Starting from your reasoning, I could get close to what I want. Because when it is a consultation I use the event KeyPress not to allow anything to be typed, change the background color to gray and change the DropDownHeight to 1, not to show the list of options, when clicked on the combobox and when not consulted, everything goes back to normal. But I’d like to find a different way. It’s a considerable amount of combobox and it wouldn’t be practical to do that with each of them

  • your example code helped me a lot. I added in the question, the code I was able to do, starting from your example. Still have to do the ctrl+c function, when they are like "read only". Could you assist me?

  • @Grabriel I was able to implement ctrl+c. Thanks again for the code you posted. It was very helpful. I edited the question with the final version of the code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.