Ezguy Posted August 13, 2003 Posted August 13, 2003 HI, I have a problem with form communication. This is the problem. I have two forms form1 and form2. in form2 there is a function. I want to know how to call this function from form1. I tried this form2.functionname but it gives a error. Hope some one can help me Quote www.itfriend.com [ get connected with your IT friends]
Leaders dynamic_sysop Posted August 13, 2003 Leaders Posted August 13, 2003 Dim frm2 As New Form2() frm2.PerformFunction( " your function " ) Quote
Ezguy Posted August 13, 2003 Author Posted August 13, 2003 Is there any other way of calling the form withouth creating a new instance of it. Quote www.itfriend.com [ get connected with your IT friends]
*Gurus* divil Posted August 13, 2003 *Gurus* Posted August 13, 2003 No. You'll have to retain whatever instance you already created somewhere. Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
Ezguy Posted August 13, 2003 Author Posted August 13, 2003 Let me tell you what I really want. I have form1 and form2. formm1 is the main form and form2 resides in form1. in form2 there is a picturebox. the picture is shown in the picture box using a function. I want to place a new picture in this picture box by clicking a button which is in form1. If I create a new instance as dynamic_sysop it does not work. hope some one understand me. sorry for the poor english. Quote www.itfriend.com [ get connected with your IT friends]
Leaders dynamic_sysop Posted August 13, 2003 Leaders Posted August 13, 2003 if you declare form2 first ( below the windows generated code area ) then i'm not sure what the problem seems to be. Private frm2 As New Form2() Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm2.Show() frm2.PictureBox1.Image = Image.FromFile("C:\Documents and Settings\den\My Documents\My Pictures\flammable.gif") End Sub that puts an image in a picturebox on form2 fine for me. Quote
Ezguy Posted August 13, 2003 Author Posted August 13, 2003 HI, this is the code Iam using ---------------------------------------------- 'in form1 Public Class Form1 Inherits System.Windows.Forms.Form Dim pform As Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CType(pform, Form2).abc() End Sub ----------------------------------------------------- 'in form2 Public Class Form2 Inherits System.Windows.Forms.Form Public Function abc() pic1.image = the picture path End Function End Class ---------------------------------------------- Now I get this error An unhandled exception of type 'System.NullReferenceException' occurred in WindowsApplication6.exe Additional information: Object reference not set to an instance of an object. Quote www.itfriend.com [ get connected with your IT friends]
Leaders dynamic_sysop Posted August 13, 2003 Leaders Posted August 13, 2003 try changing this: Inherits System.Windows.Forms.Form Dim pform As Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click CType(pform, Form2).abc() <<<< this line End Sub to this: Inherits System.Windows.Forms.Form Dim pform As Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click frm2 = DirectCast(New Form2(), Form)'/// to like this , if you dont want to use Dim frm2 As New Form2() End Sub Quote
Ezguy Posted August 13, 2003 Author Posted August 13, 2003 yep It works now :) :) thanks Quote www.itfriend.com [ get connected with your IT friends]
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.