I have a button and on click of that i show a popup which has a listbox.
popup named - popComboList
Listbox named - lstComboBoxResult
I am giving a focus to a listbox but at initial on a click of a button the listbox doesn't get focus-(this happens only once at initial, when i first time click button) After the second click it works.
private void bnOpen_Click(object sender, RoutedEventArgs e)
{
if (IsDesignTime)
return;
lstComboBoxResult.Width = tbComboValue.ActualWidth + bnOpen.ActualWidth;
if (!popComboList.IsOpen)
{
SetPopupPosition(popComboList);
popComboList.IsOpen = true;
lstComboBoxResult.Focus();
}
else
{
popComboList.IsOpen = false;
}
}
This is a bit of a guess, but try calling UpdateLayout() after opening the pop-up, but before calling Focus(). It's possible that the listbox is not fully initialized and therefore unable to accept focus until it has become visible for the first time.
Related
I need show droppeddown combobox after start program.
I need in dropdown style only, not simple style.
This is simple fragment of my program:
private void Form1_Shown(object sender, EventArgs e)
{
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
}
But I found the watch sign as cursor till I click on Form in any place.
I guessed that my Form have not fully active state and wait for something.
When I click Form (or combobox or any control) by LBM, it activated fully and all works fine.
Of course the combobox is dropup then, so I need click combobox twice.
Đ•ell me please what is correct initialization of such style combobox without "Cursor = Cursors.Default;"
You can simply wait until cursor is the default:
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
Application.DoEvents simply process messages from the window queue, so you can process message until you get that cursor is the default. In that moment, you can drop down your control without problem.
If you prefer, create a extension method for the Form:
public static class FormExtends
{
public static void WaitToDefaultCursor(this Form form)
{
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
}
}
And use it:
this.WaitToDefaultCursor();
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
NOTE: I use Cursor.Default but not to change the cursor. The form is processing messages and it's difficult to select a good moment to drop down the control.
I have a window form containing 2 list views (for instance, named a and b) and a button. The function of the button is supposedly to show a message box differently when there is focus between the 2 list views.
so the code for the button is
if (a.Focused)
MessageBox.Show("a");
else
MessageBox.Show("b");
However, when I selected the item in 'b' list view. It always return the MessageBox.Show(b). When I start over again and selected a item in 'a' list view, it give me an null reference exception.
Anything wrong with the code?
It's because when you click the button it's focused. So it will always show "b" because listview a is not focused.
Here's a suggestion: save the latest focused listview, and make a GotFocus event in both listviews (it's not in the designer so make it programatically), when clicking the button check what the latest focused listview is:
//Put this code after InitializeComponent();
ListViewA.GotFocus += ListViewFocus;
ListViewB.GotFocus += ListViewFocus;
...
private ListView Latest = null;
private void ListViewFocus(object sender, EventArgs e)
{
Latest = (sender as ListView);
}
private void Button_Click(object sender, EventArgs e)
{
if (Latest == null) MessageBox.Show("No listview is focused");
else if (Latest.Name == "ListViewA") MessageBox.Show("a");
else MessageBox.Show("b");
}
There's also a LostFocus event, if you want to make null again.
I don't know why you get the null reference exception. What line is causing it?
I'm making a settings form, where user can assign custom hotkeys for the application. There's a TextBox, and by clicking it with mouse, it focuses and waits for one keypress and then defocuses (by focusing another label):
private void txtKey_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
}
private void txtKey_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Text = e.KeyCode.ToString();
label1.Focus();
}
Is there a way to defocus focused TextBox (and cancel the key assinging process), by either clicking it again with mouse, or by clicking the GroupBox around it? I can't figure out how to check if TextBox was already focused when clicked (because when clicked, it gets focused before I can test if it's focused). Of course I can add a button "Cancel" next to the TextBox, but that's not what I want.
There is no Click-event for GroupBox, so I can't defocus TextBox by clicking GroupBox around it. Or can I somehow?
You can set/remove the Focus with
Keyboard.Focus = null;
You can also register to the following event:
public event MouseButtonEventHandler PreviewMouseLeftButtonDown
This event fires every time you click on the TextBox, thus you can set the Focus there if you want to.
For Winforms there is a way as well. I'm not proficient in it, but here would be a way:
Make a textBox (e.g. named textBoxFocus) that lies outside your window. Size it 1, 1 and move it to -10,-10 for example. Then you can register to the Click event and write
textBoxFocus.Focus();
It's a bit of a roundabout way, but should achieve what you want.
Thanks to private_meta for getting me to right direction (in comments)! I set the flag with click event, and before setting the flag, testing if flag is set. So first click does not find the flag, but second will. And flag is cleared within textbox Enter-event (which fires before Click-event). Now every other click focuses and every other defocuses textbox, as I wanted.
private void txtKey_Enter(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Tag = null;
}
private void txtKey_Click(object sender, EventArgs e)
{
TextBox textBox = (TextBox)sender;
if (textBox.Tag != null) label1.Focus();
textBox.Tag = "clicked";
}
One of the simple way is that, you may use a bool flag here.
Algorithm:
By default, the bool value is 0;
If(Textbox Selected && flag = 0)
Do your task; and flag = 1;
I hope I could satisfy your query and you can follow this algorithm.
i have a WPF tab control with two tabs(A, B). Since there is no Clicked Event on the TabItem,therefore i add an previewMouseDown event on the Tab B and a messageBox will show up. However, after i close the messageBox, my application will not navigate to the Tab B. Anyone can help me?
C# code snippet :
private void MyTabB_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("Please login");
}
You can use the TabControlSelectionChanged event.
Since you know the selected tab for login say index 0, change selected tab after the MessageBox ie something like this
MyTabB_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tc= ((TabControl)sender;
if(tc.SelectedIndex == tc.Items.IndexOf(A/*Login tab*/))
{
MessageBox.Show("Login")
tc.SelectedIndex = tc.Items.IndexOf(B);
}
}
This means whenever you select A you will MessageBox will pop up then you login.
Alternatively why not put a button or any control with ClickedEvent in A then when clicked tc.SelectedIndex is changed.
Here is the problem: I have a Windows Forms application that I'm developing, and in one segment I'm using a ListView control.
What I'm trying can be simply stated as: on event ListViewItemSelectionChange show a MessageBox for user to confirm the change, if not confirmed change to let's say the first item. This change to the first item would again fire ListViewItemSelecionChange, so I unregister and re-register the event handler method, so everything should be good, right?
What actually happens is that the handler method is called twice (actually ListView should fire two events on Selection change, one for deselect, other for newly selected item, but I have an e.IsSelected statement at the beginning to catch only selected items, so actually you could say that there are four events fired).
The problem is, if I generated the first event with mouse click on ListView item, and I've unsubscribed before programatically changing to the first item, what generates the second event firing? Is it some focus change because of the MessageBox call? Is there any way to prevent the second event to fire?
I have a simple example solution here, it can't be more simlified (25 SLOC), so if you can, please take a look. Note that commenting the line "if (ShowMessageBox())" stops the second event from firing, is this some focus change problem?
http://www.filedropper.com/listviewtestwithmsgbox
Edit: the relevant code:
private void listViewWithSelection1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
{
// listview actually generates two ItemSelectionChanged events,
// one for deselect of a item, and another event for a newly selected item (which we want here).
if (e.IsSelected)
{
if (ShowMessageBox())
Button1_Click(null, EventArgs.Empty);
label1.Text += "item selected ";
}
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void Button1_Click(object sender, EventArgs e)
{
// change ti first ListView item
listView1.ItemSelectionChanged -= listViewWithSelection1_ItemSelectionChanged;
listView1.Items[0].Selected = true;
listView1.ItemSelectionChanged += listViewWithSelection1_ItemSelectionChanged;
}
Hmm, can you describe how the selection is being changed to begin with? If it's by the user clicking to select an item, perhaps catch the Click or DoubleClick event rather than the ItemSelectionChanged event? I have this snippet I'm using on a program currently. If the user double clicks the list box (listView, in your case), do something with the selected item.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool ShowMessageBox()
{
return MessageBox.Show("Change to first item instead?", "test", MessageBoxButtons.YesNo) == DialogResult.Yes;
}
private void listView1_Click(object sender, EventArgs e)
{
if (ShowMessageBox())
listView1.TopItem.Selected = true;
label1.Text += "item selected ";
}
}
Edited to include relevant code.
One way to do this is to have a flag which says should the on change code run.
In your ListViewItemSelecionChange code you check the value of the flag and run code accordingly.