我有一个主界面:mainform(有一个全局变量X),有一个对话框form(有个变量xx),利用ShowModal调出form,并且修改了xx的值,我怎么把xx 赋值给X?

解决方案 »

  1.   

    通俗的来讲,只要在mainform释放(mainform.free)之前可以对其任意操作!有关于你提出的相同:
    比如
    form=Tform.create(mainfrom);
    form.showmodal
    //在form中加个按纽 按下的返回值为ModalResult:=mrOK
    if form.ModalResult==mrOK the
    begin
    end;
    form.free;
      

  2.   

    把xx聲明為public,就可以在mainform裏調用 form.xx
      

  3.   

    你的全局变量最好放在一个Unit中,然后不同的Form引用这个Unit就可以了,你这个窗体赋值后另一个窗体自然会变得,也不用包含太多的窗体。你的对话框使自己创建的吧?
    如果调用系统创建的参照下面的函数:function  InputQueryNewOld(const ACaption,APrompt: string;var Value: string): Boolean;  //创建系统信息提示框;
    var
      Form: TForm;
      Prompt: TLabel;
      ImageIcon:TImage;
      DialogUnits: TPoint;
      ButtonTop, ButtonWidth, ButtonHeight: Integer;
      SysInt:Integer;
    begin
      Result := False;
      Form := TForm.Create(Application);
      with Form do
      begin
        try
    //     Canvas.Font := Font;
          //----------------------
          Font.Charset := GB2312_CHARSET;
          Font.Color := clWindowText;
          Font.Height := -12;
          Font.Name := #23435#20307;
          Font.Style := [];
          //----------------------
          DialogUnits := GetAveCharSize1(Canvas);
          BorderStyle := bsDialog;
          Caption := ACaption;
          ClientWidth := MulDiv(240, DialogUnits.X, 4);
          Position := poScreenCenter;
          ImageIcon :=TImage.Create(Form);
          with ImageIcon do
          begin
            Name := 'Image';
            Parent := Form;
            Picture.Icon.LoadFromFile('Text.ico');
            SetBounds(20,8 , 32, 32);
          end;
          Prompt := TLabel.Create(Form);
          with Prompt do
          begin
            Parent := Form;
            Caption := APrompt;
            Left := MulDiv(50, DialogUnits.X, 4);
            Top := MulDiv(8, DialogUnits.Y, 8);
            Constraints.MaxWidth := MulDiv(164, DialogUnits.X, 4);
            WordWrap := True;
          end;
          ButtonTop := Prompt.Top + Prompt.Height + 15;
          ButtonWidth := MulDiv(50, DialogUnits.X, 4);
          ButtonHeight := MulDiv(14, DialogUnits.Y, 8);
          with TButton.Create(Form) do
          begin
            Parent := Form;
            Caption := '确 定';
            ModalResult := mrOk;
            Default := True;
            SetBounds(MulDiv(38, DialogUnits.X, 4), ButtonTop, ButtonWidth,
              ButtonHeight);
            Form.ClientHeight := Top + Height + 13;
          end;
          with TButton.Create(Form) do
          begin
            Parent := Form;
            Caption := '新数据';
            ModalResult := mrAbort;
            Default := True;
            SetBounds(MulDiv(92, DialogUnits.X, 4), Prompt.Top + Prompt.Height + 15,
              ButtonWidth, ButtonHeight);
            Form.ClientHeight := Top + Height + 13;
          end;
          with TButton.Create(Form) do
          begin
            Parent := Form;
            Caption := '取 消';
            ModalResult := mrIgnore;
            Default := True;
            SetBounds(MulDiv(146, DialogUnits.X, 4), Prompt.Top + Prompt.Height + 15,
              ButtonWidth, ButtonHeight);
            Form.ClientHeight := Top + Height + 13;
          end;
          SysInt:=ShowModal;
          if SysInt = mrOk then
          begin
            Value := '1';
            Result := True;
          end
          else
          begin
            if SysInt = mrAbort then
            begin
              Value := '2';
              Result := True;
            end
            else
              Value:='ElseExit';
          end;
        finally
          Form.Free;
        end;
      end;
    end;