TabCard event when a user has the intent to change tab - vb.net

Probably a simple one but a cannot figure out the correct event:
I have a vb.net WinForm with a TabControl. On every TabPage, the user can enter/modify some data and then (hopefully) save it.
To keep things clean, I want to check, if there is unsaved data, when a user changes tabs (and delete it, if not saved).
I am looking for the best event of the TabCard to do so. There is TabControl1.Selecting, .SelectedIndexChanged and .Selected which look promising but they all fire AFTER the Tab changed.
If the user wants to return to save the data, i need to figure out where he came from and show that TabPage again. Also the event would the fire again - Not practicable.
In Conlusion: I am looking for a TabControl Event, that fires after the user clicked another tabcard but before the card actually changes...
Or a better idea to solve this isse another way.

Use the Selecting event. If you don't want to change the tab page, you can cancel the event.
'Here's an example class with a tabControl
Public Class Form1
'this variable stores the currently selected tab
Private activeTab As TabPage
'this initializes the activeTab variable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
activeTab = TabControl1.SelectedTab
End Sub
'This checks to see if the tab should change or not
Private Sub TabControl1_Selecting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlCancelEventArgs) Handles TabControl1.Selecting
If (MessageBox.Show(String.Format("Return to {0} tab?", activeTab.Name), "TabControl", MessageBoxButtons.OKCancel) = Windows.Forms.DialogResult.OK) Then
e.Cancel = True
Else
activeTab = e.TabPage
End If
End Sub
End Class

Related

Why does a form move trigger ResizeEnd?

I use the following code in my form:
Public Class Form1
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
MsgBox("Resized")
End Sub
End Class
When I move my form, it also seems to trigger MyBase.ResizeEnd. Why is that? A move of the panel doesn't change the size, so I don't understand why.
Why does a form move trigger ResizeEnd?
Because this is the documented behavior. From the documentation:
The ResizeEnd event is also generated after the user moves a form, typically by clicking and dragging on the caption bar.
If you want an event that doesn't get triggered when the form is moved, you should use either Resize or SizeChanged. The problem with those two events is that they will be triggered while the form is being resized by the user. To work around that, you may use it with both ResizeBegin and ResizeEnd with a couple of flags to signal when the user actually finishes resizing the form.
Here's a complete example:
Private _resizeBegin As Boolean
Private _sizeChanged As Boolean
Private Sub Form1_ResizeBegin(sender As Object, e As EventArgs) Handles MyBase.ResizeBegin
_resizeBegin = True
End Sub
Private Sub Form1_SizeChanged(sender As Object, e As EventArgs) Handles MyBase.SizeChanged
' This is to avoid registering this as a resize event if it was triggered
' by another action (e.g., when the form is first initialized).
If Not _resizeBegin Then Exit Sub
_sizeChanged = True
End Sub
Private Sub Form1_ResizeEnd(sender As Object, e As EventArgs) Handles MyBase.ResizeEnd
_resizeBegin = False
If _sizeChanged Then
_sizeChanged = False
MessageBox.Show("The form has been resized.")
End If
End Sub
One thing to note is that both ResizeBegin and ResizeEnd are only triggered when the user manually resizes* the form. It does not, however, handle other situations like when the form is resized via code, when the form is maximized, or restored.
* or moves the form, which is the part that we're trying to avoid here.

DataGridView.RowLeave fires before a Form button.MouseClick

I can't find anything about this anywhere:
I have a form with a DataGridView and a few Buttons. When a row of the datagridview is selected, and I click a button (on the form), dgv.RowLeave triggers before anything else. It triggers even before Click or MouseClick.
That kind of makes sense, but the problem is that the sender of RowLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) is the DataGridView, not the button. So it doesn't seem to be possible to know at that point what button was clicked on, because sender and e both refer to the DataGridView, not the Form nor the Buttons.
The Click event is triggered, but only after RowLeave was processed.
So is there any way to know where the user clicked, before RowLeave does other things (in my case, resulting in the Button.Click to be never handled), or then from within RowLeave?
Class MainForm
' The form contains a DataGridView and btnQuit (and other buttons)
Private Sub dgv_RowLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgv.RowLeave
ProgrammaticallyDoRowValidation(dgv.CurrentRow.Index) ' This does validation and more.
' But if btnQuit is clicked, I need to know here, or before RowLeave is
' triggered and NOT do this row validation.
' ...
End Sub
Private Sub Form_Click(sender As Object, e As EventArgs) Handles MyBase.Click
Dim frm As Form
frm = CType(sender, Form)
' Translated from Sach's comment below. Code never reaches this event
'(RowLeave prevents it).
End Sub
Private Sub Quit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnQuit.MouseClick
QuitPgm() ' Contains some more stuff.
' Also never executed, because RowLeave is handled first.
End Sub
End Class
I'm using WinForms here, but the idea is the same.
So Events are attached to Controls. A Button is a Control, so is a DataGridView. Then in the Code Behind you have Event Handlers which are essentially methods tied to Control Events.
So when you attach a Click event to a button, behind-the-scenes, VB.NET creates an Event Handler like so:
private void button1_Click(object sender, EventArgs e)
{
}
Now the sender is an object, but it's actually the DataGrid that is passed there. So contrary to your statement So it doesn't seem to be possible to know at that point what button was clicked on you CAN know if a button was clicked on. If it was, and if you have an event handler attached, it will get called. For example, this will show a MessageBox with the button text:
private void button1_Click(object sender, EventArgs e)
{
var btn = (Button)sender;
MessageBox.Show(btn.Text);
}
So if you want to know if the Form was clicked, attach a Click event handler:
private void Form1_Click(object sender, EventArgs e)
{
var frm = (Form)sender;
MessageBox.Show(frm.Text);
}
I'm not sure how you prevent the button click in your RowLeave event, but I think you should use RowValidating event to validate the DataGridView. Let's say we have a DataGridView with only 1 column and a Button. The validation is the column value must not higher than 100. If we put the validation in the RowValidating event, the validation is triggered after the Leave event but before Validated event. If the validation fails, the subsequence events are not fired.
Public Class Form1
Function ProgrammaticallyDoRowValidation(i As Integer) As Boolean
If Convert.ToInt32(dgv(0, i).Value) > 100 Then
Return False
Else
Return True
End If
End Function
Private Sub dgv_RowValidating(sender As Object, e As DataGridViewCellCancelEventArgs) Handles dgv.RowValidating
If Not ProgrammaticallyDoRowValidation(dgv.CurrentRow.Index) Then
e.Cancel = True
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show("Buton clicked")
End Sub
End Class
Try running the code and enter a value higher than 100 in the column. You can't click the button because it fails the validation. But if you set the Button's CausesValidation property to False, the validation won't be triggered.
The order of event according to this link is like this:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and
so on), by calling the Select or SelectNextControl methods, or by
setting the ContainerControl.ActiveControl property to the current
form, focus events occur in the following order: Enter -> GotFocus ->
Leave -> Validating -> Validated -> LostFocus
When you change the focus by using the mouse or by calling the Focus
method, focus events occur in the following order: Enter -> GotFocus
-> LostFocus -> Leave -> Validating -> Validated
So is there any way to know where the user clicked, before RowLeave
does other things (in my case, resulting in the Button.Click to be
never handled), or then from within RowLeave?
Private Sub dgv_RowLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgv.RowLeave
ProgrammaticallyDoRowValidation(dgv.CurrentRow.Index) ' This does validation and more.
' But if btnQuit is clicked, I need to know here, or before RowLeave is
' triggered and NOT do this row validation.
' ...
End Sub
This is a bit of a kludge solution, but within the DataGridView.RowLeave event, you can check if the ContainerControl.ActiveControl Property to see if the currently active control is the one you want to test for. In this case the ContainerControl is the Form.
Private Sub dgv_RowLeave(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dgv.RowLeave
ProgrammaticallyDoRowValidation(dgv.CurrentRow.Index) ' This does validation and more.
' But if btnQuit is clicked, I need to know here, or before RowLeave is
' triggered and NOT do this row validation.
' ...
If Me.ActiveControl Is btnQuit Then
' do something
End If
End Sub

How to call GridView RowEditing with button outside the GridView?

I have a gridview that is populated and a button outside the gridview that I want to enable editing on the selected row when clicked. I have this in the code behind. What goes in the btn_click event to invoke the grid view editing?
Protected Sub GridView1_RowEditing(ByVal sender As Object, ByVal e As GridViewEventArgs)
GridView1.EditIndex = e.NewEditIndex
FillGrid()
End Sub
Protected Sub btnEdit_Click(ByVal sender as Object, ByVal e As System.EventArgs) Handles btnEdit.Click
What goes here??
End Sub
There is a problem with this approach.
"GridView1_RowEditing" is expecting a row index, so it can turn on "EditItemTemplate" accordingly, correct?
But If you want to click on button outside of Gridview and make entire Gridview editable, you shouldn't trigger GridView1_RowEditing, since you don't know what editindex to pass.
You need to implement editable control(textbox) as part of "ItemTemplate", not in "EditItemTemplate".
And visibility of this control would be controlled by the outside button you have created, which will flag the visibility on / off.
Please review following link, this demonstrates how it should be implemented.
http://highoncoding.com/Articles/219_GridView_All_Rows_in_Edit_Mode.aspx

Windows Form Cancel Button Not Working

I have a Visual Studio, Visual Basic form that includes an OK button and a Cancel button.
What I want to do is have the OK button save the options that the user chooses and of course the Cancel button discarding them and returning them to their previous values.
But what I'm noticing is that as I'm debugging the form, the values are being saved regardless of whichever button I'm choosing. On the form's properties, I have declared that indeed the CancelBtn is the CancelBtn and that the OK button is the OK button, but the values are still being saved regardless.
Is there a better way to do what I would like this form to do?
EDIT:
Here's the code so far for the two buttons, both are being set to close the window. AcceptOption should save the values and CancelOption should just close the form. I'm sorry if this isn't done well but the FAQ's that I found only mention changing the properties of each button and nothing about the code.:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles AcceptOptionBtn.Click
' Save the Options
Me.Close()
' Close the form
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles CancelOptionBtn.Click
' Close the form
Me.Close()
End Sub
Don't change "the values" until the user clicks the Save button.
The form should be preloaded with a copy of the values you would like to update.
The Cancel button should just close the form.
The Save button should cause "the values", not the forms copy, to be updated.
EDIT:-
In regard to this question, there is nothing wrong with the code you have posted. Are the right handlers being called for the right button clicks? Are the form's AcceptButton and CancelButton properties set to the right buttons?
What data are your editing controls bound to, if at all?
There's nothing magical about OK and Cancel buttons. They're just... buttons. If you save your data every time a change is made, the Cancel button won't magically "unsave" them. Though if you save changes in the OK button's Click event handler, then clicking the Cancel button obviously won't save your changes. To help you further we'd need to know how you save your data.
Edit:
From looking at your code, I think you're passing data directly to your form, without performing a copy of your objects. Therefore if you modify this data, it will also be changed in the parent form. By working with a copy of your data in this form, any changes which aren't saved will be correctly discarded.
Your event handler for the cancel button should look like this:
Private Sub btnCancel_Click(sender As System.Object, e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
Your event handler for the OK button should look like this:
Private Sub btnOK_Click(sender As System.Object, e As System.EventArgs) Handles btnOK.Click
SaveSettings 'call a routine to save the settings the user has entered
Me.Close()
End Sub
It is as simple as that!
If you open your form like
myForm.showdialog()
you don't have to define the handler for the close button click event, it is automatically handled; just set the 'DialogResult' property for the button
btnCancel.DialogResult = DialogResult.Cancel
Also if you want to close the form when ESC is pressed then set the 'CancelButton' property for the form:
myForm.CancelButton = btnCancel
On the other hand if you open the form like
myForm.Show()
you do need to specify the action(s) to take on the close button click event as indicated here, ie:
Private Sub BtnCancelClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnCancel.Click
Close()
End Sub
I was having the same issues. As soon as I use My.Settings.Blabla = Blabla.value, it gets saved even if I haven't used My.Settings.Save() which makes My.Settings.Save() completely pointless as far as I can tell.
I ended up taking up Jordell's advice: Don't change "the values" until the user clicks the Save button but it wasn't too clear for me how to go about it.
I ended up using temporary variables in all my settings subs instead of the user My.Settings.UserConfigs. Only when I was in the OK sub did I call
My.Settings.UserConfigSetting = temporary_UserCofigValue
Here is an example from the code I was working on:
Private Sub btnOptionsThemeLB_Back_Update_Click(sender As System.Object, e As System.EventArgs) Handles btnOptionsThemeLB_Back_Update.Click
If (tempOptionsThemeLB_Back = Nothing) Then
tempOptionsThemeLB_Back = Me.btnOptionsThemeLB_Back.BackColor
End If
tempOptionsThemeLB_Back = RGBToColor(txtbOptionsThemeLB_Back_Red.Text, txtbOptionsThemeLB_Back_Green.Text, txtbOptionsThemeLB_Back_Blue.Text, tempOptionsThemeLB_Back)
Me.btnOptionsThemeLB_Back.BackColor = tempOptionsThemeLB_Back
End Sub
And only withing the Ok sub did I call My.Settings.
'Theme Section
My.Settings.colorBtnBack = tempOptionsThemeLB_Back

Can't set focus on a Windows Forms textbox

I can't seem to get input focus on a textbox when a tab page first comes up (I'm using Windows Forms, VB.NET 3.5).
I have a textbox on a panel on a tab page, and I want the focus to be on the textbox when the tab page comes up. I want the user to be able to start typing immediately in the focused textbox without having to click on the textbox. I have tab stops set in the order I want and the textbox is the first tab stop. The tab stops work except that when the tab page comes up the focus is not on the textbox, i.e. the one that's first in the tab order.
In the Enter event handler of the tab page I call the Focus method of the text box, but it returns False and does nothing, no error messages. I know I can access the text box because
at the same point in the code I can set the text of the text box.
If it matters, the layout of the tab page is a little complicated:
frmFoo/TabControl1/TabPageX/Panel1/Panel2/TextBox1
I want to set the focus on TextBox1.
What's the best way to get the focus on the desired textbox?
If setting focus is the best way, why is the textbox.Focus() method failing?
I would assume you are attempting to set focus in the form load event handler? If so, you need to do a Me.Show() to actually create the onscreen controls before focus can be set. Something along the lines of:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Show()
Application.DoEvents()
TextBox1.Focus()
End Sub
If you don't do the Me.Show(), the form is NOT displayed until the load event is complete.
For the tab control, handle the _SelectedIndexChanged event:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) _
Handles TabControl1.SelectedIndexChanged
If TabControl1.SelectedTab.Name = "TabPage1" Then
TextBox2.Focus()
End If
If TabControl1.SelectedTab.Name = "TabPage2" Then
TextBox4.Focus()
End If
You will still want to set the initial focus in the load event as shown above if the first field selected is to be the textbox on the tab control.
Try either:
Me.ActiveControl = TextBox1
or
TextBox1.Select()
Do the control.Focus() in the OnShown event. You don't need any of the DoEvents logic which didn't work for me anyway.
You Should Use Selected Event of TabControl
Private Sub TabControl1_Selected(ByVal sender As Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
If e.TabPage.Name = "TabPage1" Then
TextBox1.Select()
End If
End Sub
As I have Checked in Both TabControl.Selected and TabPage.Enter Event can set Select TextBox. I think there is some other elements stealing focus. please varify
Any of the solutions I found online don't solve the problem when the control is on a tab page.
However, this works:
(1) set the TabIndex of the control to 0.
(2) In your code that handles the tabpage event, do the following:
SendKeys.Send("{TAB}")
If SendKeys doesn't seem to be a valid statment, make sure you have the following import at the top of your code file:
Imports System.Windows.Forms
I found that the TabControl gets the focus when the Selected event completes. To make this work I used the Paint event of the TabPage to set the focus of the desired object.
Private Sub TabChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Tab1.Paint, Tab2.Paint, Tab3.Paint
Select Case sender.Name
Case "Tab1"
Textbox1.Focus()
Case "Tab2"
T3extbox2.Focus()
Case "Tab3"
Textbox3.Focus()
End Select
End Sub
Try the Activated event of the form like this:
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
'SendKeys.Send("{TAB}") this line works too
TextBox1.Focus()
End Sub
That is guaranteed to work.
I once had the same problem but i solved it using the Me.activate() function.