Get combobox selected from another form dynamically - vb.net

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()

Related

VB.NET how to populate a ComboBox in an UserControl from a Form

I have an UserControl where data from MySQL table loads in a ComboBox while it is loading.
Private Sub LoadFeeGroup()
Try
OpenConnection()
qry = "SELECT GroupId, GroupName FROM master_fee_group WHERE Stat='1' ORDER BY GroupName ASC"
Dim da As New MySqlDataAdapter(qry, conn)
Dim ds As New DataSet
da.Fill(ds, "master_fee_group")
With CmbGroup
.DataSource = ds.Tables("master_fee_group")
.DisplayMember = "GroupName"
.ValueMember = "GroupId"
End With
If CmbGroup.Items.Count > 0 Then
CmbGroup.SelectedIndex = 0
End If
Catch ex As Exception
MsgBox(ex.Message)
Finally
CloseConnection()
End Try
End Sub
And this Subroutine is being called when the UserControl is being loaded.
Private Sub AdmissionFeeUc_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
LoadFeeGroup()
End Sub
Now user can add any Fee Group Name (if not added previously) by opening a form.
Now I want to call this LoadFeeGroup() subroutine from that form so that user can see the added Fee Group Name in the ComboBox of the UserControl after closing the form.
Something like...
Private Sub FormFeeGroup_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
' Code for calling the UserControl subroutine..
End Sub
I have tried to call the Subroutine like below,
but failed.
How can I do that ?
UPDATE
I have added a button in the UserControl.
Private Sub BtnNewGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNewGroup.Click
Dim frmFeeGroup As New FormFeeGroup
Dim dlgres As DialogResult = frmFeeGroup.ShowDialog()
If DlgRes <> DialogResult.OK Then
Return
Else
LoadFeeGroup()
End If
End Sub
And in the FormClosing event
Private Sub FormFeeGroup_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If Me.DialogResult <> Windows.Forms.DialogResult.OK Then
Return
Else
'nothing to do
End If
End Sub
And in the Close button in the form,
Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
Me.Close()
End Sub
My purpose is Partially served. Partially because, If I open the Form from the menu, the ComboBox is not updated.
You could create your own constructor for the form where you must pass a user control instance to it. That way you cannot create an instance of the form without specifying which user control it should be "tied" to.
For instance, put this in your form's code:
Public Property TargetFeeUc As AdmissionFeeUc
Public Sub New(ByVal FeeUc As AdmissionFeeUc)
InitializeComponent() 'Must be called before anything else.
Me.TargetFeeUc = FeeUc
End Sub
Then to create a new form instance you'll always be forced to give it an AdmissionFeeUc control.
'If written inside the user control's code:
Dim frmFeeGroup As New FormFeeGroup(Me)
'If written somewhere else:
Dim frmFeeGroup As New FormFeeGroup(<user control instance here>)
'...for example:
Dim frmFeeGroup As New FormFeeGroup(AdmissionFeeUc1)
And whenever you want to update the user control from your FormFeeGroup form you just need to call:
TargetFeeUc.LoadFeeGroup()
EDIT:
To get the user control instance if it was created dynamically you have to set the control's Name property when creating it, then you can reference it via:
<target control or form>.Controls("<user control name here>")
'For example:
Me.Controls("MyFeeUc")
'Or for sub-controls:
Me.Panel1.Controls("MyFeeUc")

How to select TextBox on Label Click

In Microsoft Access, when you click on a label, the textbox associated with that label gets the focus. As far as I can tell, VB.NET does not have this same functionality. I know I can always add something in to the click event of the label, like so...
TextBox1.Focus()
But I have dozens of fields on the form, and it would make it so much easier if I did not need to add this to the click event of each label.
I guess it would be possible to make an event for all labels that forces a tab to the next control, and assuming that I have the Tab indices set up correctly, then this would work. The problem would occur when adding new fields to the form, in which case all tab indices would need re-verified.
Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click, Label2.Click
'code to tab to next field...
End Sub
Is there any easier way?
First, set the controls' TabIndex orders on your form then use this code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each c As Control In Me.Controls
If TypeOf c Is Label Then AddHandler c.Click, AddressOf Label_Click
Next
End Sub
Private Sub Label_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Me.SelectNextControl(sender, True, True, True, True)
End Sub
End Class
Now whenever you click on a label, the following control in the order will be focused.
How about creating a dictionary where the label is the key and the control to focus is the value, then add a Click event handler to all of the label in the Dictionary. Everytime you add a label/control 'all' you need to do is add another KeyValuePair to the Dictionary
Simple Example:
Public Class Form1
Protected Friend DicLabelToControl As Dictionary(Of Label, Control)
Protected Friend Sub InitLabelDic()
DicLabelToControl = New Dictionary(Of Label, Control) From {{Label1, TextBox1}, {Label2, TextBox2}}
End Sub
Protected Friend Sub AddClickEventsToLabels()
For Each lb As Label In DicLabelToControl.Keys
AddHandler lb.Click, AddressOf HandleLabelClick
Next
End Sub
Private Sub HandleLabelClick(sender As Object, e As EventArgs)
DicLabelToControl(CType(sender, Label)).Focus()
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
InitLabelDic()
AddClickEventsToLabels()
End Sub
End Class

Can I declare a dynamically adding new button globally?

I need help in my VB.NET project. I declare a dynamic textbox which is "dim textbox as new textbox()" inside the button and it will create a new textbox when I press button1 and I also put text on the new textbox created. Now when a dynamic textbox already created. When I press button2 I want to compare the text on the label to the text in dynamic textbox created but I got an error that says, OBJECT REFERENCE NOT SET AN INSTANCE OF AN OBJECT.
I suppose you create the textbox in the click event handler of the button. this way, you can only access it in this event handler method. In order to use it in the other event handler, you need to move the declaration to class level, e.g.:
Private txtBox As Textbox
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
txtBox = New Textbox()
' ...
Me.Controls.Add(txtBox)
End Sub
Private Sub btn2_Click(sender As Object, e As EventArgs) Handles btn2.Click
If txtBox IsNot Nothing Then
MsgBox(txtBox.Text)
End If
End Sub
Try it like this. You have to store the reference to your textbox in a variable, e. g. in the scope of the form instance.
Public Class Form1
Private dynTextbox As TextBox
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Me.dynTextbox = New TextBox
With Me.dynTextbox
.Top = Me.Button1.Top
.Left = Me.Button1.Right + 5
.Text = "test"
End With
Me.Controls.Add(Me.dynTextbox)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
If Me.dynTextbox IsNot Nothing Then MessageBox.Show(Me.Label1.Text = Me.dynTextbox.Text)
End Sub
End Class
If you create multiple textboxes dynamically, you may store them in some Array or List (e. g. in a List(Of TextBox)) and then you'll have to find a way to reference the one you need, that depends on your specific project.

data from datagridview not displayed into textbox (Visual Studio 2010)

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

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