全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1609
推到 Plurk!
推到 Facebook!

關於TIniFile的存取、讀取的小問題

尚未結案
kj68215
初階會員


發表:47
回覆:91
積分:27
註冊:2003-08-09

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-19 14:12:47 IP:61.231.xxx.xxx 未訂閱
有出現小問題地方在"SpeedButton1"以及"SpeedButton2",小弟用Brown顏色去標註了!
//---------------------------------------------------------------------------    #include 
#include "inifiles.hpp"//ini檔案必須引進的表頭檔
#include "Graphics.hpp"    #pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int oldX,oldY,sx,sy,sw;
String INIFILE;    const int x0=10;//x起點
const int y0=10;//y起點
const int iw=50;//寬度
const int ih=30;//高度
const int x00=5;//水平間隙
const int y00=5;//垂直間隙
const int max=60;//台數上限(TLabel)
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{    }
//---------------------------------------------------------------------------        void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;
TLabel *LB;
TImage *mg;    if(StrToInt(Edit1->Text)>max)
 {
  ShowMessage("請輸入1到60");
  Form1->Caption="超出範圍!!    ";
  Edit1->Clear();
 }
else
 {
  Image1->Parent=this;
  for (i=0;iControlCount-1;i  )
     {
     Panel2->Controls[0]->Free();
     }         sx=x0;
     sy=y0;         for(i=1;i<=StrToInt(Edit1->Text);i  )
       {
       TLabel *LB=new TLabel(this);
       LB->Parent=Panel2;
       LB->Color=clYellow;
       LB->Width=iw;
       LB->Height=ih;
       LB->AutoSize=false;
       LB->Caption="PC" IntToStr(i);
       LB->Name="PC" IntToStr(i);
       LB->Alignment=taCenter;
       LB->Layout=tlCenter;
       //LB->&MyMouseDown;
       //LB->&MyMouseMove;
       //LB->&MyMouseUp;
       LB->&MyClick;           if((sx iw)>Panel2->Width)
          {
          sx=x0;
          sy=sy ih y00;//sy 30 5
          }
       LB->Left=sx;
       LB->Top=sy;
       sx=sx iw x00;//sx 50 10
       }
  Image1->Parent=Panel2;
  Image1->SendToBack();
 }    }
//---------------------------------------------------------------------------
void __fastcall TForm1::MyClick(TObject *Sender)
{
TLabel *LB=new TLabel(this);    if(LB->Color==clRed)
  {
  LB->Color==clYellow;
  }
  else
  {
  LB->Color==clRed;
  }
}
//---------------------------------------------------------------------------    void __fastcall TForm1::FormCreate(TObject *Sender)
{
TIniFile *INIFILE;
INIFILE=new TIniFile(ExtractFilePath(Application->ExeName)  "sys.ini"));
delete INIFILE;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
int i;
TLabel *LB=new TLabel(this);    TIniFile *n=new TIniFile(INIFILE);
n->WriteInteger("sys","count",Panel2->ControlCount);
for (i=0;iControlCount-1;i  )
  {
  if(Panel2->Controls[i]==LB)
    {        LB=TLabel(Panel2->Controls[i]);//[C   Error] Unit1.cpp(184): E2034
    n->WriteString(LB->Name,"Caption",LB->Caption);
    n->WriteInteger(LB->Name,"Left",LB->Left);
    n->WriteInteger(LB->Name,"Top",LB->Top);
    n->WriteInteger(LB->Name,"Width",LB->Width);
    n->WriteInteger(LB->Name,"Height",LB->Height);        }
  }
  n->Free();
  delete n;
}
//---------------------------------------------------------------------------    void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
int i,icount;
TLabel *LB;    TIniFile *n=new TIniFile(ChangeFileExt(Application->ExeName, ".ini"));
icount=n->ReadInteger("sys","count",0);
Image1->Parent=this;
for (i=0;iControlCount-1;i  )
  {
  Panel2->Controls[0]->Free();
  }
for(i=1,iParent=Panel2;
  LB->Color=clYellow;
  LB->AutoSize=false;
  LB>Alignment=taCenter;
  LB->Layout=tlCenter;
  LB->&MyClick;      LB->Caption=n->ReadString("PC" IntToStr(i),"Caption","");
  LB->Name=LB->Caption;
  LB->Width=n->ReadInteger("PC" IntToStr(i),"Width",0);
  LB->Height=n->ReadInteger("PC" IntToStr(i),"Height",0);
  LB->Top=n->ReadInteger("PC" IntToStr(i),"Top",0);
  LB->Left=n->ReadInteger("PC" IntToStr(i),"Left",0);
  }
  Image1->Parent=Panel2;
  Image1->SendToBack();
  n->Free();
  delete n;
}    //---------------------------------------------------------------------------
在Compile時,發生了兩個小錯誤↓ [C Error] Unit1.cpp(184): E2034 Cannot convert 'TLabel' to 'TLabel *' [C Error] Unit1.cpp(210): E2378 For statement missing ; 也就是說,當Save若錯誤,icount也沒辦法讀取到數值,所以關鍵應該是在於184行code. 想了很久,不知道怎麼改,老是改不出來! 可否麻煩懂得大大幫小弟看一下哪裡錯了... thx ----------------------- 請多多指教啦!!^^
------
-----------------------
請多多指教啦!!^^
CuteBabyBoy
初階會員


發表:18
回覆:97
積分:48
註冊:2003-07-11

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-19 16:44:27 IP:218.167.xxx.xxx 未訂閱
我是不太清楚
LB=TLabel(Panel2->Controls[i])  
有什麼含意,但我猜即使此行 Mark 起來也可以跑吧.... 而你下一個問題是 for 迴圈要用 分號間隔 而不是用 ',' for ( @@ ; @@ ; @@ ) Baby
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-19 17:41:04 IP:218.160.xxx.xxx 未訂閱
 
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
int i;
//TLabel *LB=new TLabel(this);
TLabel *LB;    TIniFile *n=new TIniFile(INIFILE);
n->WriteInteger("sys","count",Panel2->ControlCount);
for (i=0;iControlCount-1;i  )
  {
//  if(Panel2->Controls[i]==LB)
    if(AnsiString(Panel2->Controls[i]->ClassName())=="TLabel")        {    //    LB=TLabel(Panel2->Controls[i]);//[C   Error] Unit1.cpp(184): E2034
    LB=(TLabel*)Panel2->Controls[i];        n->WriteString(LB->Name,"Caption",LB->Caption);
    n->WriteInteger(LB->Name,"Left",LB->Left);
    n->WriteInteger(LB->Name,"Top",LB->Top);
    n->WriteInteger(LB->Name,"Width",LB->Width);
    n->WriteInteger(LB->Name,"Height",LB->Height);        }
  }
  n->Free();
  delete n;
}    
發表人 - brook 於 2004/01/19 17:42:07
GaryKao99
中階會員


發表:46
回覆:102
積分:54
註冊:2002-08-23

發送簡訊給我
#4 引用回覆 回覆 發表時間:2004-01-19 17:45:17 IP:211.23.xxx.xxx 未訂閱
[C++ Error] Unit1.cpp(184): E2034 Cannot convert 'TLabel' to 'TLabel *'    LB=TLabel(Panel2->Controls[i]);    此行你給LB的是一個TLabel並不是一個TLabel* 請試試 LB=(TLabel*)(Panel2->Controls[i]); ~~~~~~~~~~~~~~~~~~~~~~ 我什麼都會, 就兩樣不會 就是 這個也不會, 那個也不會
kj68215
初階會員


發表:47
回覆:91
積分:27
註冊:2003-08-09

發送簡訊給我
#5 引用回覆 回覆 發表時間:2004-01-20 04:31:17 IP:61.231.xxx.xxx 未訂閱
compile是沒問題了! 但是,Save/Load卻有些問題... 也就是Save的部分,只紀錄到count的數值,其餘的Caption、Left、Top、Width、Height都沒紀錄到! 這是怎麼一回事呢? ----------------------- 請多多指教啦!!^^
------
-----------------------
請多多指教啦!!^^
CuteBabyBoy
初階會員


發表:18
回覆:97
積分:48
註冊:2003-07-11

發送簡訊給我
#6 引用回覆 回覆 發表時間:2004-01-20 09:55:25 IP:218.167.xxx.xxx 未訂閱
以 brook 發言來看     
for (i=0;iControlCount-1;i  )
  {
//  if(Panel2->Controls[i]==LB)

    if(AnsiString(Panel2->Controls[i]->ClassName())=="TLabel")     
似乎你的程式碼沒進到 if 的部份,您的 Class Name 會不會是 LB,但不管如何,寫的真的蠻....特別的^^ Baby
brook
資深會員


發表:57
回覆:323
積分:371
註冊:2002-07-12

發送簡訊給我
#7 引用回覆 回覆 發表時間:2004-01-20 16:52:43 IP:218.160.xxx.xxx 未訂閱
引言: compile是沒問題了! 但是,Save/Load卻有些問題... 也就是Save的部分,只紀錄到count的數值,其餘的Caption、Left、Top、Width、Height都沒紀錄到! 這是怎麼一回事呢?
有可能是你的Label不是拉在Panel2上,而是在拉在Form上或是Panel1上. 發表人 - brook 於 2004/01/20 16:54:59
hahalin
版主


發表:295
回覆:1698
積分:823
註冊:2002-04-14

發送簡訊給我
#8 引用回覆 回覆 發表時間:2004-01-21 16:38:18 IP:211.76.xxx.xxx 未訂閱
我收到簡訊了 是這樣的 我只會delphi c 很早就還給老師了 所以 看了你的程式碼 似乎是用我發表的網咖管理介面下去改寫的 會有錯誤訊息的那兩行 應該是lb變數宣告為tlabel型態 如果有指定instance給他 就不用create 參考看看 說錯了也還請其他大大用力指正 看來雙修delphi,bcb是新的一年要努力的方向
kj68215
初階會員


發表:47
回覆:91
積分:27
註冊:2003-08-09

發送簡訊給我
#9 引用回覆 回覆 發表時間:2004-01-21 17:13:42 IP:61.231.xxx.xxx 未訂閱
首先,謝謝CuteBabyBoy與brook的熱心指點!真的由衷的感謝! > > 另外,金剛組合版的原作者
------
-----------------------
請多多指教啦!!^^
kj68215
初階會員


發表:47
回覆:91
積分:27
註冊:2003-08-09

發送簡訊給我
#10 引用回覆 回覆 發表時間:2004-01-23 04:51:21 IP:61.231.xxx.xxx 未訂閱
經過這幾天的嘗試,小弟終於把寫完成的! 小弟就把完整的code給貼出來. p.s:紅色的地方是經過修改的!
<.h的部分>
__published:        // IDE-managed Components
        void __fastcall MyClick(TObject *Sender);    <.cpp的部分>
//---------------------------------------------------------------------------    #include 
#include "inifiles.hpp"//ini檔案必須引進的表頭檔
#include "graphics.hpp"    #pragma hdrstop    #include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
int oldX,oldY,sx,sy,sw;//oldX與oldY給於是OnMouseDown、OnMouseUp、OnMove所使用的
const int x0=10;//x起點
const int y0=10;//y起點
const int iw=50;//寬度
const int ih=30;//高度
const int x00=5;//水平間隙
const int y00=5;//垂直間隙
const int max=60;//電腦台數上限(TLabel)
const int min=1;//電腦台數下限(TLabel)
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
        : TForm(Owner)
{    }
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
int i;    if(StrToInt(Edit1->Text)>max||StrToInt(Edit1->Text)請輸入1到60");
  Edit1->Clear();
 }
else
 {
  Image1->Parent=this;
  for (i=0;iControlCount-1;i  )
     {
     Panel2->Controls[0]->Free();
     }         sx=x0;
     sy=y0;         for(i=1;i<=StrToInt(Edit1->Text);i  )
       {
       TLabel *LB=new TLabel(this);
       LB->Parent=Panel2;
       LB->Color=clYellow;
       LB->Width=iw;
       LB->Height=ih;
       LB->AutoSize=false;
       LB->Caption="PC" IntToStr(i);
       LB->Name="PC" IntToStr(i);
       LB->Alignment=taCenter;
       LB->Layout=tlCenter;
       LB->&MyClick;           if((sx iw)>Panel2->Width)
          {
          sx=x0;
          sy=sy ih y00;//sy 30 5
          }
       LB->Left=sx;
       LB->Top=sy;
       sx=sx iw x00;//sx 50 10
       }      Image1->Parent=Panel2;
  Image1->SendToBack();
  Edit1->Clear();
 }
//↓當按下Button1的同時也做了Save動作.
TIniFile *MyINI;
MyINI=new TIniFile(ExtractFilePath(Application->ExeName) "sys.ini");
MyINI->WriteInteger("sys","count",Panel2->ControlCount);
for (i=0;i<=Panel2->ControlCount-1;i  )
  {
  if(AnsiString(Panel2->Controls[i]->ClassName())=="TLabel")
    {
    TLabel *LB=new TLabel(this);
    LB=(TLabel*)Panel2->Controls[i];
    MyINI->WriteString(LB->Name,"Caption",LB->Caption);
    MyINI->WriteInteger(LB->Name,"Left",LB->Left);
    MyINI->WriteInteger(LB->Name,"Top",LB->Top);
    MyINI->WriteInteger(LB->Name,"Width",LB->Width);
    MyINI->WriteInteger(LB->Name,"Height",LB->Height);
    }
  }
delete MyINI;
}    //---------------------------------------------------------------------------
void __fastcall TForm1::MyClick(TObject *Sender)
{
TLabel *LB=new TLabel(this);
LB=((TLabel*)Sender);//之前沒寫入這一行,所以造成OnClick功能無法正常顯示(無作用)
if(LB->Color==clRed)
  {
  LB->Color=clYellow;
  }
else
  {
  LB->Color=clRed;
  }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton2Click(TObject *Sender)
{
int i;
TLabel *LB=new TLabel(this);
TIniFile *MyINI;
MyINI=new TIniFile(ExtractFilePath(Application->ExeName) "sys.ini");    MyINI->WriteInteger("sys","count",Panel2->ControlCount);
for (i=0;i<=Panel2->ControlCount-1;i  )//沒有加上等號,會少紀錄到一比"PC" IntToStr(i),所以才會Load會無法讀取(無作用).
  {
  if(AnsiString(Panel2->Controls[i]->ClassName())=="TLabel")
    {
    LB=(TLabel*)Panel2->Controls[i];
    MyINI->WriteString(LB->Name,"Caption",LB->Caption);
    MyINI->WriteInteger(LB->Name,"Left",LB->Left);
    MyINI->WriteInteger(LB->Name,"Top",LB->Top);
    MyINI->WriteInteger(LB->Name,"Width",LB->Width);
    MyINI->WriteInteger(LB->Name,"Height",LB->Height);
    }
  }
delete MyINI;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton1Click(TObject *Sender)
{
int i,icount;    TIniFile *MyINI;
MyINI=new TIniFile(ExtractFilePath(Application->ExeName) "sys.ini");    for (i=0;iControlCount-1;i  )
  {
  Panel2->Controls[0]->Free();
  }
Image1->Parent=this;
icount=MyINI->ReadInteger("sys","count",0);
for(i=1;iParent=Panel2;
  LB->Color=clYellow;
  LB->AutoSize=false;
  LB->Alignment=taCenter;
  LB->Layout=tlCenter;
  LB->&MyClick;      LB->Caption=MyINI->ReadString(("PC" IntToStr(i)),"Caption","");
  LB->Name=LB->Caption;
  LB->Left=MyINI->ReadInteger(("PC" IntToStr(i)),"Left",0);
  LB->Top=MyINI->ReadInteger(("PC" IntToStr(i)),"Top",0);
  LB->Width=MyINI->ReadInteger(("PC" IntToStr(i)),"Width",0);
  LB->Height=MyINI->ReadInteger(("PC" IntToStr(i)),"Height",0);      }
Image1->Parent=Panel2;
Image1->SendToBack();
delete MyINI;;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SpeedButton3Click(TObject *Sender)
{
Close();
}
//---------------------------------------------------------------------------
----------------------- 請多多指教啦!!^^ 發表人 - kj68215 於 2004/01/23 05:00:27 發表人 - kj68215 於 2004/01/23 05:01:42
------
-----------------------
請多多指教啦!!^^
kj68215
初階會員


發表:47
回覆:91
積分:27
註冊:2003-08-09

發送簡訊給我
#11 引用回覆 回覆 發表時間:2004-01-23 05:14:03 IP:61.231.xxx.xxx 未訂閱
由於功能還沒寫完整. 目前只完成1半,所以暫時先不發表成品. 等小弟寫完後,會發表給各位大大們看! 發表人 -
------
-----------------------
請多多指教啦!!^^
系統時間:2024-05-21 0:25:17
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!