I have a winform built in Visual Studio and C#. Up until recently, the combo boxes behaved as expected. However, while adding functionality, I moved all the form elements around and now two of the combo boxes snap closed before an option can be selected (although you can select an item with the scroll wheel or arrow keys). The code in the dropdown event hasn't changed. I'm at a loss - anyone encountered anything like this?
Here's the code of the dropdown event: -
void comboBoxTargetServer_DropDown(object sender, System.EventArgs e)
{
comboBoxTargetServer.Items.Clear();
comboBoxTargetDatabase.Items.Clear();
comboBoxTargetDatabase.Items.Add("");
comboBoxTargetDatabase.Enabled = false;
//ActiveForm.Cursor = Cursors.WaitCursor;
List<string> sqlServers = SQLUtilities.ListSQLServers();
sqlServers.Sort();
foreach (string sqlServer in sqlServers)
{
comboBoxTargetServer.Items.Add(sqlServer);
}
//ActiveForm.Cursor = Cursors.Arrow;
comboBoxTargetDatabase.Enabled = true;
}
Thanks!
You're clearing all of the items once the ComboBox has dropped down. That will empty it, so there's nothing in the DropDown, so the list will disappear. You should pick a different event to fill it. In fact, is the data static within the context of one run of the application? If so, fill the ComboBox on Form Load and leave it at that.
Related
I am working on my software design project and I need to use serial communication between arduino and computer. I use 3 buttons in the project, 2 buttons are for moving the highlighted item in Combobox and the other one is to select the item. I tried to do it that way but somehow it can only select when I press the buttons to move them. Is it possible to do that it changes the highlight of the items?
How are you making the "move" buttons change the selected item?
Anyway, to select an item in the ComboBox, use the SelectedIndex property:
private void button1_Click(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
comboBox1.SelectedIndex = 1; // selected the second item
}
Not sure how you're receiving commands from your Arduino, but when you want to send an up arrow, you could call:
private void ArrowUp()
{
SendKeys.Send("{Up}");
}
Solution is when button is read I wrote this code, SendKeys.Send("{UP}"); or SendKeys.Send("{DOWN}"); depending on the button.
I have two forms, in the main one I put some controls to hide or show a secondary form (cronoFrame) according the selected screen. The secondary form has set the property FormStartPosition as Manual; this form is created only once, the controls to show it and hide it are a combo box and a check box like this:
I want that according to the selected screen in the combo box, show on it the secondary form when the de check box is selected. The first time it works fine, but if I select another screen, the form is showed in the first selected screen no matter the selection in combo box.
This is the event method for check box:
private void chkMostrarPantallaDiscursante_CheckedChanged(object sender, EventArgs e)
{
if (chkMostrarPantallaDiscursante.Checked)
{
Screen display = displaysDic[cmbDisplays.SelectedItem.ToString()];
var wa = display.WorkingArea;
Utilerias.cronoFrame.Left = wa.Left;
Utilerias.cronoFrame.Top = wa.Top;
Utilerias.cronoFrame.Width = wa.Width;
Utilerias.cronoFrame.Height = wa.Height;
Utilerias.cronoFrame.Show();
} else
{
Utilerias.cronoFrame.Hide();
}
}
Making several tests, the only way to achieve this is creating a new object of the secondary form every time that check box is selected and disposing the form when it is not selected. But I need the same object, not a new one. Any ideas to solve this?
A am writing a WinRT application which utilises a Gridview to display some data. The Gridview has a SelectionMode of Extended so that as the user navigates the grid with the cursor keys the selected item moves with them (plus I have multi-select functionality)
The problem I'm experiencing is that if you navigate the grid using the cursor keys and have Ctrl pressed down, the selected item remains where is was and only the focus changes. My DataTemplate doesn't show the focused item so it's quite confusing to the user.
Is there anyway I can change this behaviour so that navigating the grid with Ctrl held down works in the same way as if it wasn't being held down?
The solution was quite simple in the end. Just create a GotFocus handler like this one:
private void SdxGridView_GotFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource is GridViewItem && !((GridViewItem)e.OriginalSource).IsSelected)
{
SelectedItems.Clear();
((GridViewItem)e.OriginalSource).IsSelected = true;
}
}
I have a case where the user is given a ComboBox with potentially a lot of choices in it. Paired with this is a TextBox that filters the items. What I would like to do is open the drop down list when the TextBox has focus--let the user see what the current filter accomplishes as they type it. (This isn't just autocomplete, I'm currently matching the filter text anywhere in the item, I may replace this with a RegEx search down the road.)
It sounds simple enough--drop the box when the TextBox gets focus, close it when it loses focus. It opens--and promptly closes back up. Any good answers?
My Google-Fu must be weak tonight, I can't believe nobody has wanted to do this before yet I find nothing out there. (I have seen a related thing of typing in an open ComboBox to provide suggested options like Google does but my list is required, not merely suggestions.)
You can add on the Focus event of the TextBox code for the ComboBox setting the property
ComboBox.DroppedDown = true;
Than add on the TextChanged event of the TextBox code
ComboBox.SuspentLayout();
//ComboBox.Items add/remove
ComboBox.ResumeLayout();
Don't forget to reset the items when Text is empty.
EDIT:
This seems to work (but you don't get to see the mouse)
string[] items = { "abcd", "abc", "bcd", "cd" };
private void textBox1_TextChanged(object sender, EventArgs e)
{
comboBox1.SuspendLayout();
comboBox1.Items.Clear();
comboBox1.Items.AddRange(items.Where(item => item.ToLower().Contains(textBox1.Text.ToLower())).ToArray());
comboBox1.ResumeLayout();
comboBox1.DroppedDown = true;
}
I'm working with a data grid that contains a Combo Box Column, but editing this Combo Box (by simply clicking on it) gets annoying sometimes, since one must click at least twice to change the value of that field. I want to change that behaviour, so I thought it would be very simple: just create a OnMouseOver event to make the mouse-overed combo box be selected, but the only available event is the Disposed one.
Is there any way to change this behaviour?
I just dealt with the same problem, and solved it by setting the DataGridView.EditMode to EditOnEnter.
If you don't like that behaviour for all your other columns, I found this suggestion for placing in the CellEnter event:
if (DataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
((DataGridViewComboBoxEditingControl)DataGridView1.EditingControl).DroppedDown = true;
}
I haven't tried it, but it looks promising. The same technique is discussed on this question.
In Winforms, there's a CellMouseEnter event (and a CellEnter event for non-mouse navigation) on the DataGridView. You can use that to set the selected cell.
The reason you are getting only the Disposed event (I think) is because you are trying to go too deep. I get only the Disposed event when I try to go all the way to dataGridView1.Columns["Column1"]...
Instead, as KeithS mentioned, you can assign the CellMouseEnter event to your DataGridView.
From there, you can do the following...
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0 || e.ColumnIndex==2)
{
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
dataGridView1.BeginEdit(true);
}
}
That if-statement is just there to show how to restrict this functionality to certain columns, in case you want that.
The first line inside the if-statement sets the current cell and the second line begins the edit process.
This is a general process that should work with any type of column you can throw into a DataGridView. The DataGridView's EditMode shouldn't matter.
This works really good.
On the CellClick event of the datagridview:
void datagridview1_CellClick(object sender, .Windows.Forms.DataGridViewCellEventArgs e){if (e.ColumnIndex > 0)
{
W1.dGVReports.CurrentCell = W1.dGVReports.Rows[e.RowIndex].Cells[e.ColumnIndex];
W1.dGVReports.BeginEdit(true);
(W1.dGVReports.EditingControl as System.Windows.Forms.DataGridViewComboBoxEditingControl).DroppedDown = true;
}
}