Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

How do I call a var in form1 from a unit?

mydelphimydelphi Member Posts: 62
I have a trackbar on my main form1 window, and I need to use it's .position value from
inside various functions that are located in another unit that my Form1 calls in its Uses.
The name of this unit is myUnit.

uses windows .. myUnit;

So, I made an integer variable and named it myVar. So, in the myUnit, the function
needs the integer value in myVar, but it will not compile for reasons that escape me or
lack of knowledge in this area of programming.

Any help would be appreciated, thanks..

-mydelphi

Comments

  • zibadianzibadian Member Posts: 6,349
    : I have a trackbar on my main form1 window, and I need to use it's
    : .position value from
    : inside various functions that are located in another unit that my
    : Form1 calls in its Uses.
    : The name of this unit is myUnit.
    :
    : uses windows .. myUnit;
    :
    : So, I made an integer variable and named it myVar. So, in the
    : myUnit, the function
    : needs the integer value in myVar, but it will not compile for
    : reasons that escape me or
    : lack of knowledge in this area of programming.
    :
    : Any help would be appreciated, thanks..
    :
    : -mydelphi

    You don't need an additional variable for this. In unit containing your form is already a variable for the form. This you can use to gain access to the form in any part of your code. Then you can call on any of the components placed on the form and all the public properties and methods. Here's a small example, assuming Form1 is located in Unit1 and there's a TrackBar1 on the form:
    [code]
    unit Unit2;

    interface

    procedure ResetTrackBar;

    implementation

    uses
    Unit1;

    procedure ResetTrackBar;
    begin
    Form1.TrackBar1.Position := 0;
    end;

    end.
    [/code]
    You can use Unit2 in Unit1 if necessary.
  • mydelphimydelphi Member Posts: 62
    : : I have a trackbar on my main form1 window, and I need to use it's
    : : .position value from
    : : inside various functions that are located in another unit that my
    : : Form1 calls in its Uses.
    : : The name of this unit is myUnit.
    : :
    : : uses windows .. myUnit;
    : :
    : : So, I made an integer variable and named it myVar. So, in the
    : : myUnit, the function
    : : needs the integer value in myVar, but it will not compile for
    : : reasons that escape me or
    : : lack of knowledge in this area of programming.
    : :
    : : Any help would be appreciated, thanks..
    : :
    : : -mydelphi
    :
    : You don't need an additional variable for this. In unit containing
    : your form is already a variable for the form. This you can use to
    : gain access to the form in any part of your code. Then you can call
    : on any of the components placed on the form and all the public
    : properties and methods. Here's a small example, assuming Form1 is
    : located in Unit1 and there's a TrackBar1 on the form:
    : [code]:
    : unit Unit2;
    :
    : interface
    :
    : procedure ResetTrackBar;
    :
    : implementation
    :
    : uses
    : Unit1;
    :
    : procedure ResetTrackBar;
    : begin
    : Form1.TrackBar1.Position := 0;
    : end;
    :
    : end.
    : [/code]:
    : You can use Unit2 in Unit1 if necessary.


    Thanks for your help zibadian. However, I finally came to a solution early this morning.
    What I did was make a Lib.pas file and add in the var myvar : integer; to it and then
    called it from inside the myUnit. Both Form1 and myUnit have access to this variable.

    Because my code snipplets are growing more and more and getting more difficult to
    maintain from inside a project, I decided to just make a middle-man and created the
    Lib.pas for this purpose. I want to add in Functions and ASM code to this and much
    more.

    The only thing I am wondering now is weather or not I am properly calling it from my
    project.

    1, Should I call be calling it from my Form1 or myUnit?
    2, Where is the right place to add the inlcude to: after the Interface or Implementation?

    Thanks again,
    -mydelphi
  • zibadianzibadian Member Posts: 6,349
    : : : I have a trackbar on my main form1 window, and I need to use it's
    : : : .position value from
    : : : inside various functions that are located in another unit that my
    : : : Form1 calls in its Uses.
    : : : The name of this unit is myUnit.
    : : :
    : : : uses windows .. myUnit;
    : : :
    : : : So, I made an integer variable and named it myVar. So, in the
    : : : myUnit, the function
    : : : needs the integer value in myVar, but it will not compile for
    : : : reasons that escape me or
    : : : lack of knowledge in this area of programming.
    : : :
    : : : Any help would be appreciated, thanks..
    : : :
    : : : -mydelphi
    : :
    : : You don't need an additional variable for this. In unit containing
    : : your form is already a variable for the form. This you can use to
    : : gain access to the form in any part of your code. Then you can call
    : : on any of the components placed on the form and all the public
    : : properties and methods. Here's a small example, assuming Form1 is
    : : located in Unit1 and there's a TrackBar1 on the form:
    : : [code]: :
    : : unit Unit2;
    : :
    : : interface
    : :
    : : procedure ResetTrackBar;
    : :
    : : implementation
    : :
    : : uses
    : : Unit1;
    : :
    : : procedure ResetTrackBar;
    : : begin
    : : Form1.TrackBar1.Position := 0;
    : : end;
    : :
    : : end.
    : : [/code]: :
    : : You can use Unit2 in Unit1 if necessary.
    :
    :
    : Thanks for your help zibadian. However, I finally came to a
    : solution early this morning.
    : What I did was make a Lib.pas file and add in the var myvar :
    : integer; to it and then
    : called it from inside the myUnit. Both Form1 and myUnit have access
    : to this variable.
    :
    : Because my code snipplets are growing more and more and getting more
    : difficult to
    : maintain from inside a project, I decided to just make a middle-man
    : and created the
    : Lib.pas for this purpose. I want to add in Functions and ASM code
    : to this and much
    : more.
    :
    : The only thing I am wondering now is weather or not I am properly
    : calling it from my
    : project.
    :
    : 1, Should I call be calling it from my Form1 or myUnit?
    : 2, Where is the right place to add the inlcude to: after the
    : Interface or Implementation?
    :
    : Thanks again,
    : -mydelphi

    1: I cannot tell, because that's a design decision.
    2: I usually add as many units to the uses-list after the implementation as possible. The only times I add them to the uses-list after the interface, is when the declared variables/types/procedures need to be available between the interface and the implementation. This way I can create circular unit references without any errors.
Sign In or Register to comment.