I have a data grids within two tabs. so when second tab click it should open a filter window (not a <popup>, its a <window>) . I am doing it as follows.
if (tabControl1.SelectedIndex == 1) {
DashboardFilterView filterWindow = new DashboardFilterView();
filterWindow.ShowDialog(); }
When I click the close button of window it is closed.
Question :
After closing popup window, if I click on row of datagrid which is in my current tab, again popup window is displayed.
How can I prevent this from happening more than once?
Since tabControl1_SelectionChanged event is firing for even grid row click, I added a check to confirm whether this event occurs from tab.
e.OriginalSource is TabControl solved the problem.
private void tabControl1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource is TabControl)
{
if (tabControl1.SelectedIndex == 0)
{
// Do something
}
else if (tabControl1.SelectedIndex == 1)
{
DashboardFilterView filterWindow = new DashboardFilterView();
filterWindow.ShowDialog();
}
}
}
Related
I have a ContextMenuStrip with a ToolStripButtonMenu "Print".
A MDI child form is open containing a DataGridView. I am doing a validation to an editable column "Copies" in that grid. I don't want the user to input letters for example. The validation is working fine when leaving the cell but if I am clicking on a control such as the "Print" button, the validation is not caused.
The following screen shot shows how I can click on the "Print" button while the Copies cell contains letters:
// The code for the cell validation
private void QuantitiesDataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)
{
if (e.ColumnIndex == QuantitiesDataGridView.Columns[COL_COPIES].Index)
{
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "";
int enteredValue;
if (!int.TryParse(e.FormattedValue.ToString(), out enteredValue) || enteredValue < 1)
{
e.Cancel = true;
QuantitiesDataGridView.Rows[e.RowIndex].ErrorText = "Invalid number of copies";
}
}
}
I was looking for a property of the ToolStripButtonMenu such as CauseValidation but there is not such one.
Is there a way to trigger the validation when clicking on one of the ToolStripButtonMenu so the Print button will not be triggered until the Copies value is valid?
In your ToolStripButton's Click method, try calling the active form's ValidateChildren function:
private void toolStripButton1_Click(object sender, EventArgs e) {
if (this.ActiveMdiChild.ValidateChildren()) {
// do your processing ...
}
}
In my .NET 4,5 Winforms application, the ListView control's MouseUp event is firing multiple times when I open a file from that event as follows:
private void ListView1_MouseUp(object sender, MouseEventArgs e)
{
ListViewHitTestInfo hit = ListView1.HitTest(e.Location);
if (hit.SubItem != null && hit.SubItem == hit.Item.SubItems[1])
{
System.Diagnostics.Process.Start(#"C:\Folder1\Test.pdf");
MessageBox.Show("A test");
}
}
Note: When clicking on the SubItem1 of the listview, the file opens but the message box appears at least twice. But, when I comment out the line that opens the file the message box appears only once (as it should). I do need to open the file whose name is clicked by the user in the listview. How can I achieve this without the MoueUp event firing multiple times?
Please also note that the MouseClick event for listview does not always work as also stated here. So, I have to use MouseUp event.
EDIT: I should mention the ListView is in Details mode.
Avoid HitTest() and use ListView's native function GetItemAt(). An example from MSDN looks like this:
private void ListView1_MouseDown(object sender, MouseEventArgs e)
{
ListViewItem selection = ListView1.GetItemAt(e.X, e.Y);
// If the user selects an item in the ListView, display
// the image in the PictureBox.
if (selection != null)
{
PictureBox1.Image = System.Drawing.Image.FromFile(
selection.SubItems[1].Text);
}
}
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.
I'm making a context menu strip appear during the right clicking of either a selected or unselected tab in the tab strip of a winforms tabcontrol. It's going to have close, and close all but this for now. Anyway, I need to be able to capture which tab the mouse is over when the right click is pressed. Anyone know how?
Another solution that I'd accept is one that selects the unselected tab with a right click before the context menu is shown.
In your mouse click event you can add this code to find it, if tabs is your tabcontrol
for (int i = 0; i < tabs.TabCount; ++i) {
if (tabs.GetTabRect(i).Contains(e.Location)) {
//tabs.Controls[i]; // this is your tab
}
}
This could help, it captures the position of your right click with the mouse and if it's on the rectangle of any tab, that tab will be selected and get's right menu to be showed
private void tabControl1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
for (int i = 0; i < tabs.TabCount; ++i)
{
if (tabs.GetTabRect(i).Contains(e.Location))
{
tabControl1.SelectTab(i);
this.contextMenuStrip1.Show(this.tabControl1, e.Location);
}
}
}
}
Have fun :)
The sender parameter of the event handler usually gives you the object that you clicked on.
void whatever_OnClick(object sender, EventArgs e) {
var tab = sender as TabControlClassHere;
}
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.