Passing Data between Windows Forms
-
I am trying to pass data between text boxes and can only get it to happen one-way. I have formA and open formB. Both forms are open and have textboxes on both. I can create a command button on formA to get the text from a text box on formB, but when I create a command button on formB to push the text back to formA it error out with no instance of object. I dim frm as formA and get to see the properties and controls of formA. Using a statement like me.frm.textbox1.text = me.formB.textbox2.text Any suggestions?? Rob
-
I am trying to pass data between text boxes and can only get it to happen one-way. I have formA and open formB. Both forms are open and have textboxes on both. I can create a command button on formA to get the text from a text box on formB, but when I create a command button on formB to push the text back to formA it error out with no instance of object. I dim frm as formA and get to see the properties and controls of formA. Using a statement like me.frm.textbox1.text = me.formB.textbox2.text Any suggestions?? Rob
You need to give formB a way to reference formA. One way of going about this is to add a private field of type formA to formB and then pass the calling instance of formA to formB in its constructor: (In formA...)
dim frmB as new formB(Me)
(In formB...)Private frm as formA Public Sub New(ByVal frmRef as formA) frm = frmRef End Sub
Then, in formB you can sayfrm.TextBox1.Text = "Howdy from formB!"
As an aside, you may want to reconsider the way you are designing your app. It might be a better idea to add public properties to your forms and update the display with these properties than to allow direct access to the controls from other classes. This would make it a lot easier to alter your classes in the future. For example, as it stands now, if you decide that you want to use a combo box instead of a text box you will have to change the code in both forms instead of just the form that is implementing the change. Charlie if(!curlies){ return; } -
You need to give formB a way to reference formA. One way of going about this is to add a private field of type formA to formB and then pass the calling instance of formA to formB in its constructor: (In formA...)
dim frmB as new formB(Me)
(In formB...)Private frm as formA Public Sub New(ByVal frmRef as formA) frm = frmRef End Sub
Then, in formB you can sayfrm.TextBox1.Text = "Howdy from formB!"
As an aside, you may want to reconsider the way you are designing your app. It might be a better idea to add public properties to your forms and update the display with these properties than to allow direct access to the controls from other classes. This would make it a lot easier to alter your classes in the future. For example, as it stands now, if you decide that you want to use a combo box instead of a text box you will have to change the code in both forms instead of just the form that is implementing the change. Charlie if(!curlies){ return; }Thanks Charlie. I made the Dims Public and it works that much better. Bob
-
Thanks Charlie. I made the Dims Public and it works that much better. Bob
I'm glad you got it working, but unless this is a throw away app, you may end up kicking yourself later when you decide to change the layout of your UI. A little extra coding now to encapsulate your classes will save you a lot of coding later. However, if you expect never to touch it again once it's done, public fields will work fine. Charlie if(!curlies){ return; }