How do I call a method of another from

  • Thread starter Thread starter Partha Protim Roy
  • Start date Start date
P

Partha Protim Roy

Hello,


I have a Customer form say A to enter/update customer details. In the Form
A I have a button which opens another form say B.

In the Form B, I am providing user with a option to search Customer from the
database depending on various search criteria.

On matching the criteria, list of Customer is displayed in the datagrid.
Selecting a particular row and click on OK button will do the following

1. Close the Form B

2. Load the information of the selected customer in the form A.


I am having problem in loading the information in Form A.

Can anybody help me in solving this issue.


Partha
 
Partha Protim Roy said:
I have a Customer form say A to enter/update customer details. In the Form
A I have a button which opens another form say B.

In the Form B, I am providing user with a option to search Customer from the
database depending on various search criteria.

On matching the criteria, list of Customer is displayed in the datagrid.
Selecting a particular row and click on OK button will do the following

1. Close the Form B

2. Load the information of the selected customer in the form A.


I am having problem in loading the information in Form A.


I am assuming your problem is in the transfer from B to A?

Give A a method to load and display the details based on a
Customer name " A.LoadDetails(CustomerName)

Give B a public function to return a string that will be your
users selection; B.GetCustomerName()

From A's button, create a form B and ask it for the name:

Dim FormB As New FormB
Dim name As String

name = FormB.GetCustomerName()
FormB = Nothing
If name.Length > 0 then LoadDetails(name)


In FormB's (Public) GetCustomerName function, load
the customer list, and show the dialog.

LoadCustomerList
Me.ShowDialog
Return UserSelection

UserSelection is a module level String variable. If the user
selects a name and hits OK, then set that UserSelection string
to the selected name, and close the form. If the user hits Cancel,
then just close the form.

The value of UserSelection will be assigned to the name variable
back in FormA after the form closes, and as shown, if there is
something in it, then use it, and if not, the user opted to cancel.

HTH
LFS
 
Hello Larry,

Thanks.

I tried the way you have suggested but it is not happening.

It could be the reason that my Form B is modal.

Partha
 
Overload the constructor in form B

Private ParentForm As Form

Public Sub New( ByRef Parent As Form )

ParentForm = Parent

.. . . .

Now you can update members of the Parent Form with search results etc


ParentForm.MyProperty =

And then Close Form

Me.Close

HTH



--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
I could not get you .

Please, write in details.

Thanks
Partha
 
Partha Protim Roy said:
I tried the way you have suggested but it is not happening.

It should work. Try it in a new project.

Start a new project, and add a Form2. Give both Form1 and
Form2 their own button.

In Form1 use:

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim f As New Form2
Button1.Text = "Waiting..."
Button1.Text = f.GetIt
End Sub


And in Form2 use:

Private User As String

Public Function GetIt() As String
Me.ShowDialog()
Return User
End Function

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
User = "Got It"
Me.Close()
End Sub



When you run the program, clicking on the button in Form1 should
show Form2, and depending on what you do there will determine
what text is returned. If you hit the button, "Got it" will be returned
and shown on Form1' button. If you hit the X (close the form) then
Form1's button will be blank.

That works OK here....

LFS
 
Partha,

This question from you, gives forever a lot of messages.
Very important is where you create your "other" form and how you do it.

(There are a lot of ways to do that in VBNet)

When that is known than it is a piece of cake to help you mostly.

Maybe you can show us that.

Cor

"Partha Protim Roy"
 
Partha Protim Roy said:
I could not get you .

Please, write in details.

He basically suggests to pass the new form (B), a reference to your
calling form (A) so that it can set a property of that form (A).

While it would work, it is not really an optimal solution because
the dialog (FormB) would have to know what property to call on
the calling form (FormA). If you call that same form (B) from many
other forms, they all would have to have that same property for it
to work properly.

The reverse is a bit better, give the Dialog (formB) a property that
the caller (FormA) can request after the user closes the form. But
I prefer using a public function on the form.

By using a public function, the dialog doesn't need to know anything
about who called it, only that it needs to return the name the user
selected, or an empty string if the user cancels the operation. And,
the caller makes a direct call to the form and acts on what is returned,
instead of looking at the form's properties. For a single value, it works
well. For multiple values, an object can be returned that has the multiple
values encapsulated, or, the dialog can expose properties, as was
mentioned above....

LFS
 
Cor Ligthert said:
This question from you, gives forever a lot of messages.
Very important is where you create your "other" form and how you do it.

(There are a lot of ways to do that in VBNet)

When that is known than it is a piece of cake to help you mostly.

Maybe you can show us that.


Wasn't that enoough?


LFS
 
Larry,
Wasn't that enoough?
No both can be created using an module as independent objects.
They can as well be created in one of the Forms, what makes things much
easier,
That can be as well be done with a MDIparent and MDI child, which makes it
again easier or as a dialog using a showform what makes it as well easy. (I
have seen here people here who put a button on a MDIparent by instance
before you say that cannot be done)

By instance for a showdialog the method from OHM is in my opinion the best
solutions for the question.

Maybe I forgot some however all of those make the calling of a method in
another form mostly a little bit different.

I hope this clears it?

Cor.
 
Your making the assumption that the action is being initiated by the calling
form on instantiation. This OP suggests that he wants to do a search on a
PopUp, this means the DialogResult/Or Updating the callers properties is the
best way to return a value to the caller in my opinion.

However, you are entitled to your opinion of course. There are of course
many ways to skin a rabbit as it were in .NET, thats what makes it so
versitile.





--

OHM ( Terry Burns )
. . . One-Handed-Man . . .
If U Need My Email ,Ask Me

Time flies when you don't know what you're doing
 
Cor Ligthert said:
No both can be created using an module as independent objects.
I hope this clears it?

I do not see the many different cases you see. It matters little if the
forms are SDI or MDI child forms. The OP said one form called
another. That seems pretty plain to me.

By instance for a showdialog the method from OHM is in my opinion the best
solutions for the question.

OHM suggested passing in the Parent form reference and then
calling a property on the calling form:

Private ParentForm As Form

Public Sub New( ByRef Parent As Form )
ParentForm = Parent

......

And when closing:

ParentForm.MyProperty = "George"
Me.Close


But, that forces the Parent form to expose a Public MyProperty.

If FormA calls that dialog, it has to expose FormA.MyProperty
If FormAA calls that dialog, it has to expose FormAA.MyProperty
If FormAAA calls that dialog, it has to expose FormAAA.MyProperty

Do you see what the problem is with that method?

Consider how the FileOpen dialog handles that same situation:

Dim openFile As New System.Windows.Forms.OpenFileDialog
openFile.ShowDialog()
Name = openFile.FileName

Here, the dialog exposes the property, not the calling form. That
is preferrable than having the parent expose the property.


Consider the InputBox:

name = InputBox("", "Enter Customer")

This is like the suggestion I made. The caller gets the user's value
returned from the call.

Now, here is the big question. Can you show me an example that
is included in the NET Framework, where the caller exposes a property
for the dialog to set? (Like OHM's suggestion?)

<g>
LFS
 
Thank you very much.

It is working fine.

Once again thank u Larry

Partha
 
Larry,
I do not see the many different cases you see. It matters little if the
forms are SDI or MDI child forms. The OP said one form called
another. That seems pretty plain to me.
Called can be in my opinion be call the method not instance the Form as new
form, that it is unclear for me is in my opinion enough to have doubts, not
that I am right, I have doubts where you can be right by the way.
OHM suggested passing in the Parent form reference and then
calling a property on the calling form:
You are right, I did only look at the overloaded instancing. When it is a
showdialogform, than it is very sufficient (again in my opinion)

By the way, Larry, I did not say that your solution is wrong, my reaction
was on the message from the OP and the many long threads about this subjects
that I have seen about this. Where at the end was by instance showed that
the the Op was creating everytime a new form while he did wanted to access a
method on a previous. That was the reason of my message. I did only slightly
look at your solution and thought that you took 2 ways the form could be
instanced.

When the OP tell us how he instances his forms, than all becomes in my idea
much easier.

Cor
 
One Handed Man ( OHM - Terry Burns ) said:
Your making the assumption that the action is being initiated by the calling
form on instantiation.

That is a little vague. I made no assumtion other than the OP wanted to show
a list of names to the user, and have the user select one from the list. The
problem was in learning what name the user selected.

This OP suggests that he wants to do a search on a PopUp,

I don't understand that at all. A search on a PopUp? Isn't a PopUp some
form of context menu? There was no mention of menus that I saw....

this means the DialogResult/Or Updating the callers properties is the
best way to return a value to the caller in my opinion.

However, you are entitled to your opinion of course.

Yes, I can agree there! (Whew!)

But, see my reply to Cor. What dialog in the .NET framework acts
as you suggest? If it is the best type to use for dialogs, where is
it in the NET framework class library? Did MSFT purposely use
'lower grade' dialogs??? <g>

LFS
 
Cor Ligthert said:
You are right, I did only look at the overloaded instancing. When it is a
showdialogform, than it is very sufficient (again in my opinion)

By the way, Larry, I did not say that your solution is wrong, my reaction
was on the message from the OP and the many long threads about this subjects
that I have seen about this. Where at the end was by instance showed that
the the Op was creating everytime a new form while he did wanted to access a
method on a previous.

Aha! I now see why you said it matters how the form was created. But, it
was fairly evident from the first post, that the use was to be like a standard
dialog form. The form was to show, get input, and close. I would have also
offered something different if the form was not modal, and was loaded
previously.

LFS
 
Lets look at the OP's post once again . . .
I have a Customer form say A to enter/update customer details. In the Form
A I have a button which opens another form say B.
OK, we all agree to instantiate a object of Type Form(B) !
In the Form B, I am providing user with a option to search Customer from the
database depending on various search criteria.
On matching the criteria, list of Customer is displayed in the datagrid.
Selecting a particular row and click on OK button will do the following

This implies that after the form is instantiated, the user will perform a
search and then close the form. Your code Initialises form B's User property
asynchronsously and at any time after this, the user performs the search
then closes the form. Do you see the problem with this approach.?

The emphasis should be to drive it from form B. If the user uses the
..ShowDialog Method, this enables us to Sychronously execute some statements
which would allow us to interrogate the B's properties or methods of Friend
of Above. The B's Closure can be a Hide() instead and the B.Close() method
can be called after retreiving the data. This is a better method actually in
my opinion than yours.

However, I will conceed that revealing A's Method or Properties with a view
to B setting them is perhaps not the best way to do it, however, it's still
valid as an answer.

Regards - OHM
 
One Handed Man ( OHM - Terry Burns ) said:
OK, we all agree to instantiate a object of Type Form(B) !
Yep



This implies that after the form is instantiated, the user will perform a
search and then close the form.

The user will perform a search
Select an item from the results of that search
Then close the form

Your code Initialises form B's User property
asynchronsously and at any time after this, the user performs the search
then closes the form. Do you see the problem with this approach.?

You did not follow along, or you got confused somewhere.
The (private) User is set in the click event of the OK button.
That would be after they did the search and selected a name.

The value that was set in the OK button is the value that is
returned from the function. Set up the example and try it
for yourself. As I indicated, it functions much like the
InputBox function. (For example, put a textbox on the 2nd
form, and enter text into it. Then in the button click event,
assign that text to the User variable and see for yourself that
your text does get returned.)

However, I will conceed that revealing A's Method or Properties with a view
to B setting them is perhaps not the best way to do it, however, it's still
valid as an answer.

OK, you've seen the light, but what about Cor, who also
thought your suggestion was the best method???

he he
LFS
 
"Larry Serflaten"
OK, you've seen the light, but what about Cor, who also
thought your suggestion was the best method???
Where?

By instance for a showdialog the method from OHM is in my opinion the best
solutions for the question.

This one from me. As you showed me did Terry not use a showdialog, so this
is not a little bit wrong as text but almost completly as what I wrote in my
previous message.

I still did not investigate as it I told in the prevous message, if this is
a challenge to do it better I will gladly accept, however I think with that
we will not win much, so better not.

Or is it only to enjoy it extra for you and to show it to us, than I am
happy with you.

:-))))

Cor
 
OK, your right I conceed that your method will work. I still think that mine
is cleaner and easier to read.

//Calling Form
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles searchButton.Click
Dim searchForm As New B
Dim DResult As DialogResult

DResult = searchForm.ShowDialog()
Select Case DResult
Case DialogResult.OK
Me.returnedText.Text = searchForm.UserName
Case DialogResult.Cancel
Me.returnedText.Text = "Cancelled"
End Select
searchForm.Close()
End Sub

//Called Form
Private m_UserName As String

Public ReadOnly Property UserName() As String
Get
Return m_UserName
End Get
End Property

Private Sub okButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles okButton.Click
Me.DialogResult = DialogResult.OK
Me.Hide()
End Sub

Private Sub canelButton_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles canelButton.Click
Me.DialogResult = DialogResult.Cancel
Me.Hide()
End Sub

Private Sub returnTextBox_TextChanged(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles returnTextBox.TextChanged
m_UserName = Me.returnTextBox.Text
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top