Combo box not showing items when it has focus - c#

I have 2 combo boxes on a winform, both dragged onto the surface via the designer, both get their items from a database both connect to the dbase fine and are populated properly. But one of them, when it has focus, the selected item is not visible; all that can be seen is the blue background highlight. I have compared the properties of both and cannot discern any setting that is different and that might cause this. When the combobox loses focus the selected item is show, black foreground on a white background as would be expected.

Have a look at the data that is in the Display Member. This might end with blanks/white spaces.
I had this same issue when the combobox was populated using a dataset from an SQL Database.
After changeing the select statement to remove leading and trailing spaces it worked as expected.
The explanation for me:
When editing/having focus, the combobox shows the text right-alligned in reverse video. If the text ends with a lot of blanks, as happens if you have SQL database with fixed length fields that are padded with spaces at the end, the result is that it shows the spaces at the end of the value (right alligned). If you remove the spaces in the select-query (in MSSQL this is done by ltrim(rtrim()) as the text of the selected item is not padded with spaces on the end, therefore it shows the text (in reverse video) until the focus is lost.

Related

I need a multi-column control that only displays one column in c#

In my project I need a control that allows me to enter 2 columns.
First column is an Id Number
Second column is some Text.
Example...
row 1 Id = 1 Text = Day Shift
row 2 Id = 2 Text = Night Shift
But I only want to display the Text Values and then have the user select either Day Shift or Night Shift.
The program can then just lookup the corresponding value (1 or 2) for whatever text they chose.
- In Microsoft Access I would just have used a ComboBox and hidden the first column.
I cannot find anything in Visual Studio 2017 to put on my Windows Form that easily does this.
I want to set it all up at design time and the closest I have come so far is by using a LISTVIEW control using a display type of LIST (I don't want column headers either.
It seems to do what I want except that it always wants me to leave a blank space below my lines of text - presumably for a horizontal scrollbar even though I set it to False in the properties and it does not actually display a scroll bar.
If I resize the ListView control to just be big enough for my two rows of text it tries to display the 2nd row next to the 1st row and still leaves a blank space below the rows.
See the below images, assuming I uploaded them properly, I am totally new to asking questions here.
Is there a way I can achieve this - should I be using a different control?
The closest I came to what I need is the top image
[![enter image description here][1]][1]
Thanks and I hope it all makes sense.
In reply to Harry I added the below...
After adding the ListView control to my form I then clicked on the little arrow selector (in the top right of the ListView control).to bring up the collections list popup box.
It is there I set the view to be ‘List’ so I don’t have to have column header, then I clicked on EDIT ITEMS to get another pop-up screen…Click the [Add] button to add items in the rows.
Then I changed the Text to ‘Day Shift’ for the first member (and ‘Night Shift’ for the second member).
To add values in the rows for the 2nd column I then clicked the SUBITEMS (collection) box in properties to get the next popup…
I then clicked the Add button and created a new Text item (value 1)
and repeated this for the Night Shift member but gave it a value of 2.
These values are to be the actual ID values (in the 2nd column).
Note…
I did create column Headers but setting the View to List means I don’t get them displayed (and don’t want them displayed).
but I did notice that changing the View to List also removes the second column (Id) from the ListView display.
As I don’t need the Id values (1 & 2) displayed I am not going to worry about what happened to them disappearing in the display. All I did, though, was just changed the View from Details to List.
Sorry If I’m misunderstanding you but at this point there is no actual code I am creating, no doubt Visual Studio 2017 does that itself but I am not looking at that. Is that what you needed to see?
Hope this makes better sense though, I did try and include images but the system will not let me do that until my reputation points are higher, sorry.
Thanks

Unique bug in devexpress' combo box, 2 out of 131 rows that were added seemed to have extra characters (WPF)

This is the first time I've experienced something like this. As far as I know I've done everything as they should be and for the most part it did output what I wanted but somehow there were unique bugs that seemed impossible.
It all started with a combo box that I populated with this code:
items.Add(new { Text = dr4["item_name"], Value = dr4["item_id"] });
It did the trick and filled the combo box with the 131 rows of item names in the combo box. But while scrolling inside the combo box, I found two items that were occupying two lines. I don't know how this happened, all of the items that were inserted in that combo box underwent the exact same way since I looped the code above so the problem could have not been from there. I double checked my database to see if I had some extra spaces or characters but they didn't and was normal as the rest of the 129 rows.
I tried creating a textbox and tried to fill it with both the combo box's text and value using this code:
textBox.Text = comboBox.SelectedValue.ToString() + comboBox.Text.ToString();
I had some interesting results. The 129 rows of data appeared right next to each other (not even a space) BUT the 2 specific rows of data appeared as if someone pressed enter after the selected value was printed.
For the sake of further explanation, let's say the selected value was "5" and the text was "This is number 5":
Now, the textbox's text output from the 129 rows were:
5This is number 5
While the textbox's text output from the 2 other rows where:
5
This is number 5
Now I don't know how this is happening and can't even explain it myself as they're both all from the same table, it couldn't be their length because there are longer texts from the other text from the 129 rows than the 2 other rows that's behaving unexpectedly.
I can't understand where are they getting this extra characters from, there doesn't seem to be any pattern. I think this has something to do with the devexpress combo box I'm using. I've tried populating a normal visual studio's combo box and it produced my desired output, all of the 131 items inside the combo box were normal.

C# Combobox Displaying Blank Items

I'm coding a combobox in C# and for some reason the items in the drop down don't have text. When I have selected an item, it is shown in the combo box text field (the drop down list is always blank whenever I click the drop down button). The datasource seems bound properly because the proper values are being returned when I select items, and the size of the drop down list will change depending on how many items the datasource has. Everything looks fine except for the fact that it seems like my drop down is populated with a bunch of empty strings, which it clearly isn't since as soon as an item is selected the proper text will display.
This is the relevant code:
if (list.Count > 0)
{
cboCustomers.DisplayMember = "Name";
cboCustomers.DataSource = list;
cboCustomers.ValueMember = "ID";
cboCustomers.SelectedIndex = 0;
}
I have looked for an answer to this but can't find it anywhere...I'm sure it's something really simple, but I can't figure it out. The closest problem I found had an answer suggested to set the display member before the data source, which clearly didn't work.
The list is populated from a database query. This will run on keyUp, the idea is that the list is populated as the person is typing based on the info given. So if I wrote 'S' I'd get a combobox with a dropdown that had all the clients starting with 'S'.
Given you don't have any anomalies in your binding, you are probably being affected by DrawMode property of your ComboBox, which may be set to OwnerDrawFixed or OwnerDrawVariable. Set it to Normal and things should get better.
as soon as an item is selected the proper text will display.
A foreground color the same as the background color will produce the same results you are seeing.

ListView text is truncated -- how do I display the entire text of an item?

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);

Display an invalid default value in a DataGridViewComboBoxColumn

I have a DataGridViewComboBoxColumn in a DataGridView in a windows application. The user can change settings elsewhere to potentially invalidate a selection in a DataGridViewComboBoxColumn. I have a requirement to retain/display the invalid item while only leaving valid items selectable in the list.
Without correcting the selection an exception is thrown:
DataGridViewComboBoxCell value is not valid.
Catching and ignoring the setting reverts the selected value to the first valid item in the list.
Is there a way to provide a value to a DataGridViewComboBoxColumn so that it does not show up in the list of selectable values?
I found a very strange solution:
Set the autosizecolumnmode to none...
Here's an explaination
http://www.kebabshopblues.co.uk/2007/03/24/more-on-that-datagridviewcombobox-error/
I'm going to use an example that the values in the drop-down list are colors, and the DataGridView has a list of t-shirts in your closet.
Have you tried inserting the invalid color into the list object that is bound to the ComboBox column? Perhaps you can insert something into the list whose key matches your invalid color but shows "(Invalid)" (or other similar text). You'd have to respond to the CellValidating event if someone were to try and choose it after your initial binding.
If the invalid colors come by altering a lookup somewhere else in the application so that the DataGridView's values (the t-shirts) are no longer valid, you have some options. Perhaps you could change that logic to look up the data that goes to the list of t-shirts and see if there are any existing t-shirts with that color -- then prompt the user to say "You have deactivated Red, but you have Red T-shirts; what do you want to do?" You could stop deactivating Red, change the T-shirts, or delete the T-shirts.
If the invalid colors come from a source you don't control, you could prompt the user when they try to look at the list of t-shirts, "Red is no longer a valid color for T-shirts, what do we do with the Red t-shirts?"
We have a similar constraint in our application. We dropped the combo boxes and use CellValidating instead.
Presumably your bound datasource to populate the combobox is read only. If so, why not just temporarily insert the invalid value into the underlying datasource. That way it would still be displayed. You could add a temporary column to flag invalid items and not allow the user to leave it selected and then delete it when the user navigated off of the cell. I've never done this with a datagridview but we did something very similar with a different 3rd party grid. Good Luck!

Categories