Author Topic: Transfer string to textbox on another form  (Read 3463 times)

0 Members and 1 Guest are viewing this topic.

TJK44

  • Guest
Transfer string to textbox on another form
« on: November 11, 2011, 04:08:07 PM »
This isn't related to AutoCAD, but .Net in general. I have been staring at this for 30min trying all sorts of ways and it's not working. Thought I would ask here. I am pulling data from a datagrid on form1 and placing the text into a textbox on another form (form2). Here is my code, not sure why it's not writing to form2.

Code: [Select]
    Private Sub dgvParts_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvParts.CellContentClick
        Dim dgv As DataGridView = CType(sender, DataGridView)
        'Try


        If dgvParts.Columns(e.ColumnIndex).Name = "btnSelect" Then
            Dim i, rowcount As Integer
            i = dgvParts.Rows.Count
            rowcount = i + 1

            Dim frm1 As New Form2

            frm1.txtPartNum.Text = dgvParts.Item(1, e.RowIndex).Value.ToString
            frm1.txtPartDesc.Text = dgvParts.Item(2, e.RowIndex).Value.ToString

            Me.Close()
        End If
        'Catch ex As Exception
        '    MsgBox("An error occurred, please contact IT!")
        'End Try
    End Sub

When i debug the values I want are getting into the .text part of the textboxes but nothing shows up in the textboxes.

Thanks in advance,

Ted

exmachina

  • Guest
Re: Transfer string to textbox on another form
« Reply #1 on: November 11, 2011, 04:40:56 PM »
Because frm1 (Dim frm1 As New Form2)  only live in "Sub dgvParts_CellContentClick". See:
http://msdn.microsoft.com/en-us/library/76453kax%28v=VS.90%29.aspx

Sorry, but i only speak Spanish.
« Last Edit: November 11, 2011, 04:44:20 PM by exmachina »

zoltan

  • Guest
Re: Transfer string to textbox on another form
« Reply #2 on: November 11, 2011, 04:43:48 PM »
You are creating a new instance of Form2.  Is that the instance that is currently shown?  Are you going to show the form after the text box if filled?

exmachina is right.  frm1 needs to be member of the class that dgvParts_CellContentClick is in.

TJK44

  • Guest
Re: Transfer string to textbox on another form
« Reply #3 on: November 11, 2011, 05:15:10 PM »
For the time being I closed form2 before performing the search with form1 then using frm1.show and then appending the text to the textboxes.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Transfer string to textbox on another form
« Reply #4 on: November 11, 2011, 06:05:45 PM »
There are several solutions, but it depends upon your particular needs.

I personally don't like managing forms like that .. changing control values from a different form ... you can get unexpected results.

I'd do this:

In Form2, create a structure called part and a property to capture the current part:

Structure Part
Code: [Select]
'Structure to hold the part, along with various properties
    Structure Part
        Dim Name As String
        Dim Number As String
        Dim Description As String

'What happens when we create a new part?
'i.e. Dim ThisPart As New Part("PartName", "PartNumber", "Description")
        Shared Sub New()
        End Sub

        Sub New(ByVal PartName As String, Optional ByVal PartNumber As String = "", Optional ByVal PartDescription As String = "")
            Name = PartName
            Number = PartNumber
            Description = PartDescription
        End Sub
    End Structure

Property CurrentPart
Code: [Select]
'Private variable to hold the current part
'Note, simply creating a new part does
'not place it as the current part
    Private _m_Current_Part As Nullable(Of Part)

'CurrentPart properties
    Public Property CurrentPart
        Get
            If (_m_Current_Part Is Nothing) Then
                Return New Part
            Else
                Return _m_Current_Part
            End If
        End Get
        Set(ByVal value)
            _m_Current_Part = value
        End Set
    End Property

In Form1 use the following:

Code: [Select]
Dim frm2 As New Form2
Dim thisPart As New Form2.Part(PartName, PartNumber, Description)
frm2.CurrentPart = thisPart

Now in Form2, you can simply get the values for the textboxes:
Code: [Select]
tbName.text=_m_Current_Part.Name
tbNumber.text=_m_Current_Part.Number
tbDescription.text=_m_Current_Part.Description

Good luck!
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

exmachina

  • Guest
Re: Transfer string to textbox on another form
« Reply #5 on: November 11, 2011, 07:22:15 PM »
There are several solutions, but it depends upon your particular needs.

I personally don't like managing forms like that .. changing control values from a different form ... you can get unexpected results.

I'd do this:

In Form2, create a structure called part and a property to capture the current part:
...

Are you sure? Why do I have to create a structure? It seem you need to learn to program Vb.net, C#, delphi or vb6...I do not understand why you say this


Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Transfer string to textbox on another form
« Reply #6 on: November 12, 2011, 01:09:45 AM »
Why do I have to create a structure?

You do not need to create a structure. I said this is how I would manage moving data between different forms. It is also how a professional programmer would handle it.

It seem you need to learn to program Vb.net, C#, delphi or vb6...I do not understand why you say this

I am Microsoft certified in C# and .NET, and I am working on the C++ certification now. I also have been programming for 32 years, 17 of those years have been professionally. I've written software for thousands of clients all over the world ... so I know quite a bit about programming.

The reason I suggested a structure and a property is because of coding standards. It is generally not acceptable to change a class element directly, for example, to change the properties of a form textbox from another form or class. The changes should be handled by a method within the class to minimize errors from inadvertently changing something that should not have been changed. The best way to handle this in VB is through a public property or public method. (Public methods in a form should also be used in a limited way) That is why I suggested using a property.

This leads to the part about using a structure. Since there is more than one item being changed in the foreign class, and those items are all related, it becomes a natural solution to pass a single data structure than to pass several bits of data.

The structure handles the organization of the part and it is intuitive. This makes passing part information to other parts of the program much easier and you only need to pass one structure containing all the part information.

It certainly is more code, but it almost certainly will mean the code will be programmer friendly and portable, because when you see 'Part', you know exactly what it is and what it is being used for.

As far as the problem with scope for frm2, it depends upon other factors as to whether the data being passed will remain in scope after form1 is destroyed or the current function exits. If the data is being passed ByRef, then once the calling function is out of scope, the data sent is also out of scope, therefore the data ceases to exist. If the data is being passed ByVal, then all of the data is copied into a new memory location and remains in scope, regardless of the state of the original data.

I'd be more concerned with calling a modeless form from a form that can be destroyed before the modeless form is destroyed. It can create some real nasties. All I can say, is it is a good thing .NET is managed code.

Now that I look at the provided code a bit closer, I also see that the original poster is apparently creating a new form every time someone clicks in a DataGridView cell, but the new form is never shown. This would mean that frm2 goes out of scope when the event handler completes. I also noticed that the form containing the DataGridView is being destroyed when the click event handler completes. At this point frm2 is destroyed with form1.

Depending upon the desired effect, I might suggest, instead of Me.Close, using Me.Hide and then open frm2 as a modal form with Form1 as its parent. Then when form2 is closed, use its parent property to show the previous form... Me.Parent.Show() .. this ensures you return to the same location that you left. Now that you do that, instead of
Code: [Select]
Dim frm2 As New Form1

This will resolve the scope issue for frm2
Code: [Select]
Static frm2 As New Form1

I offered the solution as a gift. The original poster can accept it or they can reject it. I have no problem with it either way.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

fixo

  • Guest
Re: Transfer string to textbox on another form
« Reply #7 on: November 12, 2011, 05:43:05 AM »
My 2 c (it's lazy and non-professional way)
Without explanations, sorry
Add Module:
Code: [Select]
    Module Module1

        Public value1 As Object
       '' declare other values here
    End Module

Then in the Form1 on cell click event
Code: [Select]
    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Dim dgv As DataGridView = CType(sender, DataGridView)

        If e.ColumnIndex = 2 Then '<-- put you column index here
            value1 = dgv(2, e.RowIndex).Value
           '' add other values here
        End If

        Dim frm1 As Form2
        frm1 = New Form2

        frm1.Show()
        frm1.TextBox1.Text = value1.ToString

        Me.Hide()

    End Sub

You can use the same code for the back way
in the next form event