How to call GridView RowEditing with button outside the GridView? - vb.net

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

Related

TabCard event when a user has the intent to change tab

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

Refresh dgv works only with the activated event, why?

I have two forms both with a dgv connected to two different datasets. When i change my data in one of the datasets i have to refresh the other one (i know the solution is not the best). After trying many many different ways of rebinding, refreshing and so i found the following code working:
Private Sub View_Activated(byval sender as Object, _ Byval e as
System.Eventargs) Handels MyBase.Activated Table_Load(Nothing,
Nothing) End Sub
Private Sub View_Load(byval sender as Object, _ byval e as
system.eventargs) Handels Mybase.Load
Me.TableAdapter.Fill(Me.Dataset.Table) End Sub
After Chaning things in dgv1 i can see the changes in dgv2 with these commands. BUT when i change mybase.activated to mybase.shown or something else it is not working anymore. Apparently this only works when i use the event activated, but i have no idea why?
BTW. I totally dont understand why the dgv is updated with the new data when i have my activated-event but when i comment the activated-event out the dgv is not updated.
The form's Activated event fires when the form window gets focus. The Load event only fires when the form is initially loaded.
When you make a change on one form, and change focus to the second form, Activated fires.

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

how to focus a specific textbox in vb.net

I'm making a desktop application in vb.net. When I click the back button (placed by me) on any form it hides current form and shows previous form but when i go again to the form from which i had hit back button then the focus cue still remains on the back button but I want the focus to be on the first textbox on that form or any specific button on which I want.....How can i achieve this...
I have already used some code to shift focus from one textbox to another when i press enter key...but it doesn't work in the above mentioned case....I'm using vb.net in visual studio 2005...
This is the code i'm using for shifting focus among textboxes
Private Sub party_code_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles party_code.KeyDown
If e.KeyData = Keys.Return Then
party_name.Focus()
End If
End Sub
Can anyone help me on this????
You need to add a handler for either the form's Activated event:
Private Sub Form1_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
party_name.Focus()
End Sub

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.