首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ Builder >

CSV文件写入到TCheckListBox中出有关问题

2012-05-06 
CSV文件写入到TCheckListBox中出问题csv文档如下:C/C++ codeRP7,RESPACK-4,118.745mm,11.246mm,T

CSV文件写入到TCheckListBox中出问题
csv文档如下:

C/C++ code
"RP7","RESPACK-4","118.745mm","11.246mm","T","90.00","4.7K""RP6","RESPACK-4","140.208mm","44.704mm","T","180.00","4.7K""P10","SIP-3-2MM","146.939mm","28.829mm","T","90.00","Header 3""U5","DW024_L","87.907mm","19.317mm","T","225.00","SN74LVC4245PW""R9","0805-MINI","116.332mm","57.785mm","T","270.00","1K""P9","SIP-6-2MM","67.31mm","13.97mm","T","270.00","Header 6""P8","SIP-6-2MM","67.31mm","30.099mm","T","270.00","Header 6""P7","SIP-6-2MM","67.31mm","46.228mm","T","270.00","Header 6""P6","SIP-8-2MM","48.133mm","46.228mm","T","270.00","Header 8"

我通过TStringList将其显示到TCheckListBox里面(选择checklistbox的原因是需要对item做checked之类的操作),但是直接显示的话,数据就会挤在一行,没有可读性了。我就打算自画checklistbox,画6条竖线分割这7列数据,自画代码如下:
C/C++ code
void __fastcall TForm1::CheckListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect,     //重绘checklistbox          TOwnerDrawState State){CheckListBox1 -> Canvas -> MoveTo(50, 0);  //绘制元器件编号列,宽度50CheckListBox1 -> Canvas -> LineTo(50, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(150, 0); //绘制元器件封装类型列,宽度100CheckListBox1 -> Canvas -> LineTo(150, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(200, 0); //绘制焊盘中心X坐标列,宽度50CheckListBox1 -> Canvas -> LineTo(200, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(250, 0); //绘制焊盘中心Y坐标列,宽度50CheckListBox1 -> Canvas -> LineTo(250, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(270, 0); //绘制元件器所在层列,宽度20CheckListBox1 -> Canvas -> LineTo(270, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(320, 0); //绘制元件器放置方位角度列,宽度50CheckListBox1 -> Canvas -> LineTo(320, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);CheckListBox1 -> Canvas -> MoveTo(470, 0); //绘制元件器备注列,宽度150CheckListBox1 -> Canvas -> LineTo(470, CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight);}

但是这样一来,我打开csv文件时,数据“显示”出来了,因为可以看到每行的checkbox显示出来了,但是后面的数据没有显示出来,我想把这7列数据都显示自己我所画的列内,和StringGrid类似,只是我的只有列的那个竖线,没有行的那个水平线而已。下面是我刚刚描述的出问题的效果图:




[解决办法]
为什么要自画呢?把CSV中的数据读出来,然后填入不同的列中不就行了吗?
[解决办法]
C/C++ code
//---------------------------------------#include <vcl.h>#pragma hdrstop#include "Unit1.h"//---------------------------------------#pragma package(smart_init)#pragma resource "*.dfm"TForm1 *Form1;//---------------------------------------#define    CSVBOX_COL_COUNT    3#define CSVBOX_COL_SPACE    2typedef struct tagTCsvRecord{    String sCol[CSVBOX_COL_COUNT];    // ......}TCsvRecord;int __fastcall GetColWidth(int iIndex)// 可用其他任何方式决定列宽{    int nRet = 0;    switch(iIndex)    {        case 0:            nRet = 150;            break;        case 1:            nRet = 150;            break;        case 2:            nRet = 200;            break;        default:            nRet = 150;            break;    }    return nRet;}__fastcall TForm1::TForm1(TComponent* Owner)    : TForm(Owner){    // 初始化测试数据    TCsvRecord *pCsvRecord = NULL;    for(int i = 0; i < 20; ++ i)    {        pCsvRecord = new TCsvRecord();        for(int j = 0; j < CSVBOX_COL_COUNT; ++ j)        {            pCsvRecord->sCol[j] = "A" + IntToStr(i) + "Col" + IntToStr(j);        }        CheckListBox1->AddItem("", (TObject*)pCsvRecord);    }}__fastcall TForm1::~TForm1(){    // 释放内存    // .......}//---------------------------------------bool FDrawAutoSize = false;void __fastcall TForm1::CheckListBox1DrawItem(TWinControl *Control, int Index, TRect &Rect,          TOwnerDrawState State){    String s;    TCanvas *Canvas = CheckListBox1->Canvas;    int nItemHeight = CheckListBox1 -> ItemHeight;    int nLineHeight = CheckListBox1 -> Items -> Count * CheckListBox1 -> ItemHeight;    TCsvRecord* pRecord = (TCsvRecord*)this->CheckListBox1->Items->Objects[Index];    if(!FDrawAutoSize)    {         int nWidth     = this->CheckListBox1->Width;        int nHeight = this->CheckListBox1->Height;        int nCurX = 0;        int nCurColWidth = 0;        for(int i = 0; i < CSVBOX_COL_COUNT; ++ i)        {            nCurColWidth = GetColWidth(i);            // 具体位置自己调吧            TRect rcCol(                nCurX + CSVBOX_COL_SPACE + Rect.Left,                Rect.Top + CSVBOX_COL_SPACE,                nCurX + nCurColWidth - CSVBOX_COL_SPACE + Rect.Left,                Rect.Bottom - CSVBOX_COL_SPACE);            nCurX += nCurColWidth;            if(i != CSVBOX_COL_COUNT - 1)            // 也许只需画 列数-1 条线就够了            {                Canvas->MoveTo(nCurX, Rect.Top);                Canvas->LineTo(nCurX,  Rect.Bottom);            }            s = pRecord->sCol[i];            // 对齐方式自己调整            DWORD dwTextFmt = DT_CENTER | DT_VCENTER | DT_SINGLELINE;            //if(FWordWrap)            //{                //dwTextFmt ^= DT_SINGLELINE;                //dwTextFmt |= DT_WORDBREAK;            //}            DrawText(Canvas->Handle, s.c_str(), s.Length(), &rcCol, dwTextFmt);// | DrawTextBiDiModeFlagsReadingOnly());        }    }    else    {        // 自己算吧。。。。        // 用 TextExtent 取得文字大小调整列宽和输出位置    }}//--------------------------------------- 


[解决办法]

探讨

引用:
为什么要自画呢?把CSV中的数据读出来,然后填入不同的列中不就行了吗?

可以把Columns设置为多列,但是这样没列都会有checkbox,我只想在第一列显示checkbox,怎么设置啊?还请你帮忙看下,谢了

[解决办法]
探讨

引用:
为什么要自画呢?把CSV中的数据读出来,然后填入不同的列中不就行了吗?

可以把Columns设置为多列,但是这样没列都会有checkbox,我只想在第一列显示checkbox,怎么设置啊?还请你帮忙看下,谢了

热点排行