Combobox and a wish....

.NET programming topics
Post Reply
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Combobox and a wish....

Post by Neofriend » Thu Feb 04, 2010 5:18 pm

I am populating a combobox from a database table. Works like charm, no problem with that.

But when the user runs the software, I'd like to not allow the user to type anything random in the combobox and instead only be able to select the already populated items. How to do that?

I have an idea of checking the items in the list and comparing if the typed text is in the combobox list or not. If it is, then allow to proceed and if it isn't then don't let the program proceed. This code could be applied on submit button.

What do you think?
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Combobox and a wish....

Post by Neo » Thu Feb 04, 2010 6:14 pm

Set DropDownStyle to DropDownList under Appearance in Properties Window
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: Combobox and a wish....

Post by Neofriend » Thu Feb 04, 2010 7:08 pm

Wow, thank you.

I can't believe that was soooooo simple :)

If this was so easy then am sure one more related wish would be simple to solve as well.

I have a login form and after filling the fields, I'd like the user to be able to press Enter and it should act the same way as if the user pressed the submit button.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Combobox and a wish....

Post by Neo » Thu Feb 04, 2010 8:09 pm

On the form properties (not button properties), find AcceptButton and set it to button you want to act as the default as in screenshot.
AcceptButton.PNG
AcceptButton.PNG (9.71 KiB) Viewed 7039 times
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: Combobox and a wish....

Post by Neofriend » Thu Feb 04, 2010 9:11 pm

My PC is suffering through fever I think.... coding from friends PC these days. Will do this asap tomorrow.

Thanks a lot! :)
Neofriend
Captain
Captain
Posts: 103
Joined: Wed Dec 23, 2009 3:37 pm
Location: Pakistan

Re: Combobox and a wish....

Post by Neofriend » Fri Feb 05, 2010 5:13 pm

One more problem, I've just tried this. And this is working but I have vistaButton (a custom button made from code) and it isn't appearing in the acceptbutton property.

What do you think I can do? - Its just next to impossible changing all the buttons and am worries about messing up things since the time is not much.

Thank you.
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Combobox and a wish....

Post by Neo » Fri Feb 05, 2010 5:55 pm

To assign AcceptButton and CancelButton on a UserControl, try the following code. Let me know if it is working.

Code: Select all

public class UserControlEx : System.Windows.Forms.UserControl
{    
    private Button _acceptButton;
    private Button _cancelButton;

    public event EventHandler<EventArgs> AcceptEvent;
    public event EventHandler<EventArgs> CancelEvent;

    [Browsable(true)]
    public Button AcceptButton
    {
        get { return _acceptButton; }
        set { _acceptButton = value; }
    }

    [Browsable(true)]
    public Button CancelButton
    {
        get { return _cancelButton; }
        set { _cancelButton = value; }
    }

    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        if (msg.WParam.ToInt32() == (int)Keys.Enter)
        {
            OnAcceptEvent(EventArgs.Empty);

            if (_acceptButton != null)
                _acceptButton.PerformClick();
        }

        if (msg.WParam.ToInt32() == (int)Keys.Escape)
        {
            OnCancelEvent(EventArgs.Empty);

            if (_cancelButton != null)
                _cancelButton.PerformClick();
        }

        return base.ProcessCmdKey(ref msg, keyData);
    }

    protected virtual void OnAcceptEvent(EventArgs args)
    {
        if (AcceptEvent != null)
            AcceptEvent(this, args);
    }

    protected virtual void OnCancelEvent(EventArgs args)
    {
        if (CancelEvent != null)
            CancelEvent(this, args);
    }
}
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Combobox and a wish....

Post by Neo » Fri Feb 05, 2010 5:57 pm

This can also be a good fix.

Code: Select all

        private void UserControl1_Load(object sender, EventArgs e)
        {
                if (this.ParentForm.AcceptButton == null)
                    this.ParentForm.AcceptButton = this.button1;
        }
Post Reply

Return to “.NET Programming”