![]() |
![]() ![]() ![]() ![]() |
|
Прочитать данные из другого дочернего окнаDelphi , Программа и Интерфейс , MDI приложенияПрочитать данные из другого дочернего окна
Оформил: DeeCo
type
TMDIChildForm = class(TForm)
Edit1: TEdit;
private
{ Private declarations }
procedure ReadDataFromOtherMDIChildForm;
end;
var
MDIChildForm: TMDIChildForm;
implementation
{$R *.DFM}
uses
MainForm;
// Property FormStyle of this form is fsMDIForm
procedure TMDIChildForm.ReadDataFromOtherMDIChildForm;
var
i: Integer;
DataFromOtherForm: string;
begin
// Suppose you have created three different MDIChild forms of the same type,
// each with the following caption: "aaa", "bbb", "ccc".
// You are currently on form with caption "aaa" and want to read data
// contained on form with caption "ccc".
// You can find here the code, you have to use the "as" clause and
// properties MDIChildCount and MDIChildren:
// First you have to find where the form "ccc" is in memory;
for i := 0 to MDIForm.MDIChildCount - 1 do
begin
if (Pos('ccc', MDIForm.MDIChildren[i].Caption) 0) then
Break;
end;
// Check to see if the form is the last on MDIChildren array and
// correct I variable
if (i = MDIChildCount) then Dec(i);
// I variable contains the index of the form with caption 'ccc'
if (Pos('ccc', MDIForm.MDIChildren[i].Caption) 0) then
begin
// If the form with caption 'ccc' exists then you access data and show it
// The following line of code is very interesting, look at the "as" clause,
// if you have different types of MDIChild forms, you simply change
// the type of form after the "as" clause
// The data you want is contained on Edit1.Text
with (MDIForm.MDIChildren[I] as TMDIChildForm).Edit1 do
DataFromOtherForm := Text;
ShowMessage(DataFromOtherForm);
end;
end;
Программный код написан на языке Delphi для создания оконного приложения с использованием архитектуры Multiple Document Interface (MDI), которая позволяет иметь несколько открытыми окнами одновременно. Основная цель этого кода - прочитать данные из другого дочернего окна MDI и отобразить их в сообщении. Работает следующим образом:
Код хорошо структурирован и легко понятен, но более эффективным и масштабируемым было бы хранение дочерних окон MDI в коллекции или списке, а не перебор всех форм каждый раз, когда данные нужно прочитать из другой формы. В статье описывается способ чтения данных из другого дочернего окна в приложении на Delphi, используя свойство MDIChildren и оператор 'as' для доступа к элементам формы. Комментарии и вопросыПолучайте свежие новости и обновления по Object Pascal, Delphi и Lazarus прямо в свой смартфон. Подпишитесь на наш :: Главная :: MDI приложения ::
|
||||
©KANSoftWare (разработка программного обеспечения, создание программ, создание интерактивных сайтов), 2007 | ||||