Values are not populating in the Form - vb.net

Using VB.Net (Windows Application)
In the form (called as FirstForm), i am using textbox, add-form Button, search button.
When i click the add-form button, it will give the new form (same as FirstForm)
Code for adding new form
Private Sub btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnadd.Click
Dim SecondForm As New FirstForm
SecondForm.Show()
End Sub
Search Button Code
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
If FirstForm.Focus = True Then
FirstForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
Else
Dim SecondForm As New FirstForm
SecondForm.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End If
End Sub
The above code is working, but If i am in second Form when i click the search button and selected the value, then the value is appearing in the FirstForm textbox, it is not appearing in the SecondForm textbox.
If SecondForm is showing, the selected Value should appear in the SecondForm textbox not in the FirstForm Textbox.
How to solve this issue.
Need Vb.net code Help

Use Me - Reference variable which hold ref. of current form.
Dim frm As FirstForm
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If IsNothing(frm) OrElse frm.IsDisposed Then
frm = New FirstForm
End If
frm.Show()
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
Me.textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub

Using "me" will not solve the problem?? why are you referring to the form in a static way?
Private Sub Search_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Search.Click
textbox1.Text = gridview1.Rows(crRow).Cells("code").Value.ToString().Trim()
End Sub

Related

How do you open an already disposed form in VB?

When I close Form2 it won’t open up again after.
I am using a button click to open up another form. Right now I just have:
Dim Form2 As New Form2
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
Form2.Show()
End Sub
Now whenever I close the form I cannot open it again.
Like this
Private MyForm2 As Form2
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
If MyForm2 Is Nothing OrElse MyForm2.IsDisposed Then
MyForm2 = New Form2
End If
MyForm2.Show()
End Sub

Open different forms on different button click in VB.NET

A noob query I have, is there any way to use a single command to open different forms on different button click events. I have 24 buttons in one form and will use these buttons to open 24 different forms.
So instead of doing it for 24 times as:
Private Sub BtnCh1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh1.Click
FormCh1.Show()
End Sub
Private Sub BtnCh2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh2.Click
FormCh2.Show()
End Sub
Private Sub BtnCh3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh3.Click
FormCh3.Show()
End Sub
Private Sub BtnCh4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCh4.Click
FormCh4.Show()
End Sub
Can it be done with a single command?
In your form's load event add the forms in a List(Of Form)
Private list As List(Of Form)
Private Sub Me_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
list = New List(Of Form)
list.Add(New Form1())
'
'
'
list.Add(New Form24())
End Sub
Set your button's Tag property with the form's index and set them all to use the same click event:
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
list(CType(sender, Button).Tag).Show()
End Sub
Attach all the handlers to your method and then branch behaviour based on the Select Case:
Private Sub Button_Click_Handler(sender As Object, e As EventArgs) Handles Button66.Click, Button67.Click, Button68.Click
Dim btn As Button = DirectCast(sender, Button)
Select Case btn.Name
Case Button66.Name
Dim f1 As New Form1
f1.Show()
Case Button67.Name
Dim f2 As New Form2
f2.Show()
Case Button68.Name
Dim f3 As New Form3
f3.Show()
End Select
End Sub

Changing the background color for Form2

i have two RadioButtons one for Light Blue and the other for Ghost White and a button to show the next Form (Form2)
i want to be able to check on a Radio Button and the backcolor of form2 changes to the checked Radio Button
this is what i have on my coding so far
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
Private Sub rbLightBlue_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbLightBlue.CheckedChanged
If rbLightBlue.Checked Then
SecondForm.BackColor = (Color.LightBlue)
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rbGhostWhite.CheckedChanged
If rbGhostWhite.Checked Then
SecondForm.BackColor = (Color.GhostWhite)
End If
End Sub
The problem am having is making the background color change on Form2.
Any answer for this question will be very helpful.
I am not sure what you are doing, it probably has to do with how you are creating your SecondForm, this code does work, see if it helps you narrow it down.
Public Class Form1
Dim SecondForm As Form2 = New Form2
Private Sub rbLightBlue_CheckedChanged(sender As Object, e As EventArgs) Handles rbLightBlue.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.LightBlue
End If
End Sub
Private Sub rbGhostWhite_CheckedChanged(sender As Object, e As EventArgs) Handles rbGhostWhite.CheckedChanged
If DirectCast(sender, RadioButton).Checked Then
SecondForm.BackColor = Color.GhostWhite
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SecondForm.Show()
End Sub
End Class

vb2010 pass data from one form to another

I am trying to pass a value of a textbox on frmMain to a textbox on frmDepartment. I have tried the following code which I thought would work but it dosen't. I ma a new user to VB and come from a php background where it would have been a simple task of setting a seesion. Can anyone help with this? If you need to see more code, please ask. many thanks
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
In frmMain, I am getting value like this:
Dim value As Object = UserDataGridView.Rows(e.RowIndex).Cells(0).Value
txtCustomerActive.Text = CType(value, String)
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.Show()
End Sub
Which is showing in frmMain ok.
In frmDepartment I have this code
Private Sub txtDeptCustomer_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDeptCustomer.TextChanged
'Dim customer As String
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
instead of putting the code within the txtDeptCustomer.TextChanged sub, try putting it within the frmDepartment_load:
Private Sub frmDepartment_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
txtDeptCustomer.Text = frmMain.txtCustomerActive.Text
End Sub
or you could set the frmDepartment text box text on the frmMain button click:
Private Sub btnDepts_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDepts.Click
frmDepartment.txtDeptCustomer.Text = txtCustomerActive.Text
frmDepartment.Show()
End Sub

Datagridview - Focus on Cell that was right clicked

I have a datagridview that I have put a ContextMenuStrip1 on. I would like it to remove a row in the datagridview when the row is right clicked on and they click on "delete row". I have the delete working and the menu is showing up but this isn't firing when you right click on the datagridview.
This is where I am setting the row to edit:
Private Sub ModifyRowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ModifyRowToolStripMenuItem.Click
If Not datagridview_TagAssignment.CurrentRow Is Nothing Then
datagridview_TagAssignment.CurrentCell = datagridview_TagAssignment.Item(0, datagridview_TagAssignment.CurrentRow.Index)
datagridview_TagAssignment.BeginEdit(True)
End If
End Sub
I am always ending up on row(0) and never the row I right clicked on.
Private Sub datagridview_TagAssignment_CellMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles datagridview_TagAssignment.CellMouseClick
If e.Button = Windows.Forms.MouseButtons.Right AndAlso e.RowIndex >= 0 Then
datagridview_TagAssignment.Rows(e.RowIndex).Selected = True
End If
End Sub
Anyone have any suggestions?
Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
ContextMenuStrip1.Items.Add(rowClicked.ToString)
ContextMenuStrip1.Show(DataGridView1, e.Location)
ContextMenuStrip1.Items.Clear()
End If
End Sub
Edit: Updated to handle a context menu strip.
That should give you the row index of the row that was right clicked using the mouse coordinates. Which should let you delete the row based on knowing the index.
Edit
Per Your comment on it not working this is my code
I have a Solution with a WinForm with a dataGridView added to it. and this is the code in the form.
Public Class Form1
Dim bindS As New BindingSource
Dim rowClicked As Integer
Private Sub DataGridView1_MouseClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseClick
If e.Button = Windows.Forms.MouseButtons.Right Then
rowClicked = DataGridView1.HitTest(e.Location.X, e.Location.Y).RowIndex
ContextMenuStrip1.Items.Add(rowClicked.ToString)
ContextMenuStrip1.Show(DataGridView1, e.Location)
ContextMenuStrip1.Items.Clear()
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim s As New List(Of String)
s.Add("String one")
s.Add("String Two")
bindS.DataSource = s
DataGridView1.DataSource = bindS
End Sub
End Class
Right Clicking on a row shows the correct row index
Make sure that the event args that you are handling are the System.Windows.Forms.MouseEventArgs I noticed that you're handling the cell click