How to access data from calling object in vb.net - vb.net

I have a Window-Form 'caller' in vb.net containing a datagridview with a small overview table of certain objects, each with its own ID in the first column. Now, if a row is double clicked, i want to show a dialog 'edit', where one can edit many details of that row which i do not want in the overview table.
My approach is as follows: In the caller form i wrote this to call 'edit':
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim f_edit As New edit
f_edit.ShowDialog(Me)
End Sub
That works fine.
However, in the called Form "edit" i need to check, which ID was selected and load this data from the database to edit it. I can access some data from the calling form 'caller' using e.g.
MsgBox(CType(Me.Owner, caller).Text)
to show the window title of 'caller'. However, i want to extract the currently selected ID in the datagridview or at least some variabhle containing it. In the caller form, this could be easily done by evaluating
dg_data.Item(0, selectedRow).Value.ToString
but i cannot access any relevant information in 'caller'. I have a public class with some global variables there but i cannot access them as well.
Probably my strategy to solve this problem is not the most clever approach? Basically, i want to open a very detailed edit window when someone clicks on a line in an overviewtable but simultaniously blocking the rest of the application as long as the edit window is open.
Thanks!

The idea is to pass the data to the second form. When you create an instance of the second form (my class is called Form2, yours is called edit) with the New keyword the Sub New is called on Form2.
Private Sub OpenEditDialog()
Dim f_edit As New Form2(32) '32 is the number you retrieve from your DataGridView
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
You pass the ID to Form2 and set a variable at Form level. You can then use the variable anywhere in Form2.
Public Class Form2
Private ID As Long
Public Sub New(SelectedID As Long)
InitializeComponent()
ID = SelectedID
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
MessageBox.Show(ID.ToString)
End Sub
End Class
You need to call InitializeComponent() so the controls will show up.

How do you usually get data into objects? You set a property or pass an argument to a method or constructor? Why should this be any different? Decide which you want to use and then write that code in your form. If it's required data, I would suggest a constructor. Just write this code in your form:
Public Sub New
and hit Enter. That will generate a little extra code automatically. You can then add a field to store the value, a parameter to the constructor and then assign the parameter to the field inside.

Thank you for pointing me to the correct route.
I solved it like this (which works fine and which is hopefully acceptable):
In the calling form:
Private Sub dgdata_dbclick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dg_data.CellMouseDoubleClick
Dim selectedRow As Integer = dg_data.CurrentCell.RowIndex
Dim f_edit As New edit
f_edit.edit(dg_data.Item(0, selectedRow).Value.ToString)
f_edit.ShowDialog(Me)
f_edit.Dispose()
End Sub
In the called form:
Public Sub edit(ByVal id As Long) 'Handles MyBase.Load
'Enter commands to prepare your form
End Sub

Related

How can i call my function from my main form in another form without this error in vb.net

I am currently developping an app WinForm using vb.net and i have to call a function, situated in my main form, in another form. Here is my function in my main form
Public Sub ImprimCalibrage(t1 As String, t2 As String)
Dim strCalibr As String = Build_str("CAL", "CALIBRAGE", t1, t2)
BuildFile("fileT.txt", strCalibr)
Print()
End Sub
To call it, i have created an attribute in my secondary form like this
ReadOnly form1 = Application.OpenForms("Main")
And now i call my function doing this form1.ImprimCalibrage(TextBox1.Text, TextBox2.Text)
But when i click on the button which supposed excute the function, i got this error
'Object variable or With block variable not set.'
I don't have any idea how to solve it because i dont have with block and my only variable in just a String.
Application.OpenForms("Main") is returning Nothing because at the time it is called (when Form2 is created) , there is no open form named Main in the collection. This Nothing then persists until you try and use it. You can't call a method on Nothing. Even if you later ensure there is a form named Main in the OpenForms collection, it doesn't alter the fact that at the time you looked (and captured) there was nothing
Move the code to just before you need it
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim form1 As Form1 = Application.OpenForms("Main")
form1.ImprimCalibrage(TextBox1.Text, TextBox2.Text)
End Sub

vb.net a forms "new" routine is being called before I even invoke the form

I have a form called "partmanager". On it is a button to show another form "parteditor" to allow editing details of a part. Clicking that button will show the form and pass in a variable to the parteditors "new" routine.
My problem is that when the calling form (partmanager) starts, it immediately calls new routine in the parteditor form before it (partmanager) is even initialized so the parteditor form does not get the string that is supposed to be passed in. Later, when the calling form is visible and I click the button to show the parteditor form, new has already been prematurely called and so is not called again and the form does not get the string passed in.
I hope this makes sense!
I can implement a property in the parteditor form and pass in my variable that way prior to showing the form and that will work, thereby not even requiring a "new" routine in the parteditor forms code.
So my question is, is implementing the property the proper way to pass this variable to the form being called, or am I not properly coding my forms? (I also have an intermediary module called "commands" where I have been defining command procedures, in this case just showing a form.)
any pointers would be appreciated, thanks!
here is the code for the button in the calling form:
Private Sub EditButton_Click(sender As Object, e As EventArgs) Handles EditButton.Click
Commands.EditPart(_PartNumber) 'call the editpart command
Me.Close()
Me.Dispose()
End Sub
here is the code for the form being called:
Public Class PartEditForm
Private _partNumber As String = String.Empty
Public Sub New(partNumber As String)
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
_partNumber = partNumber
End Sub
Private Sub PartEditForm_Load(sender As Object, e As EventArgs) Handles Me.Load
Label1.Text = _partNumber
End Sub
End Class
and here is the code in my "commands" module for loading/showing the form:
Public PartEditForm As New PartEditForm(_partNumber)
Public Sub EditPart(partnumber As String)
If PartEditForm.IsDisposed Then
PartEditForm = New PartEditForm(partnumber)
End If
PartEditForm.Show()
End Sub
you can save the routine in a dim variable and get this on the other form and close the first form or hide but you need get in new var and contine where stop before.

VB.Net passing datagridview row from one form to another form that is already open

I'm trying to develop a search window that allows a user to fill in criteria, then click the search button. When the search button is clicked the results are returned in a DataGridView. When a result in the DataGridView is clicked, I'd like to pass the row number to another form that is already open.
I understand I can pass the result by creating a new instance of the form. I pass variables to my Search Window using the following method:
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
Dim ST = New Search_Tool(DGV_Ongoing.DataSource, AgencyOptions, EthnicityOptions, EmploymentTypeOptions, EmploymentStatusOptions,
CategoryOptions, OutcomeOptions)
ST.Show()
End Sub
however I cannot call the form I want to pass the row value to because it is already open (its the main form). Is there a way to handle this? I'm struggling to find any information that doesn't call the form to open it whilst passing the data.
I can't reference Main_form.Main_DGV.CurrentCell as Visual Studio says it requires an Object Reference in order to do so.
Thanks in advance!
I'd recommend an event in your Search_Tool form that is raised when the user has found what they're looking for.
The main form is subscribed to that and can then access its datagrid.
Class Search_Tool
Inherits Form
Public Event SearchResultFound(resultId As Integer)
Private Sub OnUserClickedARowOrPressedAButtonOrWhatever()
RaiseEvent SearchResultFound(currentRow.Id)
End Sub
End Class
And in the main form.
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
Dim ST = New Search_Tool(DGV_Ongoing.DataSource, AgencyOptions, EthnicityOptions, EmploymentTypeOptions, EmploymentStatusOptions,
CategoryOptions, OutcomeOptions)
AddHandler ST.SearchResultFound, Sub(result As Integer)
'handle your result id here.
MessageBox.Show("User selected row " & result)
End Sub
ST.Show()
End Sub
The advantage of this is that the SearchForm doesn't need to know what it's being used by. It's just there to give results which will make it easier to reuse later.

Display a modeless Form but only one

VB2010. I must be missing something because I couldn't find a solution after searching for an hour. What I want to do is simple. In my app I want to display a modeless form so that it is floating while the user can still interact with the main form.
dim f as New frmColors
f.Show(Me)
But I only want one instance of the form at any time. So how can I prevent more than once instance being displayed, and if there is one instance then just give it focus?
Does something like this work for you, if the form is already visible you can not do a Show, you can just do a BringToFront, also you can check to see if the Form has been disposed so you can New up another one.
Public Class Form1
Dim f As New frmColors
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
If f.IsDisposed Then f = New frmColors 'To handle user closing form
CheckForm(f)
End Sub
Private Sub CheckForm(frm As Form)
If frm.Visible Then
frm.BringToFront()
Else
frm.Show(Me)
End If
End Sub
End Class
Make your form follow the singleton pattern. I can't vouch for this sample, but from the text it appears to do what you want.

How to make an single instance form

I have a mdicontainer form that summons forms. My problem is when the a user clicks again the menu for that form, it also make another instance of it.
What I did is declare a public class with a public variable on it ex: Boolean isFormOneOpen = false. Then every time formOne opens, it checks first the global variable I declared a while ago if it's false, if it is, instantiate an object of a formOne and then show it. Otherwise, do nothing. Very static, imagine if I have many forms, I have to declare a variable for each form to check if it's already open. Can you provide me a solution for this? Maybe a method that accepts a Form? Or any more clever way to do this.
You don't need a variable, you could iterate the MdiChildren collection to see if the form is already opened. For example:
Private Sub btnViewChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewChild.Click
For Each child In Me.MdiChildren
If TypeOf child Is Form2 Then
child.WindowState = FormWindowState.Normal
child.Focus()
Exit sub
End If
Next
Dim frm As New Form2
frm.MdiParent = Me
frm.Show()
End Sub
The VB.NET-centric solution:
Private Sub btnViewChild_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewChild.Click
Form2.MdiParent = Me
Form2.WindowState = FormWindowState.Normal
Form2.Show
End Sub
Rather than a boolean, declare a variable of the form's type. Then just make sure the variable isn't Nothing and call it's .Open() method. This has the nice side effect of also bringing your existing form instance to the front if it's already open.
Even better, in VB.Net 2.0 and later all forms have a default instance with the same name as their type, so you can just say FormName.Open() and be done with it. However, I haven't tried this in an mdi situation before.