Handling the sender object in closing event - wpf

I'm getting a bit confused over the sender object in VB .Net. If I have the following event:
Private Sub wpfWindow_Closing(sender As Object, e As
ComponentModel.CancelEventArgs) Handles wpfWindow.Closing
End Sub
Then I understand that the sender is the thing which is calling the closing event just before the window closes. However, I am struggling to see how to determine which button was pressed. If I do the following in the event:
Dim aButton As Button = CType(sender, Button)
If aButton.Content = "Next" Then
MessageBox.Show("You pressed Next")
End If
This brings up an error as it can't cast the sender, even though I pressed "Next".
Have also tried:
If sender Is btnNext Then
MessageBox.Show("You pressed Next")
End If
But in this case, it just skips it.
At the end of the day, I am just trying to navigate through some WPF windows. There is a starting window which I hide once I move onto the next. The problem is that on the second window I would like to determine if "Next" is pressed rather than "Cancel" or the close button in the top right. If next is pressed, then I would like to close the window and open another one. If the other buttons are pressed, then I would like to close the window and open the starting window.

The sender will be the Window because Closing is the Windows's event.
So you can cast the sender to Window, but not to Button.
If you have separate Click event handlers for your Next and Cancel buttons it shouldn't be difficult to determine which Button was clicked. In the Click event handler you can do what you originally wanted, casting the sender to a Button type.

Related

Click outside component to unfocus

By default, I need to focus on another component (a button or a textBox) to unfocus from the current one.
I want to just click outside.
So for example if I click on a textBox and write something, than click outside the TextBox, I shouldn't be able to type because the component is unfocused.
(I hope my explanation is clear, if not, please say so in the comments)
You can achieve this functionality by attaching to PreviewMouseDown event of a Window. Also in order to Lose Focus you need another focusable control (e.g. a text box named _dummyControl) to move focus to.
public MainWindow()
{
this.PreviewMouseDown += PreviewMouseDownEventHandler;
}
private void PreviewMouseDownEventHandler(object sender, MouseButtonEventArgs e)
{
//check conditions here...
_dummyControl.Focus();
}
more info:
PreviewMouseDown event occurs just before a MouseDown or click event occurs. By attaching to this event your code can tell when user clicks on the window and at that time you should move focus to a hidden control to simulate unfocus functionality.

Handling click events for dynamically created buttons in array

I have an array of buttons that are created through code. The reason why I've done this is because the amount of buttons could change, and in this case, I'm trying to create buttons that are displayed as days in a month on a calendar, and the amount can't be at a set amount because if the amount of buttons were being created for February, the amount would either be 28 or 29.
I've done this how I want to, however the problem comes at having to handle a click event for each button. Since I'm looking for the general idea how to handle a click event in the below example, I want to messagebox what the content is for the button.
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length
btns(ButtonCount) = New Button With {.Content = ButtonCount}
'Handler goes here.
Next
The way that I reference these buttons individually is through btns(ButtonCount), I do not name them.
So is there a way to add a click event to these buttons created in the example?
Use the AddHandler statement to assign an event handler to your button. In your event handler check which button was pressed.
First of all, take a look at the documentation as suggested in the comments.
(AddHandler Doc.)
Second, you could follow this example to achieve what you want to do:
Dim btns(Date.DaysInMonth(CurrentYear, CurrentMonth) - 1) As Button
For ButtonCount As Integer To btns.Length - 1
btns(ButtonCount) = New Button With {.Content = ButtonCount}
AddHandler btns(ButtonCount).Click, AddressOf OnBtnClick
Next
Private Sub OnBtnClick(sender As Object, e As RoutedEventArgs)
'Your Event Handling
End Sub

DevExpress XtraGrid checkbox check not registered unless focus changes

We have a databound XtraGrid on our Windows form. One of the columns is a check box. The problem is as follows: when users check the checkbox and click OK button, the checkbox, while visibly checked, is not considered checked by the grid. When I do this (while looping through rows):
isAllowed = Convert.ToBoolean(viewMain.GetRowCellValue(nRowCtr, "IsAllowed"))
I get back False. BUT, if the user checks the box, and then clicks somewhere else on the form or on another row in this grid, thus taking away focus from the checked checkbox, the same code above will return True.
Any insight on how to fix this behavior would be greatly appreciated.
Workaround found:
With default settings, when users click on a cell to edit it, the cell goes into edit mode, loads the editor control (in this case I have a CheckEdit repository control) and changes control's value (in this case checked state). If I click on another row, or another control, the cell then gets out of edit mode, committing the change to data item. But if I click on a button, then my change is lost. The workaround is to use the CheckEdit's CheckedChanged event to close editor:
Private Sub edCheck_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles edCheck.CheckedChanged
gridYears.FocusedView.CloseEditor()
End Sub
There's actually a cleaner way of doing this (it works for all RepositoryItems), detailed on the DevExpress site. The idea is to call the GridView.PostEditor method from a repository item's EditValueChanged event handler to immediately save the edited value to the grid's cell and the underlying column.
This code in the view's CellValueChanging event handler solved the problem:
private void OnCellValueChanging(object sender, CellValueChangedEventArgs e)
{
_gridView.SetFocusedRowCellValue(_gridView.FocusedColumn, e.Value);
}

Target x button of a window title to an Button.Click event handler?

I have in my window a button called btnExit which I handle its event.
I want that when I click the exit button (the red X in the right side) of the form, the event should be handled by the btnExit event.
Is this possible whithout need to use the OnExit or Closing etc.?
I guess that's impossible.
I reported a suggestion here, please vote.

Inherited form problem

i have a windows form that inherites from another windows form in the same project,
the problem is when i click a button in the inherited form the eventhandler fires twice
performing the event in the parent form and then performs the event in the inherited form
.
any suggestions?
http://www.metacafe.com/watch/852308/inheritied_forms_c/
in this video u will see what am doing exactly and the problem am facing is in the end of this video .. u will see exactly what i mean –
This is the way it's supposed to work. You have two event handlers, both are executed. I would suggest the following:
In the parent form, add a method
Protected Overridable Sub OnOKButtonClick()
MsgBox("Parent Form OK Button Clicked")
End Sub
which you call from the button's click event. In the inherited form, remove the button click event handler, and override the OnButtonClick method
Protected Overrides Sub OnOKButtonClick()
MsgBox("Inherited Form OK Button Clicked")
End Sub
This should accomplish what you are after.

Resources