Solved

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

Posted on 2014-11-25
8
103 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
  • 3
  • 3
  • 2
8 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
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
Comment Utility
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 62

Expert Comment

by:Fernando Soto
Comment Utility
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
 

Author Comment

by:RadhaKrishnaKiJaya
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
>>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 62

Accepted Solution

by:
Fernando Soto earned 500 total points
Comment Utility
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
Comment Utility
Thank you very much. It worked!!
0
 
LVL 62

Expert Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now