Resolved How to change TextBoxes Variables

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I have 8 Buttons and related TextBoxes that when each of them runs then a DataGridView come up and retrieve records from DB but I have only one method to get value from DataGridview when any rows of it double clicks so far so good but the problem is how I can change Textbox's variables when it runs with their own variable with below method?
Private Sub dgvGroups_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles dgvGroups.CellMouseDoubleClick
        Dim strValue As String = String.Empty
        If dgvGroups.SelectedRows.Count > 0 Then
            strValue = dgvGroups.SelectedRows(0).Cells(0).Value.ToString
        End If
        Main.txtBtnGroup1.Text = Trim(strValue)      ''''I need to change txtBtnGroup(NUMBERS 1 to 8)
    End Sub
 
Last edited:
Hi,

I am sure we will get there but you are going to have to help me out here since its your code not mine. So, looking back at that Picture you uploaded in Post No.5 can you please post ALL the code that you now have in:-

1) The Stock Type Button ONLY on the Main Form
2) All the code that belongs to the Group Codes Browse Form

Hopefully I should then be able to see what's going on.

Cheers,

Ian
 
Main Form's Class
Imports System.Data.SqlClient
Public Class Main
    Private Sub txtStockCode_ButtonCustomClick(sender As Object, e As EventArgs) Handles txtStockCode.ButtonCustomClick
        CodeBrowse.ShowDialog()
    End Sub

    Private Sub txtBtnGroup1_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup1.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp1"
        Dim myDGVForm As New Groups(txtBtnGroup1)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup2_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup2.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp2"
        Dim myDGVForm As New Groups(txtBtnGroup2)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup3_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup3.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp3"
        Dim myDGVForm As New Groups(txtBtnGroup3)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup4_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup4.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp4"
        Dim myDGVForm As New Groups(txtBtnGroup4)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup5_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup5.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp5"
        Dim myDGVForm As New Groups(txtBtnGroup5)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup6_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup6.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp6"
        Dim myDGVForm As New Groups(txtBtnGroup6)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup7_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup7.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp7"
        Dim myDGVForm As New Groups(txtBtnGroup7)
        myDGVForm.ShowDialog()
    End Sub

    Private Sub txtBtnGroup8_ButtonCustomClick(sender As Object, e As EventArgs) _
        Handles txtBtnGroup8.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp8"
        Dim myDGVForm As New Groups(txtBtnGroup8)
        myDGVForm.ShowDialog()
    End Sub
End Class



Second Form which called Groups
Imports System.Data.SqlClient
Public Class Groups

    Private textBoxToBeUpdated As TextBox
    Friend strLinkOfGroupSelection As String
    Dim sConn As New SqlConnection
    Dim dt As New DataTable
    Dim ds As New DataSet

    Public Sub New()
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
    End Sub

    Public Sub New(ByVal selectedTextBox As TextBox)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        textBoxToBeUpdated = selectedTextBox
    End Sub

    Private Sub Groups_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        sConn.ConnectionString = "Data Source=PC-N39;Initial Catalog=Esitas01;Persist Security Info=True;User ID=sa;Password=sas"
        sConn.Open()
        Try
            'Add 2 columns and column names to the datatable
            Dim myTable As DataTable = New DataTable("MyTable")
            myTable.Columns.Add(New DataColumn("Group Code"))
            myTable.Columns.Add(New DataColumn("Description"))

            Dim cmd As New SqlCommand("Select * from " & strLinkOfGroupSelection & "", sConn)
            '("SELECT * FROM tablename WHERE (Customer=" & RichTextBox1.Text & ")", sConn)
            Dim dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.Default)
            'Add rows to the datatable for every match found in the database
            Dim myRow As DataRow
            While dr.Read()
                myRow = myTable.NewRow
                myRow.Item(0) = dr(0)
                myRow.Item(1) = dr(1)
                myTable.Rows.Add(myRow)
            End While
            dr.Close()

            'Add datatable to datagridview
            Dim myData4 As DataTable = myTable
            ds = New DataSet()
            ds.Tables.Add(myData4)
            dgvGroups.DataSource = myData4
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
        sConn.Close()
    End Sub

    Private Sub dgvGroups_CellMouseDoubleClick(sender As Object, e As DataGridViewCellMouseEventArgs) _
        Handles dgvGroups.CellMouseDoubleClick
        Dim strValue As String = String.Empty
        If dgvGroups.SelectedRows.Count > 0 Then
            strValue = dgvGroups.SelectedRows(0).Cells(0).Value.ToString
        End If
        textBoxToBeUpdated.Text = Trim(strValue)
    End Sub
End Class


Cheers
Sinan
 
Just a thought, but in the main form, you could replace all those 8 identical handlers with a single one:

    Private Sub txtBtnGroup_ButtonCustomClick(sender As Control, e As EventArgs) _
    Handles txtBtnGroup1.ButtonCustomClick, txtBtnGroup2.ButtonCustomClick, txtBtnGroup3.ButtonCustomClick, txtBtnGroup4.ButtonCustomClick, _
            txtBtnGroup5.ButtonCustomClick, txtBtnGroup6.ButtonCustomClick, txtBtnGroup7.ButtonCustomClick, txtBtnGroup8.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp" & sender.Name.Last
        Dim myDGVForm As New Groups(sender)
        myDGVForm.ShowDialog()
    End Sub
 
Just a thought, but in the main form, you could replace all those 8 identical handlers with a single one:

    Private Sub txtBtnGroup_ButtonCustomClick(sender As Control, e As EventArgs) _
    Handles txtBtnGroup1.ButtonCustomClick, txtBtnGroup2.ButtonCustomClick, txtBtnGroup3.ButtonCustomClick, txtBtnGroup4.ButtonCustomClick, _
            txtBtnGroup5.ButtonCustomClick, txtBtnGroup6.ButtonCustomClick, txtBtnGroup7.ButtonCustomClick, txtBtnGroup8.ButtonCustomClick
        Groups.strLinkOfGroupSelection = "tbStkGrp" & sender.Name.Last
        Dim myDGVForm As New Groups(sender)
        myDGVForm.ShowDialog()
    End Sub

Thanks Herman, it looks pretty nice and easy to read and low space places in code page.
 
Hi,

You have been a bit silly here. You have followed everything I have said correctly but you have forgot to accommodate your existing code. Have a look at this:-

VB.NET:
Private Sub txtBtnGroup8_ButtonCustomClick(sender As Object, e As EventArgs) Handles txtBtnGroup8.ButtonCustomClick
  Groups.strLinkOfGroupSelection = "tbStkGrp8"
  Dim myDGVForm As New Groups(txtBtnGroup8)
  myDGVForm.ShowDialog()
End Sub

You are assigning the value " tbStkGrp8" to the Default instance of the Groups Form but you are NOT using the Default instance anymore since we have declared a New instance of the Form to pass an argument to the Constructor of the Form. So this should be:-

VB.NET:
Private Sub txtBtnGroup8_ButtonCustomClick(sender As Object, e As EventArgs) Handles txtBtnGroup8.ButtonCustomClick
  Dim myDGVForm As New Groups(txtBtnGroup8)
  myDGVForm.strLinkOfGroupSelection = "tbStkGrp8"
  myDGVForm.ShowDialog()
End Sub

But now that you know how a Constructor works you could change that to pass that value as another variable in an expanded Constructor.

Hope that gets things fixed.

Cheers,

Ian

NB. Just a quick note to Herman, the Sender object here is a Button and Not a TextBox, so you may want to rethink a bit.
 
Hi Ian,
I am really appreciate that you given me full support about my mistakes. It gained me experiences that I would not do that anymore.
Thank you so much
 
Hi Ian,
One more thing that is we can add another argument as second to the constructor which pass database table name as string.
VB.NET:
Public Sub New(ByVal selectedTextBox As TextBox, ByVal Link As String)
        ' This call is required by the designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        textBoxToBeUpdated = selectedTextBox
        strLinkOfGroupSelection = Link
    End Sub
That is also nice to pass that string to second form with that instance of Groups

Thanks
Cheers
 
Hi,

Good to hear that it's all working and that's absolutely fine. This is exactly what I was suggesting in my penultimate sentence in my last post. i.e:-

But now that you know how a Constructor works you could change that to pass that value as another variable in an expanded Constructor.

Happy programming.

Cheers,

Ian
 
Hi Herman,

You have misunderstood what was going on here.

I was teaching Socarsky how to pass a reference to various TextBox's to his second Form from his various Buttons and then YOU said:-

VB.NET:
Private Sub txtBtnGroup_ButtonCustomClick(sender As Control, e As EventArgs) Handles txtBtnGroup1.ButtonCustomClick, txtBtnGroup2.ButtonCustomClick, txtBtnGroup3.ButtonCustomClick, txtBtnGroup4.ButtonCustomClick, _
      txtBtnGroup5.ButtonCustomClick, txtBtnGroup6.ButtonCustomClick, txtBtnGroup7.ButtonCustomClick, txtBtnGroup8.ButtonCustomClick
  Groups.strLinkOfGroupSelection = "tbStkGrp" & sender.Name.Last
  Dim myDGVForm As New Groups(sender)
  myDGVForm.ShowDialog()
End Sub

What you have said here is:-

1) Handle all Button Clicks in a SINGLE event, which in PRINCIPAL is fine. But you then went on to say:-

2) Pass the Sender object, which is a BUTTON, to the New instance of the Form through it's Constructor which accepts a TEXTBOX type and not a Button Type, therefore causing errors and ultimate confusion.

3) You then still missed the issue that needed to be answered and solved.

Sorry to keep on at you Herman, and I know I don't get it right all the time myself, but you have to try and be accurate when giving guidance to someone trying to learn a new principal.

I will always give credit to those people who try to help others, so keep up the good work Herman.

Kindest regards,

Ian

BTW, Just thinking, the variable names txtBtnGroup1, 2, 3 etc may have contributed in causing the confusion for you??
 
Last edited:
To be honest I didn't know WTF those controls were to begin with, neither did I fully read all posts in the thread... I only saw 8 identical event handler and that made me itch lol.. I was assuming that the final revision the OP posted was working code, if it isn't then yes, I haven't solved anything.
 
Back
Top