June 27th, 2012, 09:38 AM
How to pass data from "created" form to mainform
Hi, I have an app that I'm working on at the moment, for one of the fields in the main form I have a date that needs to be entered. So using the "OnEnter" event I'm creating a date pop up window with a calendar so the user can simply click on a chosen date.
I have no idea how to pass the "calendar1.calendardate" output to the main form field "MainFrm1.date.text"
I'm told that I should use a public variable based but from the example I found (2001 so it's old) I should make my 2nd form use the First form in the "uses" section...
From then on I get an error "Circular Unit reference" which makes sense...but then how does the Calendar Pop up form "know" about the MainForm
Here is what I have to open the form with the MainForm:
I'm confused on what needs to be in TFcalendar to pass pack to MainForm
procedure TForm1.LusrWeddingDateEnter(Sender: TObject);
var CP : Tfcalendar;
CP := Tfcalendar.Create(application);
Please could you explain and if you don't mind using a simple example...
I appreciate your help!
June 27th, 2012, 12:57 PM
have you checked out the DateTimePicker component located in the Win32 palette, or if you need a database aware version of the datetimepicker, you could download and install this http://delphi.about.com/library/code/ncaa042004a.htm .
I think using one of those components would be the simplest solution.
As to having one form "communicate" with another form, it's not difficult, but there are some issues to be aware of.
To deal with the circular reference issue, the format of a *.pas file offers two locations to have a "uses" clause. The first is right after the interface directive and the other is right after the implementation directive.
You can put both dependent units in the uses clause after the implementation directive of their respective units, or you can put one in the interface section and the other in the implementation section. Either works
Once you have the units listed in the uses clause, then the variables, function and procedure methods declared in the other form's public section should be available to the "other" form. You could declare a public function to set, for example, Edit1.Text to a value.
From your other form, after adding MainForm to the uses clause, you would be able to use it:
TForm1 = class(TForm)
procedure SetEditText(str: String);//add this
procedure TForm1.SetEditText(str: String);
Edit1.Text := str;
As far as issues with having two forms communicate, obviously both forms would have to exist for them to communicate. Not such a big deal if a child form is talking with the main form (to initiate communications, the child would have to exist, and the main form must exist for the application to exist), but if two child forms, or a main form needs to communicate with child form, then you run the possibility of attempting to access something that doesn't exist. That would cause an access violation, possibly crashing your application.
MainForm.SetEditText('This text will be put in Edit1');
Comments on this post
June 27th, 2012, 03:29 PM
My word do I feel like an idiot... I kept on searching for a calendar component! I never thought of searching for 'date'!
Thank you for pointing that out! But your advice is not lost, I will be opening multiple forms in my app so the information you have supplied above will be invaluable! and it is has just the information need to answer the question!
I find many examples tend to show other code not needed which makes it more difficult to understand (for me anyway) so hopefully someone else who is stuck will find it in a search at a later point!
June 27th, 2012, 03:42 PM
Don't be too critical on yourself, we all started at square one, so that's is why these forums are so great.
Also, just to be aware of it, the DateTimePicker is just that, both date and time, but only one at a time. If you need to enter a time, set the component's 'Kind' property to 'dkTime' rather than dkDate.
Also, there is a calendar component which is also in the Win32 palette called TMonthCalendar. The difference is that the datetimepicker is basically diplayed as a TEdit component, with a popup (er... i I mean popdown) calendar. The TMonthCalendar is displayed as a calendar