Note: an example of how to add a space character (or any other strings) to the front (prefix) or end (suffix) of the ComboBox Items appears at the end, here.
Please understand that these basic Controls provided by WinForms are essentially "little-black-boxes," an "opaque wrapper" of .NET around old COM based Controls. As a group, they are inconsistent, and I would argue that, with rare exceptions, any time a developer spends "hacking" these Controls using the Win API, is wasting time.
There is already a useful, proven, method ("Composition") to create custom Controls: make a UserControl that contains the "atomic" Controls you want to use as an "entity."
So, add a UserControl to your Project, put a ComboBox, and a Button, into the UserControl. Spend some time tweaking the visual appearance of the UserControl.
The code-behind can look this simple:
using System;
using System.Windows.Forms;
namespace YourNameSpace
{
public partial class UCComboBox : UserControl
{
public UCComboBox()
{
InitializeComponent();
}
public Action<ComboBox> GetNewItemAction { set; get; }
public Action<int, object> ComboSelectionAction { set; get; }
public void SetComboItems(bool doClearItemsFirst, params string[] items)
{
if(doClearItemsFirst) TheCombo.Items.Clear();
TheCombo.Items.AddRange(items);
}
private void btnAddNewItem_Click(object sender, EventArgs e)
{
if (GetNewItemAction != null) GetNewItemAction(TheCombo);
}
private void TheCombo_SelectedIndexChanged(object sender, EventArgs e)
{
if (ComboSelectionAction != null) ComboSelectionAction(TheCombo.SelectedIndex, TheCombo.SelectedItem);
}
}
}
An example of the use of this UserControl:
a. assuming you've either drag-dropped an instance of the UserControl from the ToolBox, or created one in code: it's named 'UCComboBox1:
private void Form1_Load(object sender, EventArgs e)
{
UCComboBox1.SetComboItems(
true,
"one", "two", "three", "four",
"five", "six", "seven", "eight");
UCComboBox1.ComboSelectionAction = ComboSelectionAction;
UCComboBox1.GetNewItemAction = GetNewItemAction;
}
private void ComboSelectionAction(int ndx, object item)
{
Console.WriteLine("selected index: {0} selected item: {1}", ndx, item);
}
private void GetNewItemAction(ComboBox comboBox)
{
comboBox.Items.Add("nine");
}
But, how do you do add spaces (or anything else) to the Items in the ComboBox ?
Well, you could "automate" that pretty easily:
public void SetComboItems(bool doClearItemsFirst, string prefix, string suffix, params string[] items)
{
if(doClearItemsFirst) TheCombo.Items.Clear();
TheCombo.Items.AddRange(AdjustItems(prefix, suffix, items));
}
public string[] AdjustItems(string prefix, string suffix, string[] items)
{
return items.Select(itm => prefix + itm + suffix).ToArray();
}
To use this:
UCComboBox1.SetComboItems(
true,
"{",
"}",
"one", "two", "three", "four",
"five", "six", "seven", "eight");
Notes:
0. in case it's not clear to you: 'Action and 'Func are just a convenient way to use the Delegate Type. These facilities were added in .NET 3.5.
1. the choice to pass a reference to the ComboBox itself to the external client/user when the Button is clicked is based on the idea that the client/user may wish to do some editing/adjustment to the ComboBox Items beyond just adding a new Item. if adding a new Item is all that's required, you could use a 'Func that returned some 'object or 'string to add to the ComboBox, rather than an 'Action, and avoid "exposing" the ComboBox outside its host Class.