data from datagridview not displayed into textbox (Visual Studio 2010) - vb.net

I've been dealing with this for hours!!! I have 2 forms: in one form (form1) I have my layout with textboxes, etc... in the other (datagrid_form2) I have a datagridview where I choose the item, with a doubleclickcell event, to be loaded in a specific textbox of the first form (form1).
I have a button next to the textbox of the form1 that call the datagrid_form2, once the element in the datagrid_form2 is chosen the textbox of the form1 is loaded with that value.
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text )
Me.Close()
End Sub
As you can see I have the cellDoubleclick event that should load the value of the selected cell into the textbox of my form1, but it doesn't display anything in the textbox(txtmybox). in the debug the value is chosen correctly, so is not a problem of code, simply the value is not being passed at the textbox.
Any ideas? hints?
thanks in advance
p.s. I'm working with visual studio 2010 .vb project!

It seems that you are messing with the forms.
You are creating a new instance of Form1 but you don't show it.
I suggest to read this.
Also your question is similar to this
Edit:
It is not clear from your questions how you want to achieve what you ask.
You have a form with a dataGridView (I name it frmDgv) and a second form (form1) that you want to show the cell content from yours datagrid.
Is this form (form1) already opened? or you want to open a new one each time that you double click? And if you want to open it each time you want multiple instances or one in modal ?
So I'll try to include everything:
->The form will open each time
frmDgv
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
'If you want to open a Form1 each time you double click in an cell
f1.Show
'If you want a modal style info
'f1.ShowDialog
'f1.Dispose
End Sub
->The form is already open (I wouldn't follow this)
frmDgv
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
'When you open this form for first time you will set:
Dim f1 as new Form1
mLinkForm1=f1
f1.Show
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim f1 as new form1
f1=DirectCast(mLinkForm1,Form1)
f1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
End Sub
Edit 3 (I don't have Visual Studio right now , so my code is not tested)
Form: datagridview
Private mLinkForm1 As Form1
Public Property LinkForm1
Get
Return mLinkForm1
End Get
Set(value)
mLinkForm1 = value
End Set
End Property
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
LinkForm1.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Me.Close()
End Sub
Form: Form1
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
datagridview.LinkForm1=Me
datagridview.Show
End Sub
Try this and inform me.

Change this part
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
to
form1panel.txtmybox.Text = data.CurrentCell.Value
This is for string value ...

You need to show your child form:
Public Sub data_CellDoubleClick(ByVal sender As Object, ByVal e As
System.Windows.Forms.DataGridViewCellEventArgs) Handles data.CellDoubleClick
Dim form1panel As New form1
form1panel.txtmybox.Text = mydata.SelectedCells.Item(0).Value.ToString
Debug.WriteLine(form1panel.txtmybox.Text)
form1panel.Show();
//Me.Close()
End Sub

Related

How to access DataGridView properties on an already designed Windows Form

I had already designed a windows form (Form1) with a DataGridView on it, I also have a ribbon with a button on it in which I want when I click on it, it run a code like bellow:
Private Sub Button2_Click(sender As Object, e As RibbonControlEventArgs) Handles
Button2.Click
Dim f As Form1
f = New Form1
f.Show()
'(Room for my question)
end sub
I want to be able to access my datagridview user control to add columns and rows programmatically.
How should I do that?
You can create a Public Sub on form one which where you will put the changes that you want make on the the Datagridview then call it on the Button2_Click event of your Form2.
Example:
Form 1:
Public Sub AddToDGV()
With DataGridView1
.Rows.Add(Form2.TextBox1.Text, Form2.TextBox2.Text, Form2.TextBox3.Text)
End With
End Sub
Form2:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call Form1.AddToDGV()
End Sub

how to keep info on my main form when closing the secondary form

I am trying to have the user select a code from a combo box that I need it to pass to my frmmenu.
Option Explicit On Imports System.Data.Odbc.OdbcConnection Imports
System.Data.OleDb Imports Microsoft.VisualBasic
Public Class FrmMenu
Friend PinLC As String,
SwedgeLC As String,
MonoLC As String,
StrandLC As String,
WireLC As String,
TipLC As String,
LenLC As String,
TailLC As String,
PackLC As String,
LabelLC As String
then I have radio buttons that open different forms which each save info to each one of those strings above. We click on the form that will give me the SwedgeLC
Public Class FrmExtras
Dim MREDLC As String,
WREDLC As String,
Swage As String
Private Sub CboOptions2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboOptions2.SelectedIndexChanged
Swage = UCase(CboOptions2.Text)
If Swage.Substring(0, 5) = "SWAGE" Then
FrmMenu.SwedgeLC = "A"
End If
frmmenu.show()
me.close()
but on my frmmenu It doesnt keep the code "A" in this case, I never close the frmmenu I only just hide it. But when I go to show my codes on form1(frmmenu) its null. This I think happens becuase of when I go to open form 3, Form 2 will also open and that resets my code back to null. if I open form 4, form 3 and 2 open and resets both those codes to null. If I close the main form1, then the info isn't saved at all it only remembers the last form I picked the code for not all the others.
`Private Sub CboOptions2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboOptions2.SelectedIndexChanged
Swage = UCase(CboOptions2.Text)
If Swage.Substring(0, 5) = "SWAGE" Then
FrmMenu.SwedgeLC = "A"
End If
frmmenu.show()`
without the me.Close just try to comment that out.
Then in form2 just add Me.Close in the close button
Let's assume you are using default instances. In the frmMenu (I am calling it Form1) You should have a Friend field called SwedgeLC. It is Friend so it can be visible to all the classes in your project.
Public Class Form1
Friend SwedgeLC As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Close()
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MessageBox.Show(SwedgeLC)
End Sub
End Class
Private Class Form2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Form1.SwedgeLC = "A"
Form1.Show()
Close()
End Sub
End Class
Thank you everyone for your help. I figured out the problem, why other forms would open it was set to checkchanged and not (Private Sub OptExtra_Click(sender As Object, e As EventArgs) Handles OptExtra.Click) for the private sub. Now my data is coming over.

Changing Controls of a form inside a panel

I am facing a weird problem
I have 3 forms: MainForm, Form1, Form2
MainForm has 1 Panel: Panel1
Form1 has 1 Label: NameLbl and Button: ChangeBtn
Form2 has 1 textbox: NameTxt and Button: SaveBtn
I used the following code to open form1 inside Panel1 in mainform
Panel1.Controls.Clear()
Dim FormInPanel As New Form1()
FormInPanel.TopLevel = False
Panel1.Controls.Add(FormInPanel)
FormInPanel.Show()
On ChangeBtn.Click Form2 opens as showdialog
I want NameLbl.text to change to NameLbl.text when SaveBtn is clicked But normal code doesnt work.
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
Form1.NameLbl.text=NameTxt.text
End Sub
What Should I do? Any suggestions? Given that i need to open the forms in panels for certain reasons.
Please keep in mind that this is just an example. I have multiple controls in Form1 which i want to change on form2.SaveBtn.click
I have also tried this but it does nothing
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If c.Name="NameLbl" Then
c.Text = NameTxt.Text
End If
Next
End Sub
Please Somebody tell me how it do it!
Form2 doesn't seem to have any connection back to Form1 or MainForm. You'd need to raise an event from Form2 which MainForm handles or another class handles and can pass to MainForm
Edit:
Sorry, I've just seen how you're calling Form2. There are lots of ways to get a value back from Form2 after calling ShowDialog(). One is to create a property called Result and check Result if ShowDialog() == DialogResult.OK. Something like the following.
Public Class Form2
Inherits System.Windows.Forms.Form
Public Property Result() As String
Get
Return m_Result
End Get
Set
m_Result = Value
End Set
End Property
Private m_Result As String
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
Public ChangeBtn As Button
Public NameLbl As Label
Public Sub New()
Me.ChangeBtn = New Button()
AddHandler Me.ChangeBtn.Click, AddressOf ChangeBtn_Click
Me.NameLbl = New Label()
End Sub
Private Sub ChangeBtn_Click(sender As Object, e As EventArgs)
Dim form As New Form2()
Dim dr = New form.ShowDialog()
If dr = DialogResult.OK Then
Me.NameLbl.Text = form.Result
End If
End Sub
End Class
I'd like to add that if you plan on growing this application much larger you'll run into issues with maintenance. Look into some patterns for dealing with UI logic like MVC, MVP, MVVM if you're interested.
you can try this code:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
panel1.Controls(0).NameLbl.text=NameTxt.text '"0" is the index of forminpanel in panel1,maybe it need to change.
End Sub
Form1 is contained in Panel1, so you can not access it via
Form1.
Only MainForm is visible from Form2:
Private Sub SaveBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveBtn.Click
For Each c As Control In MainForm.Panel1.Controls(0).Controls
If TypeOf c Is TextBox Then
c.Text = NameTxt.Text
End If
Next
End Sub
Try this:
For Each form1 As Form1 In MainForm.OwnedForms.OfType(Of Form1)
Form1.NameLbl.text = NameTxt.text
Next
I've faced same issue and I've fixed with this
Dim f As FormInPanel
f = Form.Panel1.Controls(0)
f.transection = True
f.NameLbl.text=NameTxt.text

Get combobox selected from another form dynamically

I have a combobox in a form1 and a datagridview in another form2.
I want to get the combobox selected with a value from the datagridview in the second form
I use the code below in form2 and it works:
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
form1.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Me.Close()
End Sub
But what I want to do is that the name of the form is passed dynamically to avoid using and IFELSE clause to enumerate all the forms I have in my project that use form2
Private Sub DataGridView1_CellDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
If formbon2.Name = "FRM_BN_RECEPTION_CUIR" Then
FRM_BN_RECEPTION_CUIR.CBO_fournisseur.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
ElseIf formbon2.Name = "frm_reception_acc_provisoire" Then
frm_reception_acc_provisoire.CBO_1.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
End If
Me.Close()
End Sub
I think I've got what you want to do. I strongly suggest you stop using the Form as a Shared resource.
Use a constructor like this in your Form2:
Private ParentFormCombo as Combobox
Public Sub New(ByVal pCmb as Combobox)
ParentFormCombo = pCmb
End Sub
Then in your doubleclick you just change the text of ParentFormCombo
ParentFormCombo.Text = DataGridView1.Rows(e.RowIndex).Cells(0).Value.ToString
Then you have to stop using:
FrmList_View.Show()
Now you should always use the constructor instead (New()). So do the following instead:
Dim f As New FrmList_View(CBO_fournisseur)
'or
Dim f As New FrmList_View(CBO_1)
f.Show()

How to pass value of a textbox from one form to another form

If I have a value stored into a textbox of form1 and I have to pass that value into an another textbox of another form2. What is the method to do this passing values from one form to another?
There are a no. of ways.
1.Using TextChanged event.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Form2.TextBox1.Text = TextBox1.Text
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Form2.Show()
End Sub
Using Click event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.TextBox1.Text = TextBox1.Text
End Sub
Using LostFocus Event:
Private Sub TextBox1_LostFocus(sender As Object, e As EventArgs) Handles TextBox1.LostFocus
Form2.TextBox1.Text = TextBox1.Text
End Sub
Similarly you can work with every events.
In order to retrieve a control's value (e.g. TextBox.Text) from another form, the best way is to create a module and create a property for the private variable.
An example of a property to hold a customer's first name:
Module modPrivateVariables
Private strCustomerFirstNameSTR As String
Public Property getCustomerFirstNameSTR() As String
Get
Return strCustomerFirstNameSTR
End Get
Set(ByVal strCustomerFirstName As String)
strCustomerFirstNameSTR = strCustomerFirstName
End Set
End Property
End Module
Then in the textbox's TextChanged event use the property getCustomerFirstNameSTR to hold the textbox's text. For example, if you had a textbox named (txtCustomerFirstName), under its TextChanged event you would enter getCustomerFirstNameSTR = txtCustomerFirstName.Text.
The textbox's text will now be assigned to getCustomerFirstNameSTR property. Now you'll be able to access this property's value from anywhere and from any form in your application. For example if you had a textbox in another form say Form2 called "txtBoxInForm2" you can call txtBoxInForm2.Text = getCustomerFirstNameSTR.
If you wanted to clear the value of the property then just type getCustomerFirstNameSTR = String.Empty. The main thing to understand is that when you create a variable in one form (class) and try to access its value from another form (another class) then the variable has to be re-instantiated once.
This happens then the variable is reset to its default value which is an empty string. This will cause you to keep getting nothing (an empty textbox) every time you call it from another form. Properties don't need to be re-instantiated because they are accessed through public methods within the property itself (the get and set methods).
if both the forms are running, then you can use
form2.TextBox1.Text=form1.TextBox1.Text
Else you can declare a Public String variable in Form2, on any event,
dim Obj as new Form2
Obj.StrVariable=Me.TextBox1.Text
Obj.Show
and on Form2 Load,
Me.TextBox1.Text=StrVariable
In Form1.vb make sure you use an event such as Button.Click and inside that
Dim obb As New Form2
obb.val = Me.TextBox1.Text()
obb.Show()
Me.Hide()
In Form2.vb use a property called "val"
Public Property val As String
And on an event like MyBase.Load
TextBox1.Text = val
You could use the button1_click and state there:
Dim obj as new form2
Obj.pass=me.textbox1.text
Obj.show()
Then in your form2 before your form2 main class you state:
Public property pass as string
On the load state
Textbox1.text=pass
Now, when you click on the button on form1, form2 will show and the textbox1 on form2 will have the same text as the one in form1. Provided you use this only with text box, labels or other kind of STRING or will work.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' timer1 must be set to true
Timer1.Start() Form1.Show() Me.Hide()
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Form1.TextBox13.Text = TextBox1.Text