473,396 Members | 1,599 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,396 software developers and data experts.

how can i access a form from another form?

i have Form1 then i want to access the datetimepicker in the other form(Form2).. how do i do that? Answers are highly appreciated! thanks!
Feb 3 '10 #1
3 2065
Samishii23
246 100+
Google is your friend. But this post does provide a simple answer. (I'm learning too. lol)

.../answers/270705-access-textbox-value-another-form
Feb 3 '10 #2
tlhintoq
3,525 Expert 2GB
Although you can have Form1 directly access items on Form2 it isn't the recommended way to go. It ties the two forms tightly to each other. If a change is made in Form2 such as removing one of the controls, then code in Form1 breaks.

It is better to Form1 raise an event, and have Form2 contain its own code for how to react to this. This places responsibility for Form2 within Form2, and Form1 within Form1.

It keeps Form1 blissfully ignorant of Form2 - and a logging component - and a progress component, and a dozen other little black boxes that can be subscribed to events in Form1, all without Form1 being made responsible for directly affecting controls other than itself.

Events tutorial (including Form to Form which is the same as class to class)
Feb 3 '10 #3
Curtis Rutland
3,256 Expert 2GB
Well, if Form1 is the only one that can spawn Form2, and is in complete control of Form2, then Form1 directly accessing Form2 isn't a big problem. Especially if you are using the ShowDialog method rather than Show.

What I would do depends on the situation. If Form2 is a Dialog type form, in that when you create it you do something like this:
Expand|Select|Wrap|Line Numbers
  1. Form2 f2 = new Form2();
  2. f2.ShowDialog();
Then I would just add a public property to Form2. Something like this:
this is in the class for Form2
Expand|Select|Wrap|Line Numbers
  1. public DateTime MyDate
  2. {
  3.   get { return myDateTimePicker.Value; } //change this to whatever your datetimepicker's name is
  4. }
And just grab the value after the dialog is closed.
this is in Form1 where you spawn the form2
Expand|Select|Wrap|Line Numbers
  1. Form2 f2 = new Form2();
  2. Form2.ShowDialog();
  3. DateTime value = f2.MyDate;
If I were using the Show method rather than the ShowDialog method, and I wanted to know every time that Form2's DateTimePicker changed, I would (in form1) handle the form2 instance's ValueChanged event.
Feb 3 '10 #4

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

Similar topics

1
by: edself | last post by:
I have a form which displays a subform datasheet of information. I'd like to be able to quickly click on a particular record and open up another form showing more detailed information about that...
4
by: Vaughn | last post by:
When I pass a reference to my current form, frm_x, when I create and show another form, frm_y, do I have access to all of the public methods, controls, members, etc.. of frm_x from frm_y? In my...
3
by: fh1996 | last post by:
Form.Owner and Form.Parent, what's the difference between them? Form.ShowDialog() and Form.Show(), when to use which? Form.Activated(), what does it mean when saying a Form is "activated"? ...
2
by: authorking | last post by:
How to access a control in another form and how to make a form become a MDI container.
2
by: Stan | last post by:
I have a dataset in one form that I instanciate there. I would like to access that dataset from another form. dim ds as new myds (myds is a dataset on the first form and is generated from a...
0
by: Geraldine Hobley | last post by:
Hello I have a problem whereby I have a treeview control on one form called projecttree and I wish to clea the nodes from this control in another form The form that contains the treeview is...
4
by: JimC | last post by:
On my main form in a C# program, I create an instance of another form that contains a ListView control, in the usual way, that is: public class frmMain : System.Windows.Forms.Form { // ...
1
by: urcutesweety | last post by:
Hi, I have 2 frames(top n bottom) each has one web form. Now I want to show any error message that occur in the top frame to be displayed in the bottom frame. I dont want to be redirected to...
8
by: hoofbeats95 | last post by:
I don't think this should be this complicated, but I can't figure it out. I've worked with C# for several years now, but in a web environment, not with windows form. I have a form with a query...
7
by: rb0135 | last post by:
I have one form that initializes a class, called Players. But, I need to access these initialized values in another form. Obiously, if I do Players player = new Players(); it creates a new...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.