Remove the border from a combobox

2019-06-22 04:53发布

问题:

I am working on a form in C# that is used for displaying information, but I still need to allow some manipulation of the information. What I really want to do is remove the border from a combobox, this would allow me to display the information while also allowing the user to select other items in the set without having to display them all.

To be more specific I have a list of 4 or 5 email addresses, but I only want to display one at a time while allowing the user a way to easily select a different item. I have searched all over but can't find an easy way to modify the border of a combobox, and a border less combobox will fit my needs nicely.

I just started with C# so I'm still learning, maybe I am missing something. Seems like this should be a lot easier than it's turning out to be, hopefully somebody can help me out.

EDIT:
The form is using labels to display information, so having a combobox in the middle of it makes it look awful. Basically what I'm looking for is a Link that when clicked opens the default email program and fills in the address, but I want the drop-down button so an alternate address can be selected. I don't want to display a huge list of addresses if I don't have to, I just want to display one at a time. Like a combobox, but with no border.

I could probably just add a button that displays a list of the alternate addresses, but why reinvent the wheel if I can just remove the border from a combobox and have exactly what I'm looking for? Thanks

回答1:

Perhaps you don't need a ComboBox at all. Assuming you're using Windows Forms, You could use a standard TextBox and add your list of email addresses to its AutoCompleteCustomSource (and set AutoCompleteSource to "CustomSource").

Then if you set the TextBox's AutoCompleteMode to "Append" the user will never see the full list of email addresses - they'll just get the closes match populated in the TextBox as they type. With a bit of code-behind you might even be able to introduce the ability to cycle through the available items with the up and down arrow keys.

Edit

Now that you've updated your question, I'll suggest a completely different approach.

Add the "default" email address as a standard Label. Heck - add it as a LinkLabel and make it clickable so it behaves like a mailto: link on a web page. Next to that label, add a normal Button. Set its FlatStyle property to "System", Font name to "Marlett" and caption to "u", so it has a nice "dropdown button" look to it.

Now add a ContextMenuStrip to your form and add a menu item for each email address. You could do this in code pretty easily.

Now add this Click event handler for your button:

private void button1_Click(object sender, EventArgs e)
{
    contextMenuStrip1.Show(button1, new Point(0, button1.Height));
}

So when the button is clicked, the menu pops up displaying the "alternate" email addresses. All you'll need to do is catch the Click event of the menu items to "use" the selected email address.



回答2:

Why would you need the ComboBox to be borderless? Doesn't make much sense to me.

Make you can do is make a read-only ComboBox. You can populate the entries and the user will be allowed to select them but not type in any text.

Just set DropDownStyle to ComboBoxStyle.DropDownList.

this.comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

Update:

You can try "hiding" the border by setting DrawMode to DrawMode.OwnerDrawFixed.

this.comboBox1.DrawMode = DrawMode.OwnerDrawFixed;

This won't hide it but makes it less visible.



标签: c# controls