Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Saving data on a user control that is placed inside a tab control 1

Status
Not open for further replies.

steve1rm

Programmer
Aug 26, 2006
255
GB
Hello,

VS 2005

I have user control that has been placed on many tab pages. The tab control is not part of the user control. The user control consists of text boxes and combo boxes

The user can add new tabs and enter the data into it the user control and click save which is also part of the user control. However, if they decide to save on a previous tab and then click save it will save the contents of the text boxes and combo boxes that are on the last tab.

This is because the text boxes and combo boxes are taken from the last ones that are created.

How can I select another tab and click save, and save the contents of those text boxes and combo boxes on that tab?

my code for the save button in the user control.
Code:

Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 
Try 
Dim rowsAffected As Integer = Me.TA_IncidentTask1.UpdateTask(_incidentID, Me.cboSupportType.Text, Me.cboUsers.Text, Date.Now.ToLocalTime(), Date.Now, Me.txtTaskDetails.Text, Date.Now, Date.Now.ToLocalTime(), Me.cboStatus.Text, String.Empty, String.Empty, Me.txtCompletionNotes.Text, _taskID) 
Catch ex As Exception 
MessageBox.Show(ex.Message) 
End Try 
End Sub

Many thanks for your help,

Steve
 
Hello again, Steve.

How can I select another tab and click save..[/code]
Programmatically?

Here's one strategy:
1. When the btnSave (this button is INSIDE the usercontrol, right?) is clicked, check the usercontrol's Parent.
2. If it IS the last TabPage of the TabControl then reset its Tag property and go on saving the data.
3. If it is NOT the last TabPage of the TabControl then put something to the Tag property of the last TabPage. Then select the last TabPage.
4. If your usercontrol has a VisibleChanged event, then use it by checking its Parent TabIndex.
5. If it IS the last TabPage of the TabControl, then check its Tag property.

Something like this,
Code:
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
    'For simplicity, declare a control
    Dim lastPage As TabPage = _
    CType(Me.Parent.Parent, TabControl).TabPages(CType(Me.Parent.Parent, TabControl).TabPages.Count - 1)
    'Step 1
    If lastPage.TabIndex = CType(lastPage.Parent, TabControl).TabPages.Count - 1 Then
        Step 2
        lastPage.Tag = ""
        MessageBox.Show("Put the saving code here")
    Else
        'Step 3
        lastPage.Tag = "1"
        CType(Me.Parent.Parent, TabControl).SelectedTab = lastPage
    End If
End Sub


Private Sub UserControl1_VisibleChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.VisibleChanged
    'Step 4
    If Me.Parent.TabIndex = CType(Me.Parent.Parent, TabControl).TabPages.Count - 1 Then
        'Step 5
        If Me.Parent.Tag <> "" Then
            btnSave.PerformClick()
        End If
    End If
End Sub

Good luck!
 
Sorry, the simplicity is not working. Improper logic, I guess.
Use this instead:
Code:
If Me.Parent.TabIndex = CType(Me.Parent.Parent, TabControl).TabPages.Count - 1 Then
	CType(Me.Parent.Parent, TabControl).TabPages(CType(Me.Parent.Parent, TabControl).TabPages.Count - 1).Tag = "1"
	MessageBox.Show("Put the saving code here " & sender.text)
Else
	CType(Me.Parent.Parent, TabControl).TabPages(CType(Me.Parent.Parent, TabControl).TabPages.Count - 1).Tag = "1"
	CType(Me.Parent.Parent, TabControl).SelectedIndex = CType(Me.Parent.Parent, TabControl).TabPages.Count - 1
End If

Regards.
 
Hello Mansii,

Thanks for you time in writing the code.

I have tried it but I could get the tabindex property of the LastPage.TabIndex. It did not show up in the intellisence. In step 2.

Looking through your code, I am not sure that this is the right solution. But I might be wrong.

I was trying something like the following:
'Get the current page the control has been placed on
Dim currentPage As TabPage
currentPage = Me.TabControl1.SelectedTab

'Get the control from that page
Dim uc As Control
uc = currentPage.Controls("QuickTasks")

I think the problem is that every page has a object of the user control placed on it. And the last one that is created is on the last page. So when I click save, it will always save from the text boxes and combo boxes on that last control.

So if I want to save or update any other control and click save it will always save the last one.

Hope you understand the problem clearer.

Many thanks and I gave you a star for you above code.

Steve
 
Hello Mansii,

I tried the second version of your, I don't think it works the way it should.

Many thanks for your help,

Steve
 
I'm so sorry, Steve. I didn't read the situation correctly.
I thought that you want to trigger the last TabPage's btnSave.Click every time a user click the btnSave in other tabpage.

If what you want is one procedure that handles all btnSave in all TabPage, then create a procedure that will do the btnSave.Click Event. Let's Use your code with some modifications:

Code:
'Put this code in the main form as TabControl1 resides, NOT in the usercontrol
Private Sub [green]btnSave_Click[/green](ByVal sender As System.Object, ByVal e As System.EventArgs) ' ==> remove this ==> Handles btnSave.Click
Try
Dim [blue]cboSupport, cboUser, cboStat[/blue] As Combobox
Dim [blue]txtTaskDetail, txtCompletionNote[/blue] As TextBox

'This line will get the control where btnSave resides
'In other word, the UserControl
Dim CurrentPage As YourUserControl = CType(sender, Button).Parent

'Loop through all control in it
For Each ctr As Control In CurrentPage.Controls
   Select Case ctr.GetType.ToString
      Case GetType(TextBox).ToString
          Select Case ctr.Name
             Case "txtTaskDetails"
                 txtTaskDetail = txtTaskDetails
             Case "txtCompletionNotes"
                 txtCompletionNote = txtCompletionNotes
             Case Else
          End Select
      Case GetType(ComboBox).ToString
          Select Case ctr.Name
             Case "cboSupportType"
                 cboSupport = cboSupportType
             Case "cboUsers"
                 cboUser = cboUsers
             Case "cboStatus"
                 cboStat = cboStatus
             Case Else
          End Select
      Case Else
   End Select
Next
'Don't forget to get other values, such as _incidentID and _taskID.
'Second, move the TA_IncidentTask1.UpdateTask to the main form as well. Here, I renamed it it as GlobalUpdateTask
Dim rowsAffected As Integer = GlobalUpdateTask(_incidentID, [blue]cboSupport[/blue].Text, [blue]cboUser[/blue].Text, Date.Now.ToLocalTime(), Date.Now, [blue]txtTaskDetail[/blue].Text, Date.Now, Date.Now.ToLocalTime(), [blue]cboStat[/blue].Text, String.Empty, String.Empty, [blue]txtCompletionNote[/blue].Text, _taskID)
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub

Private Function GlobalUpdateTask(parameters...) As Integer
 'Codes here
End Function

'Last step, perhaps, assign the first Sub to every btnSave
'when you declare a New UserControl.
newUserControl = New UserControl1
AddHandler newUserControl.btnSave.Click, AddressOf [green]btnSave_Click[/green]

Now let's see the result.
Regards.
 
Hello Mansii,

Thanks for your post and writing the code for me, i have just finished. However, I have done it a different way, and used your code a guide.

If you feel there could be improvements then please feel free to comment on the code.

Many thanks for your time and help,

Steve
Code:
 Private Sub btnSaveAndCloes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAndCloes.Click    
        Try
            Dim taskDetails As Infragistics.Win.UltraWinEditors.UltraTextEditor
            Dim completionNotes As Infragistics.Win.UltraWinEditors.UltraTextEditor
            Dim users As ComboBox
            Dim supportType As ComboBox
            Dim status As ComboBox
            Dim taskID As Integer = 0
            Dim uc As Control
            Dim rowsAffected As Integer = 0

            'Update the current incident and then loop through updating all the tasks the belong to this incident
            Me.TA_Incident_dsComponent_Equipment1.Incident_Update(incidentID, Me.cboCustomers.Text, String.Empty, String.Empty, String.Empty, String.Empty, Me.cboCustomers.SelectedValue, String.Empty, String.Empty, "Not Started", Me.txtSubject.Text, Me.cboUsers.Text, Date.Now(), Date.Now.ToLocalTime())

            'Loop through all the tab and get the user control
            For Each tp As TabPage In Me.TabControl1.TabPages
                'Get the user control on this tab
                uc = tp.Controls("QuickTasks")

                'Get all the details for the controls contained the in user control in the current tab.
                taskDetails = uc.Controls("txtTaskDetails")
                completionNotes = uc.Controls("txtCompletionNotes")
                users = uc.Controls("cboUsers")
                supportType = uc.Controls("cboSupportType")
                status = uc.Controls("cboStatus")

                'Get the taskID number of the task on the tab
                taskID = tp.Name
                'Update this task to the database
                rowsAffected = Me.TA_IncidentTask_dsComponent_Equipment1.IncidentTask_Update(taskID, supportType.Text, users.Text, Date.Now.ToLocalTime(), Date.Now(), taskDetails.Text, Date.Now(), Date.Now.ToLocalTime(), status.Text, String.Empty, completionNotes.Text)
            Next tp

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Me.Close()
    End Sub
 
It's always good to know that a problem solved.
Your code looks great for me.

Regards.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top