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 think I understand what you want to do. To clarify, you have 8 Buttons and 8 TextBoxes where each TextBox is somehow related to each Button and when each Button is pressed the related TextBox for that Button needs to be updated by the DataGridView?

If so, then:-

If the DataGridView is on the same Form, then create a variable of Type Button at the Class level and set that variable to the correct TextBox to be updated when each Button is pressed. You would then update that variable which points to the correct TextBox to be updated.

If the DataGridView is on another Form then create a Constructor in the other Form which accepts a TextBox as an argument and pass the correct TextBox to be updated to the Form when each Button is pressed. Again, you would then update this variable which points to the correct TextBox to be updated.

Hope that helps.

Cheers,

Ian
 
Hi,

I think I understand what you want to do. To clarify, you have 8 Buttons and 8 TextBoxes where each TextBox is somehow related to each Button and when each Button is pressed the related TextBox for that Button needs to be updated by the DataGridView?
If the DataGridView is on another Form then create a Constructor in the other Form which accepts a TextBox as an argument and pass the correct TextBox to be updated to the Form when each Button is pressed. Again, you would then update this variable which points to the correct TextBox to be updated.
Hope that helps.
Cheers,
Ian
Confuse Ian, Sorry
Main. means main from and the second form call by 8 button and 8 buttons related 8 textboxes all of them in Main form and I need only to change textboxes' variable whic button pressed and then pass the right textbox variable.
 
Hi,

Then use my second explanation. i.e:-

If the DataGridView is on another Form then create a Constructor in the other Form which accepts a TextBox as an argument and pass the correct TextBox to be updated to the Form when each Button is pressed. Again, you would then update this variable which points to the correct TextBox to be updated.

Cheers,

Ian
 
Hi,
Then use my second explanation. i.e:-
Cheers,
Ian
Hi,
Here is a screen shot
41671346.png


Thanks Ian, let me think about your suggestion but I really need to see a sample of that.
 
Hi,
If the DataGridView is on another Form then create a Constructor in the other Form which accepts a TextBox as an argument and pass the correct TextBox to be updated to the Form when each Button is pressed. Again, you would then update this variable which points to the correct TextBox to be updated.
Hope that helps.
Cheers,
Ian
Hi,
I made your suggestion and now my second form got crazy with many error messages.
Here is Constructor :-
VB.NET:
Public txtbox As TextBox
    Public Sub New(ByVal ntxtbox As TextBox)
        txtbox = ntxtbox
        InitializeComponent()
    End Sub

Error messages are like that "Reference to a non-shared member requires an object reference." as 17
 
Hi Ian,
Hope this help you to figure out whats going on my side.
hatam.png
 
Last edited:
Hi,

I can see that you have watched the YouTube post that I provided but you seem to have got all confused as to how to use a Constructor.

To demonstrate what you need to do is to create a new project and add two Forms. In the SECOND Form add a TextBox and a Button and add the following code to the form:-

VB.NET:
Public Class Form2
  Private textBoxToBeUpdated As TextBox
 
  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 Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    textBoxToBeUpdated.Text = TextBox1.Text
    Me.Close()
  End Sub
End Class

As you can see this Form has a Private variable of type TextBox which is assigned to an incoming argument when you create a New instance of this Form with the New Keyword. When you then click the Button the text in the TextBox is assigned to the TextBox that was passed as the incoming argument from Form1.

So to use this then, in the FIRST Form add Two Buttons and Two TextBox's and add the following code to the Form:-

VB.NET:
Public Class Form1
 
  Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    Dim myDGVForm As New Form2(TextBox1)
    myDGVForm.ShowDialog()
  End Sub
 
  Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim myDGVForm As New Form2(TextBox2)
    myDGVForm.ShowDialog()
  End Sub
End Class

As you can see here, each time we press the Buttons a NEW instance of Form2 is created passing the Correct TextBox to the Constructor so that Form2 knows which TextBox will be updated when its Button is pressed and Form2 closed.

I hope this helps to put things into context for you.

Cheers,

Ian
 
I just got home after cycling and I'll switch on my laptop to try soon.
Thanks a lot for your great helping hand, Ian. You rock
 
I think it will work well when I try it on network segment tomorrow. I applied changing which you mentioned but I had to add a new constructor without parameter so that not to get error message because compiler given me this error message "Reference to a non-shared member requires an object reference." Because I used that Friend string variable in Main form to pass table titles through second form's select command.

VB.NET:
Friend strLinkOfGroupSelection As String
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
End Sub

Thanks again Ian,
 
Hi Ian,
Forgot to tell that error message confused me when I tried to add a constructor after watching that video. I could able to find a solution if that error has not occurred. Anyway, it's nice to see and learn a great tutorial from you as perfect.
 
Hi Ian,
So far so good but there is an issue occurred that second form loads a DataGridView and Sql command but all line of code in second form loading now has problem if I run it with
VB.NET:
Dim myDGVForm As New Groups(txtBtnGroup1)
        myDGVForm.ShowDialog()
 
Hi,

In my example I used myDGVForm.ShowDialog, but I get the feeling that your own Button code is supposed to be doing something else after the Form has loaded? If so, then ShowDialog will interrupt the execution of that code until the Form closes so the simple answer is to change ShowDialog to Show.

Hope that helps.

Cheers,

Ian
 
Hi,
In my example I used myDGVForm.ShowDialog, but I get the feeling that your own Button code is supposed to be doing something else after the Form has loaded? If so, then ShowDialog will interrupt the execution of that code until the Form closes so the simple answer is to change ShowDialog to Show.

Hope that helps.
Cheers,
Ian

I am sorry to tell like this but negative, there is no difference.. Now almost I am stuck that point
 
Back
Top