PetCar Posted March 3, 2004 Posted March 3, 2004 (edited) Hi, this question have been asked before.. but i dont get it. Ive searched the forum A LOT of times for questions related to mine. I want to use a module with common functions to edit controls on forms. The code i got right now: # Form1 Public Class Form1 Inherits System.Windows.Forms.Form Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click ModuleTestFunction() End Sub End Class #Module1 Module Module1 Public frm1 As New Form1 Public Function ModuleTestFunction() As Object frm1.TextBox1.Text = "Hello World!" End Function End Module When clicking the button, nothing happends. If i put an msgbox into the function, it will show up, but the textbox remains unchanged. Can anyone please tell me what i do wrong? Thanks! // Peter Edited March 3, 2004 by PetCar Quote
*Experts* mutant Posted March 3, 2004 *Experts* Posted March 3, 2004 You need to have a reference to the instance of the Form1 you want to manipulate. What you are doing is declaring a new form1, you are not referring to the instance you are showing. Although that is not excatly what you are looking for take a look into this thread: http://www.xtremedotnettalk.com/showthread.php?s=&threadid=83092 Quote
PetCar Posted March 3, 2004 Author Posted March 3, 2004 Thanks! I got it to work.. with a class. But is this the right procedure? #Form1 Public Class Form1 Inherits System.Windows.Forms.Form Private otherClass As New Class1(Me) Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click otherClass.ShowTextContent() End Sub End Class #Class1 Public Class Class1 Private callingForm As Form1 Public Sub New(ByVal newCallingForm As Form) callingForm = newCallingForm End Sub Public Sub ShowTextContent() callingForm.TextBox1.Text = "Hello World!" End Sub End Class Thanks! // Peter Quote
Leaders Iceplug Posted March 3, 2004 Leaders Posted March 3, 2004 Looks fine to me. What procedure are you talking about? (I'd usually instantiate another new class within the 'New' subroutine of a class) Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PetCar Posted March 3, 2004 Author Posted March 3, 2004 I want to create procedures and functions for showing a notifier popup, change the statusbar text etc. (I'd usually instantiate another new class within the 'New' subroutine of a class) How do you mean? I have been programming VB6 for way to long.. :) // Peter Quote
Leaders Iceplug Posted March 3, 2004 Leaders Posted March 3, 2004 Like this: Private X As Class 1 Public Sub New() X = New Class1 End Sub This is how I usually do it in VB6, but I don't know if there's anything 'evil' about the other way in .NET. For the notifiers and status bars, they should be accessible, granted that you actually have them on your form. :) You can also make procedures that take controls as ByRef arguments, and just change the values on the control from there. Quote Iceplug, USN One of my coworkers thinks that I believe that drawing bullets is the most efficient way of drawing bullets. Whatever!!! :-(
PetCar Posted March 3, 2004 Author Posted March 3, 2004 Thanks alot guys!! Now im on track again :) I got a big project comming up that have to be done in .net so i have to play around with this "new" OOP. VB6 has infected my brain :) // Peter Quote
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.