#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13

    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:

    Code:
    procedure TForm1.LusrWeddingDateEnter(Sender: TObject); 
    var CP : Tfcalendar; 
    begin
         
    CP := Tfcalendar.Create(application);
    cp.set_PopupParent(self);     
    CP.Show;  
    end;
    I'm confused on what needs to be in TFcalendar to pass pack to MainForm

    Please could you explain and if you don't mind using a simple example...

    I appreciate your help!

    Greg.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    First off,

    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.
    Code:
    unit MainForm;
    
    interface
    
    uses
       ...
    
    type
       TForm1 = class(TForm)
          ...
          Edit1: TEdit;
       private
          ...
       public
          procedure SetEditText(str: String);//add this
          ...
       end;
    
       var
          Form1: TForm1
    
       implementation
    
       {$R *.dfm}
     
       uses ...
    
       procedure TForm1.SetEditText(str: String);
       begin
          Edit1.Text := str;
       end;
    From your other form, after adding MainForm to the uses clause, you would be able to use it:
    Code:
    MainForm.SetEditText('This text will be put in Edit1');
    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.

    Comments on this post

    • clivew agrees
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2002
    Posts
    86
    Rep Power
    13
    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!

    Thank you!

    Greg.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2008
    Posts
    350
    Rep Power
    7
    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

IMN logo majestic logo threadwatch logo seochat tools logo