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?
Combobox and a wish....
Re: Combobox and a wish....
Set DropDownStyle to DropDownList under Appearance in Properties Window
Re: Combobox and a wish....
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.
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.
Re: Combobox and a wish....
On the form properties (not button properties), find AcceptButton and set it to button you want to act as the default as in screenshot.
Re: Combobox and a wish....
My PC is suffering through fever I think.... coding from friends PC these days. Will do this asap tomorrow.
Thanks a lot!
Thanks a lot!
Re: Combobox and a wish....
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.
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.
Re: Combobox and a wish....
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);
}
}
Re: Combobox and a wish....
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;
}