Solved

How to write the New() method when a form is called by two different forms

Posted on 2014-11-25
8
107 Views
Last Modified: 2014-11-25
Hello Experts,

I have a form lets say "Form A". I have two different forms called "Form B"  and "Form C". Now my program(Form B)  is calling "form A"  with some parameters. I have defined a New() method.

Now my requirement is that Form C should also call Form A with some parameters.  What can I do so that the New() method will understand which form is calling.  

Thanks in advance.
0
Comment
Question by:RadhaKrishnaKiJaya
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40465046
You can have two methods with the same name but with different parameter lists.
eg.
New(int i)
and
New(String s, int j)


OR
You could just pass something to identify which type the calling form is as a parameter
eg.
New(int TypeOfForm)
{
switch(TypeOfForm)
{
case 1:
....
case 2:
....
0
 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40465078
Thank you for your reply.

Right now  my New function looks like this.
Public Sub New(ByVal formParameter As StockSearch) ''Where stockSearch is the name of the form
        InitializeComponent()
        StockSearchObj = formParameter
     
    End Sub
So can you please tell me how to change the code.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40465142
Make sure that you have a class variable like so to hold the value.

Dim StockSearchObj As StockSearch

Otherwise it looks good. Are you having issues with it?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:RadhaKrishnaKiJaya
ID: 40465155
Thanks for ur reply.  This works fine when the stocksearch.vb call the frmModify.vb. Now I want RackStatus.VB should call the same frmModify.vb.
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 40465199
>>So can you please tell me how to change the code.

You run whichever code is appropriate to the calling form.  eg. with a switch/case statement or an if statement.
0
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40465370
Hi RadhaKrishnaKiJaya;

Change your code as follows

'' Reference to the form that created this one
Dim formObj As Form

Public Sub New(ByVal formParameter As Form)
        InitializeComponent()
        formObj = formParameter      
End Sub

Now when you need to access the formObj from this form you need to do something like this

Select Case formObj.GetType().Name
    '' Enter the form name in the case statements, this app has forms Form1 and Form3
    Case "Form1"
        '' This writes to Form1's TextBox1
        Dim f1 = CType(theForm, Form1)
        f1.TextBox1.Text = "Hello World"
    Case "Form3"
        '' This writes to Form3's TextBox1
        Dim f3 = CType(theForm, Form3)
        f3.TextBox1.Text = "Hello World 3"
    Case Else
        Console.WriteLine("Form not found")
End Select

Open in new window

0
 

Author Closing Comment

by:RadhaKrishnaKiJaya
ID: 40465386
Thank you very much. It worked!!
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40465395
Not a problem RadhaKrishnaKiJaya, glad I was able to help.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question