Load data from one form to another

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 7559 Views - Last Post: 27 October 2011 - 02:14 PM Rate Topic: -----

#1 manuu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-October 11

Load data from one form to another

Posted 23 October 2011 - 06:50 AM

Hi all,
im new to vb.net.i have two forms.first form contains two textboxes and a view button.when i click my view button second form open and it displays datagrid contains values.this works properly.but i want do some more.
when i double click the datagridviewcell of second form how to pass the particular cell value to my first form's textboxes.
Is This A Good Question/Topic? 0
  • +

Replies To: Load data from one form to another

#2 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Load data from one form to another

Posted 23 October 2011 - 07:26 AM

You should look into declaring and raising custom events.

Essentially the second form should have a a custom event that is raised whenever a datagridviewcell is double clicked (you will need to handle the double click event of each and then raise the new event).

When the second form is created, at the same time, the first form should subscribe to the new event in the second form. Then add a method to deal with the event (just like a button click), but to change the values of the textbox's instead.

You may also want to look into customer event arguments, where you can add the value of the datagridviewcell that is pressed into the args, for access in the event handler in the first form.

It sounds a bit complicated, but read up on creating events and do some testing.

There is also this great tutorial about passing values from form to form. Although it is in C#, using the previous link as well, it may be of some help as it runs through the process of creating custom events.

This post has been edited by Ryano121: 23 October 2011 - 07:29 AM

Was This Post Helpful? 2
  • +
  • -

#3 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Load data from one form to another

Posted 23 October 2011 - 07:29 AM

Sure you can, just name the form before its control like Form1.DatagridView1.... then other things is doe as normal wrong reply!!

This post has been edited by smohd: 23 October 2011 - 01:20 PM

Was This Post Helpful? -3
  • +
  • -

#4 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Load data from one form to another

Posted 23 October 2011 - 07:33 AM

@smohd

Why would you be suggesting making the control public?

It breaks the rules of encapsulation completely.

Why should other forms have complete access to controls in different forms. In that case they have complete control over it. For all we know someone else could come along and mess with the control and make the program break completely.

Values encapsulated in the controls themselves should be exposed to other forms via properties, not the entire control.

This post has been edited by Ryano121: 23 October 2011 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Load data from one form to another

Posted 23 October 2011 - 08:02 AM

@Ryano121: Thanks but since when form controls have become to be private? they are always friend,

Quote

Values encapsulated in the controls themselves should be exposed to other forms via properties, not the entire control.
and we always use properties of the control to get the values, because when we say TextBox1.Text here we have used the property text of the control, but since controls are friend we can access them using form name because of the way VB.NET treats them(different from C#)
Was This Post Helpful? 1
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: Load data from one form to another

Posted 23 October 2011 - 08:08 AM

This is true, but still not a good idea. Yes you do use TextBox1.Text, but I was talking more about the properties that the control itself exposes. The position for one. Is it really a good idea to let other forms have complete access to properties like these in controls that they have nothing to do with? In the position example, another form could easily come along and move the control to not even appear inside the form, or even hide it. No compiler errors, but a huge potential issue.

Thanks for pointing out that controls are friend in VB.NET. However I really see no sense in this whatsoever. In my opinion they really should be private.

This post has been edited by Ryano121: 23 October 2011 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Load data from one form to another

Posted 23 October 2011 - 12:58 PM

View Postsmohd, on 23 October 2011 - 03:29 PM, said:

Sure you can, just name the form before its control like Form1.DatagridView1.... then other things is doe as normal


This referring to the Default instance of Form1, in a large majority of the cases this isn't you want.
You should use an instance of the form.

Friend is used inheritance, it dictates what is shared to the descendants of the class.

This post has been edited by AdamSpeight2008: 23 October 2011 - 01:01 PM

Was This Post Helpful? 1
  • +
  • -

#8 trevster344  Icon User is offline

  • The Peasant
  • member icon

Reputation: 224
  • View blog
  • Posts: 1,505
  • Joined: 16-March 11

Re: Load data from one form to another

Posted 23 October 2011 - 01:01 PM

Whoops sorry smohd.
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Load data from one form to another

Posted 23 October 2011 - 01:07 PM

They can anything you want Friend, Protected Friend, Public, Private.

Each form should be independent and raise events (possibly customs) to do pass things to other forms.
Was This Post Helpful? 1
  • +
  • -

#10 smohd  Icon User is offline

  • Critical Section
  • member icon


Reputation: 1819
  • View blog
  • Posts: 4,627
  • Joined: 14-March 10

Re: Load data from one form to another

Posted 23 October 2011 - 01:12 PM

Thanks for the corrections, I think that pass out of my knowledge for a while... good to get new knowledge :)
Was This Post Helpful? 1
  • +
  • -

#11 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Load data from one form to another

Posted 23 October 2011 - 01:24 PM

That's because thinks Forms are special thus treat them as special, and forget that they are classes like any other .net class.

Default Instance were added as a bone to old style vb6 coder, that where / are struggling with the concepts used in .net, especially the OOP approach. They make it too easy to fall back in vb6 style of coding, and are even worse across multiple threads. As each default is a Thread Local Instance, so you are referring to a different instance of the Form, depending of which thread your on.

Form1.Title on one thread isn't the same Form1.Title on a different thread.
Was This Post Helpful? 2
  • +
  • -

#12 manuu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-October 11

Re: Load data from one form to another

Posted 23 October 2011 - 08:25 PM

As im new to vb.net,i dnt know how to create and raise custom events and also dnt know the purpose for it.if u can plse explain and code it.
Was This Post Helpful? 0
  • +
  • -

#13 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2257
  • View blog
  • Posts: 9,445
  • Joined: 29-May 08

Re: Load data from one form to another

Posted 24 October 2011 - 08:11 AM

Tutorial
Was This Post Helpful? 1
  • +
  • -

#14 manuu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 23-October 11

Re: Load data from one form to another

Posted 24 October 2011 - 11:57 PM

hi this is my code

form1
Public Class frmUnit

    Private Sub BtnUView_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUView.Click
        Dim frmUnitView As New frmUnitView
        frmUnitView.Show()
       End Sub
End Class



form2
Public Class frmUnitView

  Private Sub frmUnitView_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim connectionstring As String
        Dim con As SqlConnection
        Dim adapter As New SqlDataAdapter
        Dim ds As New DataSet
        connectionstring = "Data Source=PC\SQLEXPRESS;Initial Catalog=p;User ID=sa;Password=123"
        con = New SqlConnection(connectionstring)
        Dim sql As String = "select * from emp"
        Dim sqlcom As SqlCommand
      
        Try
            con.Open()
            sqlcom = New SqlCommand(sql, con)
            adapter.SelectCommand = sqlcom
            adapter.Fill(ds, "sql")
           
            Dim j As Integer
            For j = 0 To ds.Tables(0).Rows.Count - 1
               DgvUnit.Rows.Add()
                DgvUnit.Rows(j).Cells("nam").Value = ds.Tables(0).Rows(j)(0).ToString()
                DgvUnit.Rows(j).Cells("sname").Value = ds.Tables(0).Rows(j)(1).ToString()
               Next
        Catch ex As Exception
        End Try
    End Sub



But i want to do more....when i double click the datagridviewcell of second form or if using a button in secondform how to pass the particular cell value to my first form's textboxes.

This post has been edited by smohd: 25 October 2011 - 12:04 AM
Reason for edit:: Please use [code] tags when posting codes

Was This Post Helpful? 0
  • +
  • -

#15 Elda  Icon User is offline

  • D.I.C Regular

Reputation: 31
  • View blog
  • Posts: 314
  • Joined: 30-December 10

Re: Load data from one form to another

Posted 25 October 2011 - 12:15 AM

Maybe you can do like this.

Private Sub datagridview1_...
dim dc as integer
form1.Show()
        dc = dgv1.CurrentRow.Index
        datafill(dc)
 End sub




Public Sub datafill
    r = t.Rows(i)

'You can do stuff here
form1.textbox1.text =.......

End sub



Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2