ResizeEnd event on pressing Maximize button - vb.net

I am trying to show some hidden columns in a telerik RadGridView when the Form Window is Maximized.
I am aware of Resize Event but since Resize event is fired even before the Form is loaded, I am not using it.
I found that ResizeEnd suits my need but it is not reacting to Maximize button.
Is it possible to fire it with Maximize button.
Thanks.

since Resize event is fired even before the Form is loaded
Then add the handler in FormLoad instead of the designer
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
AddHandler Control.ResizeBegin, AddressOf controlResizeBegin
End Sub
Private Sub controlResizeBegin(sender As Object, e As EventArgs)
End Sub

Related

Catch/Handle Keydown Event From All Control Inside Groupbox

Is there any way to catch all keydown event from any controls like textboxes inside a groupbox using one event handler.?
I try groupbox_keydown, but it's not triggered. I try to make some research in msdn and they say groupbox cannot get focus, so the keydown event will never raised. Is there any workaround for it.?
KeyPreview already set to True.
Edit:
i have some textboxes inside a groupbox. most of them have default or predefined value. what i want is when user have filled the required data and want to skip some textboxes, he just need to press some key like ctrl+enter to excecuting a click on a button in that groupbox or calling a procedure.
This seems to work ok It iterates through the controls collection in the groupbox and adds the .KeyDown event to the example handler.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctl As Control In GroupBox1.Controls
AddHandler ctl.KeyDown, AddressOf GroupBox1Controls_KeyDown
Next
End Sub
Private Sub GroupBox1Controls_KeyDown(sender As Object, e As KeyEventArgs)
MessageBox.Show(DirectCast(sender, Control).Name)
End Sub
It may be that there are controls that don't have a .KeyDown event, so you might want to add an empty Try..Catch..End Try block into the code like this, but I wouldn't recommend it as it could hide other problems with the code.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each ctl As Control In GroupBox1.Controls
Try
AddHandler ctl.KeyDown, AddressOf GroupBox1Controls_KeyDown
Catch
End Try
Next
End Sub

VB.NET Button with both MouseMove and Click/MouseClick handlers

I'm attempting to to add both a MouseMove and a Click (or MouseClick) to a button. The end user needs the ability to click the button to add an element, but also the ability to drag the button to a specific location to add an element as well. I have both functions working independently. However, when I add both Handlers, the MouseMove takes precedence, and doesn't allow the Click to happen (attempting to click shows the DragDropEffects.None effect).
Button:
Dim Button As New Button()
'Button Attributes
AddHandler Button.Click, AddressOf Button_Click
AddHandler Button.MouseMove, AddressOf Button_MouseMove
Handlers:
Private Sub Button_MouseMove(sender As Object, e As MouseEventArgs)
Button.DoDragDrop("Button", DragDropEffects.Move)
End Sub
Private Sub Button_Click(sender As Object, e As EventArgs)
'Add Element Function
End Sub
I've also tried it with the MouseClick handler, with the same results:
AddHandler Button.MouseClick, AddressOf Button_MouseClick
Private Sub Button_MouseClick(sender As Object, e As MouseEventArgs)
'Add Element Function
End Sub
1.) What is the reasoning behind the Click/MouseClick not able to fire when a MouseMove is implemented as well?
2.) What is the proper workaround to get both functions usable? Do I need to do MouseDown and MouseUp handlers, and track the distance that the mouse has moved to determine if I clicked or if I am dragging?

Clicking on the form causes 'Unable to cast object of type '<Namespace.FormName>' to type 'System.Windows.Forms.Button'

I am making a version of the Microsoft game 'Minesweeper'. I make the buttons and labels programmatically and add two handlers for the buttons and one handler for the labels as follows.
AddHandler Newbtn.MouseDown, AddressOf Button_MouseDown
AddHandler Newbtn.MouseUp, AddressOf Button_MouseUp
AddHandler Newlbl.MouseDown, AddressOf Label_MouseDown
then I have three procedures:
Private Sub Button_MouseUp(sender As Button, e As MouseEventArgs) Handles Me.MouseUp
Private Sub Button_MouseDown(sender As Button, e As MouseEventArgs) Handles Me.MouseDown
Private Sub Label_MouseDown(sender As Label, e As MouseEventArgs) Handles Me.MouseClick
Everything works fine until I click outside of the grid of buttons on the form itself, when I get a 'invalidcastexception'. VS2015 goes into a break state. At the top it says:
"Your Application has entered a break state, but there is no code to show because all threads were executing external code(typically system or framework code)."
Additional information: Unable to cast object of type 'Mines.MineSweeperPJS' to type 'System.Windows.Forms.Button'.
Mines is the Namespace.
MineSweeperPJS is the form.
Any suggestions as to the cause of this issue would be greatly appreciated!
The problem is in this part of your event handlers
..... Handles Me.MouseUp
^^^
this means that every mouseup, or mousedown event on your form (Me) are passed to the event handler specified.
I think that you have written this event handler manually changing the parameter sender from Object to Button because if you let the Form Designer use its own template you end up with something like this
Private Sub Button_MouseUp(sender As Object, e As MouseEventArgs) Handles Newbtn.MouseUp
When the form engine calls these event handlers, in response to a Form mousedown or mouseup it passes the reference to the Form itself (Me) and not the reference to a Button. And this is the reason of the invalid cast.
You could remove the ...Handles Me.MouseUp because you have already set up the event handlers using the AddHandler and also restore the sender parameter to be As Object

Using the same event handler for multiple buttons

I have 15 buttons created through design.
I want them to have a background picture whenever I click on any of them, for example:
If I click on button11 then its background will be "Hello.jpg"
If click button12 then its background will be become "Hello.jpg"
Is there a method to write a code instead of writing code for individual button?
The code should detect which button I clicked and then change its background.
Private Sub e_11_Click(sender As Object, e As EventArgs) Handles e_11.Click
e_11.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub
Is there a way that handles every button click?
Yes, you can bind the same method to multiple controls:
Private Sub MyButtons_Click(sender As Object, e As EventArgs) _
Handles e_1.Click, e_2.Click, e_3.Click, ...
Dim myButton = DirectCast(sender, Button)
myButton.Image = Image.FromFile("E:\battleship\Explode.gif")
End Sub

Detecting a radiobutton change

I have about 50 radiobuttons on one form and I don't want to create an if statement for each one to detect when one changes, they are not part of a group box. How would I detect if any radiobutton changed and then write the name to another variable?
Put all the radio buttons on a panel and loop through the panels radio button controls, programmatically adding the same event handler for each as described by #Steve. One way I like to handle the event is to assign each radio button an index into its tag property. Then just store any relevant data in a list of objects and access the data for that radio button by pulling out it's corresponding object from the list using its tag. Much easier than doing it by hand.
Edit: Good catch #Neolisk. Updated answer.
You could Always set the CheckedChanged event for all 50 radiobuttons to the same event handler.
This is an example done via code:
Private Sub OnChange(sender As System.Object, e As System.EventArgs)
Dim rb = CType(sender, RadioButton)
Console.WriteLine(rb.Name + " " + rb.Checked.ToString)
End Sub
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddHandler Me.RadioButton1.CheckedChanged, AddressOf OnChange
AddHandler Me.RadioButton2.CheckedChanged, AddressOf OnChange
' and so on .....'
End Sub
I have done this via code and not using the designer to avoid the long add of Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged .......