[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Updating parent form text and data (specifically text box and datagrid) from child form

Posted on 2006-05-12
15
Medium Priority
?
290 Views
Last Modified: 2009-12-16
I don't even know where to start regarding how to do this....so I decided to post to the forum before spending an inordinate amount of time on an inefficient attempt.

I have a windows form with a datagrid (populated from dataview) containing either filtered (using .Rowfilter = ?) or non-filtered rows based on several combo boxes specifying criteria.  I have an 'add new' button that displays a form to create new record.  A new record is added to my SQL Server table through the new dataset used on the data entry form.

What is an efficient way to update the dataset/dataview/datagrid on the parent form. The use of recordsets in MS Access was simple  Forms![formname].grid.Requery.  

I know it won't be that simple but I'm oblivious as to an efficient way to go.

In another example but with a similar issue.  How do you modify the text of a text box on a parent form (or any specific form desired) when it is not the active form?

Thank you all!

Phil
0
Comment
Question by:TSFLLC
  • 5
  • 4
  • 4
  • +1
15 Comments
 
LVL 11

Expert Comment

by:TornadoV
ID: 16668541
0
 
LVL 7

Assisted Solution

by:wtconway
wtconway earned 400 total points
ID: 16668573
First I'd like to address the textbox modification on a form. Here is what the method I read about on accomplishing this. If you think about your forms in .NET, you'll notice they are really just objects of particular classes. Inherited methods from the System.Windows.Forms.Form class cause for it to draw information to the screen. Simple really. So since they are just objects in our project, we'll have to have instantiated references to them. We accomplish this like so:

Let's say you have a module, I generally name mine modVars.vb because i have no clue what to name a module.

Module modVars
  Public Class theForms 'is a class that stores form objects
     Public frmForm1 as Form1 'if you leave your class name for the first form as "Form1"
     Public frmForm2 as Form2 'you get the idea
  End Class

  Public myForms as New theForms() 'this is an instantiated object of the class we just defined.
End Module

Public Class Form1
....blah blah blah
   Sub Form1_Load(blah blah blah) Handles Form1.Load
       myForms.frmForm1 = Me
   End Sub
End Class

you do the same thing in form2's load handler. Then while in any form you can do like so:

myForms.frmForm1.TextBox1.Text = "hey you!"

Lemme know if you have any more questions
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16668644
As for reloading/refreshing the data in the datagrid. Do you have a sub routine or a function that handles loading in the data and binding it to the datagrid? If so, just call that routine again and the data should pop in there like expected. Make sure that in the routine you reset certain controls to either defaults or blank values so the user doesn't get confused.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Expert Comment

by:TornadoV
ID: 16668704
Here is a great example on cross-form data binding:

http://www.code-magazine.com/article.aspx?quickid=0409041&page=4 
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16669109
What version VB.Net?
0
 

Author Comment

by:TSFLLC
ID: 16670303
Version 2003.
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16670398
Have you tried my suggestion on the multiple forms?
0
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 1600 total points
ID: 16670453
If you only ever use one instance of your forms at a time then look at using the Singleton approach.

I just had a lengthy discussion here in this PAQ:
http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21845440.html
0
 

Author Comment

by:TSFLLC
ID: 16670830
Man!  I just lost everything I typed in here for you guys.........I'm disgusted.  What I write may have a little edge to it now.  Here goes again.

wt,

I did test your putting this code in a Public class.  Works great.  Was originally concerned about more public variables.  Then I realized I was only looking at 5-6 for that number of forms I update fields on.

That resolves my text/combo box issues for sure.  But theoretically shouldn't I be able to reference almost ANYTHING on a non-active form using myForms.FormName.? including datasets/dataviews/rows from my datagrid if necessary.  To answer you question above, yes I do load data into my grid from a sub routine BuildMainTable().  Then I use dv.RowFilter to filter it based on additional fields available for filtering.  I attempted to put something at the bottom of my SaveRec() routine like this...myForms.FormName.BuildMainTable()....but it apparently doesn't work like that else I don't know the appropriate method.  When I begin to type myForms.FormName.... it displays all of the fields on the form, but I don't know how the rest of the line should go if I am able to reference/affect the dataset/dataview.

Mike (Idle_Mind) posted a comment to another open question I have that I think was mean for here because it deals with my datagrid/dataset.  I am going to copy it here:

There are several ways to go about it...here is one using a Delegate to point to your main forms function:

(assuming Form1 was your main form)

    Module Module1

        Public Delegate Function GetMainTableDelegate() As DataSet
        Public MainTable As GetMainTableDelegate

    End Module

    Public Class Form1
        Inherits System.Windows.Forms.Form

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            MainTable = New GetMainTableDelegate(AddressOf Me.GetMainTable)
        End Sub

        Private Function GetMainTable() As DataSet
            ' ... your code ...
        End Function

    End Class

Now from ANYWHERE you can use "MainTable" to refer to the DataSet:

    MainTable.XXX = "something"
    MainTable.XXX(yyy)



I would like to be able to affect ALL fields/grids/datasets/dataviews/etc. on a form with one method instead of using both yours and Mike's.  However, if it's not possible that's OK.  I'm going to bump the points up and probably divide them.

Regardless, you guys have made my life of VB grinding smoother.

Please respond.  Thanks!

Phil
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16670896
Definitely look at the Singleton approach I mentioned above...you can use it to solve most of your problems.

It will allow you to use syntax like Form1.Instance.XXXX from ANYWHERE without explicitly declaring Public variables in a Module.
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16670898
Phil,

You should be able to access objects on the form just like you can with public/friend sub routines and methods. For example,

on Form1
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents DataSet1 as System.Data.DataSet

myForms.Form1.Button1.Text = "hey" 'this should work
myForms.Form1.DataSet1.Rows.Blah.Blah 'you get the idea

That should work. Just make sure that if you are declaring/defining these objects at runtime to make them public or friends. Ok?
0
 
LVL 7

Expert Comment

by:wtconway
ID: 16670925
Well with my version you can accomplish the same thing. Just so long as the variable is a Friend or Public variable, the "myForms" object can access it from any other form. And if you're already using a module then why not?

Not fighting for points here, Lord knows Idle_Mind has enough and I have plenty to stay afloat. Just trying to find the best solution for your purposes. The approach I use has always worked great for me.
0
 

Author Comment

by:TSFLLC
ID: 16671003
I'm going to review this 'Singleton' approach this weekend.

If it eleviates the necessity of Public variables even if only 5-6 (5-6 here, 5-6 there, 5-6 everywhere eventually) it would be more appealing to me.

Will post Sunday/Monday.

Thanks all!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16671240
wtconway...yes, you can access everything on the form with the correct visibility...BUT only if you have a reference to it, either by passing references around, or by using a Module with global variabes.

My point is that you can move away from having global variables (or a global instance of a Class) in a module that you must "set", or passing references about, when you use the Singleton approach.

On a side note, in VB.Net 2005 you can use the forms name to access it without passing a reference:

    Form1.Show()
    Form1.Text = "Hello World!"

The above would modify the caption of the "default instance" of Form1 and can be called from anywhere.
0
 

Author Comment

by:TSFLLC
ID: 16679790
Hi guys!

wtconway...I had originally implemented the use of myForms.x with the use of public variables.  Over the weekend I reviewed and implemented the Singleton method where form instances are created.  The fact that I don't have to create public declarations in my Module.vb and can create a small unique class from within one or several forms to accomodate access to objects on that form is by far the most appropriate way to go.

However, your code works flawlessly and I will give you points for effort.

Bob...I also implemented the code you posted to this other question regarding Public Delegate Function....

BY FAR!!!!.....the combination of the Singleton method and the setup of Public Delegate Functions (in my case for DataSet, DataTable & DataView) will allow me to move forward with my design and I already see 6-7 forms that the combination of both will work flawlessly.

For the purpose of helping others with this same I have re-defined the problem and provided my code for the answer(s):

1)  Be able to reference objects (text fields, labels, datagrid, etc.) on parent form while child form is active form.
2)  Be able to invoke subs/functions on parent form while child form is active form.
3)  Specifically...display records on BrowseForm in datagrid.  Multiple combo boxes allow filtering of grid using dataview.rowfilter using button with UpdateGridRowFilter() function.  DoubleClick datagrid row to populate child data entry form.  Update fields on data entry form and call Public Function UpdateMainTable() and refresh parent form datagrid.


-->> From my windows form BrowseForm….

#Region " Windows Form Designer generated code "

    Private Shared WithEvents BrowseFormInstance As BrowseForm

    Public Shared ReadOnly Property Instance() As BrowseForm
        Get
            If BrowseForm.BrowseFormInstance Is Nothing Then
                BrowseForm.BrowseFormInstance = New BrowseForm
            End If
            Return BrowseForm.BrowseFormInstance
        End Get
    End Property

    Private Shared Sub Form_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles BrowseFormInstance.Closed
        BrowseForm.BrowseFormInstance = Nothing
    End Sub

    Private Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub


    Delegate Sub dlgRefreshBrowseFormGrid()
    Public Shared RefreshBrowseFormGrid As dlgRefreshBrowseFormGrid

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BrowseDataSet = New GetBrowseDSDelegate(AddressOf GetBrowseDataSet)
        BrowseDataTable = New GetBrowseDTDelegate(AddressOf GetBrowseDataTable)
        BrowseDataView = New GetBrowseDVDelegate(AddressOf GetBrowseDataView)
        BuildTablesAndViews()
        BuildGrids()
        BuildComboBoxes()
        RefreshBrowseFormGrid = New dlgRefreshBrowseFormGrid(AddressOf UpdateGridRowFilter)
    End Sub


-->> Code from function in my Module1.vb executed from child data entry form….

    Public Function UpdateMainTable(ByVal xTable As String)
        Dim da As New SqlDataAdapter

        Try
            ' Update SQL2000 tables with Child Form dataview
            ' GetMainEntryTableDA - contains stored procedures for Select(True)/Delete(False)/Update(False)
            ' xTable being the appropriate table from BrowseForm & Data Entry Form
            da = GetMainEntryTableDA(False)      ‘ If SelectOnly = True, Select, otherwise Add/Delete/Update record
            da.Update(dsMainEntryTable, xTable)
            dsMainEntryTable.AcceptChanges()
            ' Repopulate BrowseForm ds/dt/dv
            BrowseDataSet()
            BrowseDataTable()
            BrowseDataView()
            ' Refresh datasource of BrowseForm datagrid
            BrowseForm.Instance.grdTable.DataSource = dvBrowseTable
            ' Update filter of BrowseForm datagrid
            BrowseForm.Instance.RefreshBrowseFormGrid.Invoke()
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.OKOnly Or MsgBoxStyle.Critical)
        End Try
    End Function



Thanks Bob and wtconway!

Phil
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question