473,325 Members | 2,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Passing values from form2 to form1 using objects

26
it's no longer a problem passing variables between two form its just this

form1 is still open

i do a few actions that change a var in form2

and i want the new number to be send to form1 immediatly after those changes are done

how can i do so without opening a new form1 and sending the var into the only form1 that is open
Mar 9 '07 #1
16 3814
vijaydiwakar
579 512MB
it's no longer a problem passing variables between two form its just this

form1 is still open

i do a few actions that change a var in form2

and i want the new number to be send to form1 immediatly after those changes are done

how can i do so without opening a new form1 and sending the var into the only form1 that is open
pls explain thy problem in detail
Mar 10 '07 #2
shoram
26
pls explain thy problem in detail
the problem is that i dont know how to write somthing in form1 while im in form2
without opening a new form1
Mar 10 '07 #3
vijaydiwakar
579 512MB
the problem is that i dont know how to write somthing in form1 while im in form2
without opening a new form1
to access the form u must have to create the object.
try this code
for that add two forms and one module and try this code
Expand|Select|Wrap|Line Numbers
  1. 'in form1
  2. Private Sub Form_Load()
  3.    ReDim myObj(0)
  4.    Set myObj(0) = Me
  5.    Form2.Show
  6. End Sub
  7. 'in form2
  8. Private Sub Form_Click()
  9. myObj(0).Caption = "hai"
  10. End Sub
  11. ' in module
  12. Public myObj() As Form
  13.  
This will defenetly solve thy problem
Mar 10 '07 #4
shoram
26
im sorry but can you do it in c# i dont know anything in vb and thanks for helping
Mar 10 '07 #5
vijaydiwakar
579 512MB
im sorry but can you do it in c# i dont know anything in vb and thanks for helping
I'm sorry i don't no anything in C#
Mar 10 '07 #6
kenobewan
4,871 Expert 4TB
Suggest that you find a converter.
Mar 10 '07 #7
shoram
26
where can i find a converter? i didnt know they exist
Mar 10 '07 #8
SammyB
807 Expert 512MB
It's the same principle in C#, but you will need to pass the Form1 object to Form2 and change the Modifiers property of any control that you want to access from Privater to Internal. HTH --Sam

You can also create an event in Form2 that Form1 could fire whenever something changes. That is much more complecated, but easily done. If that is what you want, you should tell us whether you are using 2003 or 2005.

Here is the code for Form1:
Expand|Select|Wrap|Line Numbers
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             Form2 FrmChild = new Form2(this);
  4.             FrmChild.Show();
  5.         }
Here is the code for Form2. Notice that the constructor is overridden to get the Form1 object:
Expand|Select|Wrap|Line Numbers
  1.         private Form1 FrmParent;
  2.         public Form2(Form1 parent)
  3.         {
  4.             InitializeComponent();
  5.             FrmParent = parent;
  6.         }
  7.         private void button1_Click(object sender, EventArgs e)
  8.         {
  9.             this.textBox1.Text = FrmParent.textBox1.Text;
  10.         }
  11.  
  12.  
Mar 10 '07 #9
shoram
26
sorry for the late replay but i have 2003 and the method you gave me doesnt work
if its not so hard can you please tell me how to do that in 2003
thanks in advance
Mar 14 '07 #10
SammyB
807 Expert 512MB
sorry for the late replay but i have 2003 and the method you gave me doesnt work
if its not so hard can you please tell me how to do that in 2003
thanks in advance
Well, I did do it in 2005, but it does not use any new features. What problem are you having?
Mar 14 '07 #11
shoram
26
i over came the error but how do i insert the value into the textbox?
Mar 20 '07 #12
shoram
26
i over came the error but how do i insert the value into the textbox?
ok forget that replay, my problem is diffrent
i need to pass a value from form2 to form1 not the other way
Mar 20 '07 #13
SammyB
807 Expert 512MB
ok forget that replay, my problem is diffrent
i need to pass a value from form2 to form1 not the other way
That's what I did in post #9 above, only I put the value into a Textbox on Form1. If you need to pass back a value, then you will need to create a Form1 property for Form2 to use:
Expand|Select|Wrap|Line Numbers
  1. public int Know;
  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4.     Form2 FrmChild = new Form2(this);
  5.     FrmChild.ShowDialog();
  6.     MessageBox.Show(Know.ToString());
  7. }
Then, Form2 just puts the value into that property:
Expand|Select|Wrap|Line Numbers
  1. private Form1 FrmParent;
  2. public Form2(Form1 parent)
  3. {
  4.     InitializeComponent();
  5.     FrmParent = parent;
  6. }
  7. private void button1_Click(object sender, EventArgs e)
  8. {
  9.     FrmParent.Know = 42;
  10.     this.Close();
  11. }
Mar 20 '07 #14
shoram
26
by that answer i can surly belive that you saw the hitch hikcer galxci guide but not about that. when i did the thing you told me to it opened a diffrent form telling me the answer and i wanted that the form1 to get the value (without closing)
Mar 21 '07 #15
SammyB
807 Expert 512MB
by that answer i can surly belive that you saw the hitch hikcer galxci guide but not about that. when i did the thing you told me to it opened a diffrent form telling me the answer and i wanted that the form1 to get the value (without closing)
If you leave the form open (Show instead of ShowDialog) then Form1 will not know when the value changes unless you also throw an event back in Form1. See http://www.braintique.com/csharp/cha...ch-intro.shtml for an example of how to setup a custom event. There are five key steps:
  1. create a delegate
  2. associate the delegate with the event
  3. raise the event
  4. associate the event handler with the event
  5. write the event handler
The first time that you do this is very confusing. so I would work through the Braintique sample and then apply that to your two forms. HTH --Sam
Mar 21 '07 #16
shoram
26
If you leave the form open (Show instead of ShowDialog) then Form1 will not know when the value changes unless you also throw an event back in Form1. See http://www.braintique.com/csharp/cha...ch-intro.shtml for an example of how to setup a custom event. There are five key steps:
  1. create a delegate
  2. associate the delegate with the event
  3. raise the event
  4. associate the event handler with the event
  5. write the event handler
The first time that you do this is very confusing. so I would work through the Braintique sample and then apply that to your two forms. HTH --Sam
thatks alot you really saved me
Mar 23 '07 #17

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Michael C | last post by:
What's the best wat to pass data between two separate forms. For instance, Form1 invokes Form2 like this: Form z = new Form2(); z.Show(); How can I pass back strings and boolean values from...
4
by: thawk | last post by:
I have tried to pass information from one form to another using the described method in the documentation, but everytime I attempt to do this I get an error message that states that I have a null...
3
by: Steve B. | last post by:
Can't pass an array from one class to another. Steve Class Form1 { public string dGridTitles = new string {"xyz",,,,,}; public string getdGridTitles() {
8
by: Chris | last post by:
Hi, I have two froms (form1 and form2). I want to be able to pass values from form 1 to form2 and be able to use those values leter in form2. This is my code for form1 Private Sub...
7
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
7
by: The Doctor | last post by:
A rather elementary question, In VB5, how can I pass a variable from one form to another?
10
by: captainphoenix | last post by:
I have VB2005 and I need to pass intnumplayers from form1 to form2. Simply declaring the variable in a global module WILL NOT SOLVE MY PROBLEM, as I need to pass the values themselves from form1 to...
6
by: olsongt | last post by:
I have a C# problem I've been struggling with for a week now. I can't figure it out. I've made a sample application to demonstrate my problem: how do I pass data between forms? I've read tons of...
8
by: pt36 | last post by:
I have two pages one.html two.html page one.html have a form named form1 and a textarea named text1 man one write in the textarea text 1 page two.html have a form named form2 and a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.