Q&A

  • [요청]아래 코딩을 좀더 매끄럽게..
며칠전부터 델파이를 접하게 뒈었습니다..

몇가지 나름데로 숙제 같은 형식으로 배우고 있는데..



아래 코딩을 보면...(넘넘 초보라 물어보기 부끄럽습니다...^^*)



통신설정값을 정해주는 프로그램을 작성했습니다.





unit FMain;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

StdCtrls, Buttons,

Inifiles,

FileCtrl;



type

TForm1 = class(TForm)

cbComport: TComboBox;

cbBaudrate: TComboBox;

cbStopBits: TComboBox;

cbParity: TComboBox;

cbBytesize: TComboBox;

Label1: TLabel;

Label2: TLabel;

Label3: TLabel;

Label4: TLabel;

Label5: TLabel;

btnLoad: TBitBtn;

btnSave: TBitBtn;

btnSaveAs: TBitBtn;

lblComport: TLabel;

lblBytesize: TLabel;

lblParity: TLabel;

lblStopbits: TLabel;

lblBaudrate: TLabel;

OpenDialog1: TOpenDialog;

SaveDialog1: TSaveDialog;

procedure btnSaveClick(Sender: TObject);

procedure cbComportChange(Sender: TObject);

procedure cbBaudrateChange(Sender: TObject);

procedure cbStopBitsChange(Sender: TObject);

procedure cbParityChange(Sender: TObject);

procedure cbBytesizeChange(Sender: TObject);

procedure btnLoadClick(Sender: TObject);

procedure btnSaveAsClick(Sender: TObject);

private

{ Private declarations }

public

{ Public declarations }

end;



var

Form1: TForm1;



implementation

var

INI : TIniFile;



// 첫번째 질문

// ini파일을 만들때 마다 코딩을 해 주었습니다..

// 처음 한번만 정의해 주고 그것을 불러 쓰는 방법은 없는지...?

//

///////////////////////////////////////////////////////////////

// Comport := 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6,

// COM7:7 COM8:8)';

// Baudrate := 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)';

// StopBits := 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)';

// Parity := 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)';

// Bytesize := 'Bytesize(7Bit:0, 8Bit:1)';

/////////////////////////////////////////////////////////////////



{$R *.DFM}



// INI에서 불러서 값을 각각의 콤포 박스에 넣기

procedure TForm1.btnLoadClick(Sender: TObject);

begin



if OpenDialog1.Execute Then

begin



INI := TIniFile.Create(OpenDialog1.FileName);



cbComport.ItemIndex := INI.ReadInteger('Communication',

'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', 0);

lblComport.Caption := IntToStr(cbComport.ItemIndex);



cbBaudrate.ItemIndex := INI.ReadInteger('Communication',

'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', 0);

lblBaudrate.Caption := IntToStr(cbBaudrate.ItemIndex);



cbStopBits.ItemIndex := INI.ReadInteger('Communication',

'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', 0);

lblStopBits.Caption := IntToStr(cbStopBits.ItemIndex);



cbParity.ItemIndex := INI.ReadInteger('Communication',

'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', 0);

lblParity.Caption := IntToStr(cbParity.ItemIndex);



cbBytesize.ItemIndex := INI.ReadInteger('Communication',

'Bytesize(7Bit:0, 8Bit:1)', 0);

lblBytesize.Caption := IntToStr(cbBytesize.ItemIndex);



INI.Free;

end;

end;



// 현재 불러진 INI에파일에 설정한 값을 저장

procedure TForm1.btnSaveClick(Sender: TObject);

begin

if FileExists(OpenDialog1.FileName) then

begin

INI := TIniFile.Create(OpenDialog1.FileName);



INI.WriteInteger('Communication',

'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

INI.WriteInteger('Communication',

'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

INI.WriteInteger('Communication',

'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

INI.WriteInteger('Communication',

'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

INI.WriteInteger('Communication',

'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);

end

else

if FileExists(SaveDialog1.FileName) Then

begin

INI := TIniFile.Create(SaveDialog1.FileName);



INI.WriteInteger('Communication',

'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

INI.WriteInteger('Communication',

'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

INI.WriteInteger('Communication',

'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

INI.WriteInteger('Communication',

'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

INI.WriteInteger('Communication',

'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);



INI.Free;

end

else

ShowMessage('Click Save As');



end;



// 다른 이름의 INI파일로 저장

procedure TForm1.btnSaveAsClick(Sender: TObject);

begin





//두번째 질문

//여기를 코딩하고 나니 무척 쉬운 방법이 있을 것 같은데..

//오늘 아침부터 이거 해서 지금 저녁 먹고도 못했습니다...

//콤보박스 중에 하나라도 셋팅값을 설정해주지 않으면 설정해 주라는 메세지를 띄워야

//하는 것인데..웬지 어렵게 보입니다..

//이제 까지너무 쉬운걸 물어 봤나요? 전 아침부터 지금 까지 고민중입니다..

// 도와 주세요~~

if cbComport.ItemIndex = -1 then

ShowMessage('값을 설정하십시오!')

else if cbBaudrate.ItemIndex = -1 then

ShowMessage('값을 설정하십시오!')

else if cbStopBits.ItemIndex = -1 then

ShowMessage('값을 설정하십시오!')

else if cbParity.ItemIndex = -1 then

ShowMessage('값을 설정하십시오!')

else if cbBytesize.ItemIndex = -1 then

ShowMessage('값을 설정하십시오!')

else

begin

if SaveDialog1.Execute Then

begin

INI := TIniFile.Create(SaveDialog1.FileName);



INI.WriteInteger('Communication',

'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

INI.WriteInteger('Communication',

'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

INI.WriteInteger('Communication',

'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

INI.WriteInteger('Communication',

'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

INI.WriteInteger('Communication',

'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);



INI.Free;

end

end;



end;



procedure TForm1.cbComportChange(Sender: TObject);

begin

lblComport.Caption := IntToStr(cbComport.ItemIndex);

end;



procedure TForm1.cbBaudrateChange(Sender: TObject);

begin

lblBaudrate.Caption := IntToStr(cbBaudrate.ItemIndex);

end;



procedure TForm1.cbStopBitsChange(Sender: TObject);

begin

lblStopBits.Caption := IntToStr(cbStopBits.ItemIndex);

end;



procedure TForm1.cbParityChange(Sender: TObject);

begin

lblParity.Caption := IntToStr(cbParity.ItemIndex);

end;



procedure TForm1.cbBytesizeChange(Sender: TObject);

begin

lblBytesize.Caption := IntToStr(cbBytesize.ItemIndex);

end;



end.





1  COMMENTS
  • Profile
    사이비쥐 <;●●~ 2001.09.20 03:26
    처음자 wrote:

    > 며칠전부터 델파이를 접하게 뒈었습니다..

    > 몇가지 나름데로 숙제 같은 형식으로 배우고 있는데..

    >

    > 아래 코딩을 보면...(넘넘 초보라 물어보기 부끄럽습니다...^^*)

    >

    > 통신설정값을 정해주는 프로그램을 작성했습니다.

    >

    >

    > unit FMain;

    >

    > interface

    >

    > uses

    > Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

    > StdCtrls, Buttons,

    > Inifiles,

    > FileCtrl;

    >

    > type

    > TForm1 = class(TForm)

    > cbComport: TComboBox;

    > cbBaudrate: TComboBox;

    > cbStopBits: TComboBox;

    > cbParity: TComboBox;

    > cbBytesize: TComboBox;

    > Label1: TLabel;

    > Label2: TLabel;

    > Label3: TLabel;

    > Label4: TLabel;

    > Label5: TLabel;

    > btnLoad: TBitBtn;

    > btnSave: TBitBtn;

    > btnSaveAs: TBitBtn;

    > lblComport: TLabel;

    > lblBytesize: TLabel;

    > lblParity: TLabel;

    > lblStopbits: TLabel;

    > lblBaudrate: TLabel;

    > OpenDialog1: TOpenDialog;

    > SaveDialog1: TSaveDialog;

    > procedure btnSaveClick(Sender: TObject);

    > procedure cbComportChange(Sender: TObject);

    > procedure cbBaudrateChange(Sender: TObject);

    > procedure cbStopBitsChange(Sender: TObject);

    > procedure cbParityChange(Sender: TObject);

    > procedure cbBytesizeChange(Sender: TObject);

    > procedure btnLoadClick(Sender: TObject);

    > procedure btnSaveAsClick(Sender: TObject);

    > private

    > { Private declarations }

    > public

    > { Public declarations }

    > end;

    >

    > var

    > Form1: TForm1;

    >

    > implementation

    > var

    > INI : TIniFile;

    >

    > // 첫번째 질문

    > // ini파일을 만들때 마다 코딩을 해 주었습니다..

    > // 처음 한번만 정의해 주고 그것을 불러 쓰는 방법은 없는지...?

    > //

    > ///////////////////////////////////////////////////////////////

    > // Comport := 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6,

    > // COM7:7 COM8:8)';

    > // Baudrate := 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)';

    > // StopBits := 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)';

    > // Parity := 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)';

    > // Bytesize := 'Bytesize(7Bit:0, 8Bit:1)';

    > /////////////////////////////////////////////////////////////////



    찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍



    물론 이씀당!!

    procedure를 하나 만드세여..

    그 안에서 ini파일을 열고..위처럼 변수를 전역변수로 만드신후 저장시키세여..

    그럼 나중에 변수만 가져다 쓰심 댑니당...



    ---------------------------------------------------------------------------------



    > {$R *.DFM}

    >

    > // INI에서 불러서 값을 각각의 콤포 박스에 넣기

    > procedure TForm1.btnLoadClick(Sender: TObject);

    > begin

    >

    > if OpenDialog1.Execute Then

    > begin

    >

    > INI := TIniFile.Create(OpenDialog1.FileName);

    >

    > cbComport.ItemIndex := INI.ReadInteger('Communication',

    > 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', 0);

    > lblComport.Caption := IntToStr(cbComport.ItemIndex);

    >

    > cbBaudrate.ItemIndex := INI.ReadInteger('Communication',

    > 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', 0);

    > lblBaudrate.Caption := IntToStr(cbBaudrate.ItemIndex);

    >

    > cbStopBits.ItemIndex := INI.ReadInteger('Communication',

    > 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', 0);

    > lblStopBits.Caption := IntToStr(cbStopBits.ItemIndex);

    >

    > cbParity.ItemIndex := INI.ReadInteger('Communication',

    > 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', 0);

    > lblParity.Caption := IntToStr(cbParity.ItemIndex);

    >

    > cbBytesize.ItemIndex := INI.ReadInteger('Communication',

    > 'Bytesize(7Bit:0, 8Bit:1)', 0);

    > lblBytesize.Caption := IntToStr(cbBytesize.ItemIndex);

    >

    > INI.Free;

    > end;

    > end;

    >

    > // 현재 불러진 INI에파일에 설정한 값을 저장

    > procedure TForm1.btnSaveClick(Sender: TObject);

    > begin

    > if FileExists(OpenDialog1.FileName) then

    > begin

    > INI := TIniFile.Create(OpenDialog1.FileName);

    >

    > INI.WriteInteger('Communication',

    > 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);

    > end

    > else

    > if FileExists(SaveDialog1.FileName) Then

    > begin

    > INI := TIniFile.Create(SaveDialog1.FileName);

    >

    > INI.WriteInteger('Communication',

    > 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);

    >

    > INI.Free;

    > end

    > else

    > ShowMessage('Click Save As');

    >

    > end;

    >

    > // 다른 이름의 INI파일로 저장

    > procedure TForm1.btnSaveAsClick(Sender: TObject);

    > begin

    >

    >

    > //두번째 질문

    > //여기를 코딩하고 나니 무척 쉬운 방법이 있을 것 같은데..

    > //오늘 아침부터 이거 해서 지금 저녁 먹고도 못했습니다...

    > //콤보박스 중에 하나라도 셋팅값을 설정해주지 않으면 설정해 주라는 메세지를 띄워야

    > //하는 것인데..웬지 어렵게 보입니다..

    > //이제 까지너무 쉬운걸 물어 봤나요? 전 아침부터 지금 까지 고민중입니다..

    > // 도와 주세요~~





    찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍찍

    --예제소스입니다----



    procedure TForm1.Button1Click(Sender: TObject);

    var i : integer ;

    begin

    for i := 0 to ComponentCount - 1 do

    begin

    If Components[i].ClassType = TCombobox then

    begin

    If TCombobox(Components[i]).ItemIndex <= -1 then

    showmessage(Components[i].name + '의 값을 설정하십시오!') ;

    end;

    end;

    end;

    -------------------------------------------------------------------



    > if cbComport.ItemIndex = -1 then

    > ShowMessage('값을 설정하십시오!')

    > else if cbBaudrate.ItemIndex = -1 then

    > ShowMessage('값을 설정하십시오!')

    > else if cbStopBits.ItemIndex = -1 then

    > ShowMessage('값을 설정하십시오!')

    > else if cbParity.ItemIndex = -1 then

    > ShowMessage('값을 설정하십시오!')

    > else if cbBytesize.ItemIndex = -1 then

    > ShowMessage('값을 설정하십시오!')

    > else

    > begin

    > if SaveDialog1.Execute Then

    > begin

    > INI := TIniFile.Create(SaveDialog1.FileName);

    >

    > INI.WriteInteger('Communication',

    > 'Comport(COM0:0, COM1:1, COM2:2, COM3:3, COM4:4, COM5:5, COM6:6, COM7:7 COM8:8)', cbComport.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Baudrate(1200:0, 2400:1, 4800:2, 9600:3, 19200:4, 38400:5)', cbBaudrate.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'StopBits(1 Stopbit:0, 1.5 Stopbit:1, 2 Stopbit:2)', cbStopBits.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Parity(Even:0, Mark:1, No Parity:2, Odd:3)', cbParity.ItemIndex);

    > INI.WriteInteger('Communication',

    > 'Bytesize(7Bit:0, 8Bit:1)', cbBytesize.ItemIndex);

    >

    > INI.Free;

    > end

    > end;

    >

    > end;

    >

    > procedure TForm1.cbComportChange(Sender: TObject);

    > begin

    > lblComport.Caption := IntToStr(cbComport.ItemIndex);

    > end;

    >

    > procedure TForm1.cbBaudrateChange(Sender: TObject);

    > begin

    > lblBaudrate.Caption := IntToStr(cbBaudrate.ItemIndex);

    > end;

    >

    > procedure TForm1.cbStopBitsChange(Sender: TObject);

    > begin

    > lblStopBits.Caption := IntToStr(cbStopBits.ItemIndex);

    > end;

    >

    > procedure TForm1.cbParityChange(Sender: TObject);

    > begin

    > lblParity.Caption := IntToStr(cbParity.ItemIndex);

    > end;

    >

    > procedure TForm1.cbBytesizeChange(Sender: TObject);

    > begin

    > lblBytesize.Caption := IntToStr(cbBytesize.ItemIndex);

    > end;

    >

    > end.

    >

    >