Handling click events for dynamically created buttons in array - wpf

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

Related

How to fix the late key press problem in access

I have a textbox that's purpose is to update the subtotal value whenever there is a key stroke in access.
Now the problem with this is that the OnKey press event function updates the subtotal value after 1 extra key press. The text box is named as QuantityOrdSub and it is the box that takes quantity, this text box multiplies it with the unit price (DLOOKUP function which works). The output must instantly go into SubTotalValue text box and it this textbox has a control source = Subtotal
Private Sub QuantityOrdSub_KeyPress(KeyAscii As Integer)
Me.SubTotalValue.Value = (DLookup("UnitPrice", "Stock", "StockID=" & Int(Me.StockSearchID.Value))) *
(Int(Me.QuantityOrdSub.Value))
End Sub
To restate, I am trying to update a text box instantly on key stroke.
Two problems:
1) The correct event to use is On Change, i.e. Private Sub QuantityOrdSub_Change, not KeyPress.
2) .Value of the textbox isn't updated until you leave the control. To get the entered text while the user is typing, you must use .Text, i.e. Me.QuantityOrdSub.Text.
If you want instant calculation, I wouldn't monitor the KeyPress() event. Instead, I would have the control report when its value changes, therefore I'd monitor the Change() event.
Private Sub QuantityOrdSub_Change()
With QuantityOrdSub
If Len(.Text) > 0 Then
SubTotalValue.Value = DLookup("UnitPrice", "Stock", "StockID=" & StockSearchID.Value) * Int(.Text)
End If
End With
End Sub

How to focus on a ToolStripTextBox (one of the items in a ToolStripDropDownButton.DropDownItems collection) when the drop down opens

I'm building the DropDownItems of a ToolStripDropDownButton programmatically. The first field is always a ToolStripTextBox (which users can type into to filter the following items). I want the ToolStripTextBox to have focus as soon as the drop down opens so that user can:
Click on the ToolStripDropDownButton
Begin typing (to filter the items)
However, when I try to focus on the ToolStripTextBox (within the DropDownOpened event handler):
Dim v As ToolStripTextBox = DirectCast(tsbForms.DropDownItems(0), ToolStripTextBox)
Me.ActiveControl = v.Control
I get an exception:
System.ArgumentException: 'Invisible or disabled control cannot be activated'
Here's a screen shot of the dropdown so you can see what I'm talking about:
Currently the textbox at the top doesn't have focus and you have to click in it before you can start typing in the filter.
FYI I've tried testing the Visibility of the ToolStripTextBox before setting ActiveControl and it is True in this event. I've tried performing the operation a few other events and got the same result.
Just setting focus worked for me:
Dim v As ToolStripTextBox = DirectCast(tsbForms.DropDownItems(0), ToolStripTextBox)
v.Focus()
I used the DropDownOpened event of the parent ToolStripDropDownButton.

Handling the sender object in closing event

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.

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

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