I use a ComboBox DropDown to show a number of items that can be selected. I also use a default text "Select item". It works perfectly apart from the fact that once I picked an item from the list, I can select the text and remove it (using the backspace) so that the area for the selected item is blank (see image).
I would like to prevent this or, in case this is not possible, to have the default text shown again. The latter doesn't work so far, so I tried the DropDownList but then the background of the entire list becomes grey. If I then select OwnerDrawFixed or OwnerDrawVariable in DrawMode, the background turns white again but also the font color becomes white (instead of black).
I found that I have to use DrawItem to change the layout but even that doesn't work. I simply want the same style as I indicated for DropDown. Does anyone know how to do this or where to find a similar question that has already been answered?
How about something like this?
You could also try setting the DropDownStyle to DropDownList and FlatStyle to Flat. This gives you a slightly different result.
private void InitialiseCombo(ComboBox combo)
{
combo.Items.Clear();
combo.Items.AddRange(new object[] { 3391200121, 3391200122 });
combo.DropDownStyle = ComboBoxStyle.DropDown;
combo.Text = "Select Item";
combo.KeyPress += combo_KeyPress;
}
void combo_KeyPress(object sender, KeyPressEventArgs e)
{
e.Handled = true;
}
The suggested solution did not do the trick but I found a workaround to the initial problem, which is the manual deletion of the ComboBox Text, so I'll just put it here in case someone else has the same problem. When using
MyComboBox_TextChanged(object sender, EventArgs e)
I can set a restriction myself: if the text is blank, it should show the default text. If the typed text is equal to one of the item in the ComboBox, it should set the selectedItem equal to the typed text.
I also included GotFocus and Leave EventHandlers.
Related
Using WinForms. I have a combobox with DropDownStyle DropDown. In the items I put a single item "XA". When the user enters "X" into the ComboBox (not yet dropped down) and then presses the drop down button "X" is automatically replaced with "XA". How can I stop this happening? I would like the user to be able to keep text as "X" and only change the text to "XA" if "XA" was clicked in the drop down list.
To recreate create a new WinForms application and add a comboBox then add the following code
private void Form1_Load(object sender, EventArgs e)
{
comboBox1.DropDownStyle = ComboBoxStyle.DropDown;
comboBox1.Items.Add("XA");
}
Note that if the user does not press the dropdown then "X" stays in the combobox.
Note that there is a similar sounding question here but it is actually different.
How do I set a ComboBox default *not in the drop down* when a list item begins with the same text as a drop down item?
I think this solution should help you:
Winforms combobox bug - 2 items with the same value but different key
It changes if (m.Msg == LB_FINDSTRING) to m.Msg = LB_FINDSTRINGEXACT;, which should prevent the behavior you describe.
THIS IS NOT A PLAIN LISTVIEW!
I want to create a Windows Forms Application in C#. But i have some problems with the ObjectListView.
I want to change the color of one specific cell in the objectlistview but I can only change the color of one row.
this is my objectlistview:
screenshot
there you can see the column "Queue" and there is a value "Allgemein" and I want to color all these values with "Allgemein" in these column in red but not the whole row.
I changed with this code the color of the row:
objectListView1.Items[0].BackColor = Color.Red;
I think that what you are trying to do is well explained in this ObjectListView cookbook.
Just In case someone needs this, if you want to change the color of the whole column (not header), use decoration.
There is a class TintColumnDecoration, used by default in OLV to tint sorted column, but you can customize it:
Dim MyTint= New TintedColumnDecoration(YourColumn)
MyTint.Tint = Color.FromArgb(50, Color.LightGreen)
OLV.AddDecoration(MyTint)
From http://objectlistview.sourceforge.net/
CellEditStarting event
The CellEditStarting event is triggered after the user has requested to edit a cell but before the cell editor is placed on the screen.
This event passes a CellEditEventArgs object to the event handlers. In the handler for this event, if you set e.Cancel to True , the cell editing operation will not begin. If you don’t cancel the edit operation, you will almost certainly want to play with the Control property of CellEditEventArgs.
You can use this to customise the default editor, or to replace it entirely.
For example, if your ObjectListView is showing a Color in a cell, there is no default editor to handle a Color. You could make your own ColorCellEditor, set it up correctly, and then set the Control property to be your color cell editor. The ObjectListView would then use that control rather than the default one. If you do this, you must fully configure your control, since the ObjectListView will not do any further configuration of the editor. So, to listen for the event, you would do something like this:
this.myObjectListView.CellEditStarting += new CellEditEventHandler(this.HandleCellEditStarting);
And your handler method might look something like this:
private void HandleCellEditStarting(object sender, CellEditEventArgs e) {
if (e.Value is Color) {
ColorCellEditor cce = new ColorCellEditor();
cce.Bounds = e.CellBounds;
cce.Value = e.Value;
e.Control = cce;
}
}
With this code in place, your spiffy ColorCellEditor will be shown whenever the user tries to edit a color in your ObjectListView.
I have been stuck on this problem for awhile now, and I was hoping for some direction or an answer. How do you change the text of a ToolStripDropDownButton based off of a selected item within itself?
I have a grid that is to be displayed, but I want to change the size of the grid on the fly. By selecting the ToolStripDropDownButton and selecting a predetermined list of grid sizes, the text of the ToolStripDropDownButton should change to show the user the new grid size.
Any answer is appreciated.
I have found the answer to my question, thank you all for the answers.
private void changeGridSizeDropDownButton_DropDownItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem != null)
{
changeGridSizeDropDownButton.Text = e.ClickedItem.Text;
// This line converts my list gridsize options like 64, 32, and 16
// and Parses that text into the new selected gridsize.
gridSize = Int32.Parse(e.ClickedItem.Text);
}
}
It's easy you'll see.
myToolStripDropDownButton.DropDownItems[0].Text = "New grid size";
The index[0] is the first item in your DropBox just change it to access other items.
This is to change the name of the items.
If you need to change Text somewhere on the ToolStrip independently from your DropDownButton check what item is selected than change
myToolStrip.Items.Add("DefautlGridSize");
myToolStrip.Items[0].Text("NewGridSize");
If you don't need the ToolStrip especially I would consider using a ComboBox with a Label which would do the job much more simply.
Hope this helped
I'm working on a custom control that internally uses a ComboBox.
My problem is when the ComboBox is focused and has the drop-down open, it appears to focus the entire control. I would like to automatically highlight the first item in the drop drown, but right now you have to push the Down key to do so.
Is there a way to programmatically Highlight the first item in a ComboBox (set the readonly IsHighlighted property to true)? I believe the concept of IsHighlight within a ComboBox is different than Focus. Also, I am binding via ItemsSource, so I have no reference to ComboBoxItems.
Here's a way of doing it, although it might not cover all the cases - but you didn't provide too many details (for example, what happens when there is already an element selected? Do you still want to select the first element in the list? The code below will highlight the first element only when there is no selection in the combobox. To make it always select the first element, the DropDownOpened event should be handled too).
public MainWindow()
{
InitializeComponent();
combobox.ItemContainerGenerator.StatusChanged += new EventHandler(ItemContainerGenerator_StatusChanged);
}
void ItemContainerGenerator_StatusChanged(object sender, EventArgs e)
{
if (combobox.ItemContainerGenerator.Status == System.Windows.Controls.Primitives.GeneratorStatus.ContainersGenerated)
{
(combobox.ItemContainerGenerator.ContainerFromIndex(0) as ComboBoxItem).Focus();
}
}
(Hope I understood correctly and this is what you want to do).
It might not be what you are looking for but if you set mycombo.SelectedIndex = 0 then mycombo.IsDropDownOpen = True it should open it up and have the first item selected. It will be highlighted but will also be the value in the combobox as well. I'm not sure if this is not the desired effect though..
Here's a picture of a System.Windows.Forms.ListView using LargeIcons
The selected item shows all it's text
e.g. The top left item shows only 11 characters of its name, it's shown fully if that's selected. How can I make it show all the text(or atleast more than 11 characters), for items that's not selected ?
There is DrawItem event http://msdn.microsoft.com/en-us/library/system.windows.forms.listview.drawitem.aspx. Inside this event you have access to System.Drawing.Graphics.DrawString method.
void view_DrawItem(object sender, DrawListViewItemEventArgs e)
{
e.Graphics.DrawString(e.Item.Text, drawFont, Brushes.Black,
new RectangleF(e.Item.Position.X,
e.Item.Position.Y,
20,
160));
}
I entered some values for width/height, but you should use MeasureString or similar method.
Also do not forget to set OwnerDraw=true on ListView, otherwise it will not work.
I had this problem a while ago and solved by inserting a space after x characters and/or before a capital letter, so the text is wrapped where I wanted it to. This is somewhat a hack but it's an easy fix. You of course need to keep track of the changed you made to the text so you can reverse it if the user selects the item for further use.
Caveat: this worked for me when the View type was Details, I haven't tried LargeIcons.
After writing output (mine was in bulk) to the listview add the following:
lstResults.AutoResizeColumn(0, ColumnHeaderAutoResizeStyle.HeaderSize);
lstResults.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);