Click here to Skip to main content
15,910,603 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to make a simple calculator with 2 form that are joined together by inheritance.
how i can pass form 1 text box's to form 2?
from 1 has just 2 text box.
form 2 has 1 text box and 4 button .
Posted
Comments
BillWoodruff 9-Mar-15 1:40am    
I don't think any meaningful answer can be given here unless you define:

1. what you mean by "inheritance"

2. why you would want to pass a TextBox from one to another: there is almost never a reason to pass a Control from one Form to another; in almost every case, any necessary communication between Forms can be achieved by other ways that do not expose an entire Control.

You can get a clear response here by describing what you are doing on the two Forms; how do you want them to interact ?
Akbar Fardi 9-Mar-15 1:45am    
form 1 has two text box's and i want to use from them in form 2 .in fact form 2 inherit
form 1 two text box's .
you say that we cant pass text box itself and we just pass it value .but how?
BillWoodruff 9-Mar-15 10:41am    
Hi, Since a Form cannot inherit from TextBoxes, and TextBoxes cannot inherit from Form, I think what you mean is that Form2 inherits from Form1. Please clarify that.

Without knowing what the purpose of the TextBoxes are, and the purpose of the communication between the two Forms, there's nothing I can say to assist you.
Akbar Fardi 9-Mar-15 14:51pm    
yes you say right .form 2 inherit from form 1.
in addition i want to make a simple calculator and i want to use inheritance in my program.
I get that we can't inherit a text box from a form but I want to know how can i use from text box in form 1 in form 2?
BillWoodruff 10-Mar-15 4:41am    
There are some interesting (and tricky) aspects of using inherited Forms in Windows Forms; you may have discovered some of the interesting behaviors already.

But, until you clearly state what you are doing and exactly what you want to achieve, I can't help you ... which I would be happy to do !

Please don't post the same question in multiple forums.

cheers, Bill

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA
 
Share this answer
 
To add to solution 1 there are several ways of doing this. However, you shouldn't probably pass the text box from a form to another, just the data the text box contains.

One way to inform the other form is to use events. Have a look at Passing Data between Windows Forms[^] and investigate how the demo is built. It should help you to get started.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 9-Mar-15 1:35am    
Good point in first paragraph, my 5.
—SA
Wendelius 9-Mar-15 2:23am    
Thanks SA :)
Sergey Alexandrovich Kryukov 23-Mar-15 1:05am    
I created an article on this topic, as my past answers often were not well understood, please see Solution 3.
—SA
As the question turned out to be very popular, and my previous answers often were not well understood, probably were not clear enough, I decided to write a Tips/Trick article complete with detailed code samples and explanations: Many Questions Answered at Once — Collaboration between Windows Forms or WPF Windows.

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900