//listview双击事件
void __fastcall TMainFrom::ListViewDblClick(TObject *Sender)
{
line = ListView->Selected->Index;
//ShowMessage(line);
}
//按钮1单击事件
void __fastcall TMainFrom::Button1Click(TObject *Sender)
{
int line;
ListView->Items->Item[line]->SubItems->Strings[1]= FormatDateTime("yyyy-MM-dd",DateTimePicker2->DateTime)+FormatDateTime(" HH:mm:ss",DateTimePicker3->DateTime);
ListView->Items->Item[line]->SubItems->Strings[2]= Edit3->Text;
ListView->Items->Item[line]->SubItems->Strings[4]= ComboBox2->Text;
}
现在请教点击Button1后如何将line对应这行字体颜色改为绿色!
PS:善用搜索,找到有关在ListViewAdvancedCustomDrawItem事件中实现,但通过测试只能实现鼠标点击某行后后改变当前行颜色。(如下)
void __fastcall TMainFrom::ListViewAdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage,
bool &DefaultDraw)
{
if(Item->Index==line) //line 行 (指定行)
{
ListView->Canvas->Brush->Color = clWhite ;
ListView->Canvas->Font->Color = clRed; //字体为 clBlue 颜色
}
else
suiListView->Canvas->Brush->Color = clWhite;
}
请大哥指点如何简单 实现点击Button1修改listview内容的同时完成此行为绿色
4 个解决方案
#1
int lineIndex;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
lineIndex = -1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
lineIndex = 1; // 修改第二行
ListView->Items->Item[lineIndex]->SubItems->Strings[0] = "test";
ListView->Items->Item[lineIndex]->SubItems->Strings[1] = Edit1->Text;
ListView->Items->Item[lineIndex]->SubItems->Strings[2] = ComboBox1->Text;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::ListViewAdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage, bool &DefaultDraw)
{
if (Item->Index == lineIndex) // State.Contains(cdsSelected)
Sender->Canvas->Font->Color = clBlue;
else
Sender->Canvas->Font->Color = clBlack;
}
#2
void __fastcall TForm4::Button2Click(TObject *Sender)
{
line = 3;
ListView1->Items->Item[line]->SubItems->Strings[1] = "111";
ListView1->Items->Item[line]->SubItems->Strings[2] = "222";
ListView1->Items->Item[line]->SubItems->Strings[4] = "4444";
// 这3种好像都可以
// ListView1->Refresh();
// ListView1->Repaint();
ListView1->Invalidate();
}
#3
测试完成感谢各位大哥帮助,结贴!!!!!
int line,lineIndex;
//------------------------------------------------------------------------------------------------------------------------------
__fastcall TMainFrom::TMainFrom(TComponent* Owner)
{
lineIndex = -1;
}
//-----------------------------------------------------------------------------------------------------------------------------
lineIndex = line;
ListView->Items->Item[lineIndex]->SubItems->Strings[1]= FormatDateTime("yyyy-MM-dd",DateTimePicker2->DateTime)+FormatDateTime(" HH:mm:ss",DateTimePicker3->DateTime);
ListView->Items->Item[lineIndex]->SubItems->Strings[2]= Edit3->Text;
ListView->Items->Item[lineIndex]->SubItems->Strings[4]= ComboBox2->Text;
ListView->Refresh();//引用ksrsoft 代码,否则会出现Strings[1] Strings[2] Strings[4]变色其他不变的情况
#4
手动触发重绘,建议使用 Invalidate 方法。
#1
int lineIndex;
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
lineIndex = -1;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
lineIndex = 1; // 修改第二行
ListView->Items->Item[lineIndex]->SubItems->Strings[0] = "test";
ListView->Items->Item[lineIndex]->SubItems->Strings[1] = Edit1->Text;
ListView->Items->Item[lineIndex]->SubItems->Strings[2] = ComboBox1->Text;
}
// ---------------------------------------------------------------------------
void __fastcall TForm1::ListViewAdvancedCustomDrawItem(TCustomListView *Sender,
TListItem *Item, TCustomDrawState State, TCustomDrawStage Stage, bool &DefaultDraw)
{
if (Item->Index == lineIndex) // State.Contains(cdsSelected)
Sender->Canvas->Font->Color = clBlue;
else
Sender->Canvas->Font->Color = clBlack;
}
#2
void __fastcall TForm4::Button2Click(TObject *Sender)
{
line = 3;
ListView1->Items->Item[line]->SubItems->Strings[1] = "111";
ListView1->Items->Item[line]->SubItems->Strings[2] = "222";
ListView1->Items->Item[line]->SubItems->Strings[4] = "4444";
// 这3种好像都可以
// ListView1->Refresh();
// ListView1->Repaint();
ListView1->Invalidate();
}
#3
测试完成感谢各位大哥帮助,结贴!!!!!
int line,lineIndex;
//------------------------------------------------------------------------------------------------------------------------------
__fastcall TMainFrom::TMainFrom(TComponent* Owner)
{
lineIndex = -1;
}
//-----------------------------------------------------------------------------------------------------------------------------
lineIndex = line;
ListView->Items->Item[lineIndex]->SubItems->Strings[1]= FormatDateTime("yyyy-MM-dd",DateTimePicker2->DateTime)+FormatDateTime(" HH:mm:ss",DateTimePicker3->DateTime);
ListView->Items->Item[lineIndex]->SubItems->Strings[2]= Edit3->Text;
ListView->Items->Item[lineIndex]->SubItems->Strings[4]= ComboBox2->Text;
ListView->Refresh();//引用ksrsoft 代码,否则会出现Strings[1] Strings[2] Strings[4]变色其他不变的情况
#4
手动触发重绘,建议使用 Invalidate 方法。