تلوين خلية في SringGrid؟
#1
السلام عليكم

كيف يمكن عند الضغط على زر (Button) يتم تلوي خلية في الـ StringGrid 

كود :
Procedure TForm3.Button1Click(Sender: TObject); Var   begin  StringGrid1.Cells[aCol, aRow] := clLime; ;end;

لا أدري لماذا يجب أن يكون الكود في StringGrid1DrawCell و إلا لن يعمل
[-] كل من 1 user says قال شكرا ل ALG2009 على المشاركة المفيدة
  • fars74
الرد
#2
لأنه الحدث المسؤول عن رسم الخلايا في المكون, الحدث تقريبا في مناداة دائمة كل ضغطة بالفارة او انتقال من خلية الى خلية اخرى يتم استدعاء الحدث OnDrawCell, لا يمكن التلوين من خلال دالة خارجية لان فيه شغل كبير فستحتاج للتلوين كل ما تعاملت مع stringgrid.


PHP كود :
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
  
// Title cells
  if gdFixed in State then
  begin
    StringGrid1
.Canvas.Brush.Color := clActiveCaption;
    StringGrid1.Canvas.Font.Color := clBlack;
  end
  
else if gdSelected in State then // selected cells
  begin
    StringGrid1
.Canvas.Brush.Color := clWebLightBlue;
    StringGrid1.Canvas.Font.Color := clBlack;
  end
  
else
  begin   // cell normal state
    StringGrid1.Canvas.Brush.Color := clInfoBk;
    StringGrid1.Canvas.Font.Color := clBlack;
  end;

  Rect.Left := Rect.Left - 4;
  StringGrid1.Canvas.FillRect(Rect);
  StringGrid1.Canvas.TextOut(Rect.Left + 3, Rect.Top + 5, StringGrid1.Cells[ACol, ARow]);
end; 
الرد
#3
(26-05-2020, 06:26 PM)sofiane201 كتب : لأنه الحدث المسؤول عن رسم الخلايا في المكون, الحدث تقريبا في مناداة دائمة كل ضغطة بالفارة او انتقال من خلية الى خلية اخرى يتم استدعاء الحدث OnDrawCell, لا يمكن التلوين من خلال دالة خارجية لان فيه شغل كبير فستحتاج للتلوين كل ما تعاملت مع stringgrid.

جزاك الله خير
ولكن عندي زر به كود ومن ظمن الكود يتحقق من تكرار الخلايا في StringGrid فإذا تكررت الخلية يقوم بتلوينها أي الكود ظمن الـزر (Button) فكيف يمكن عمل ذلك
يعني لا يوجد حل؟
الرد
#4
يمكنك عمل أي شيء يتعلق بمظهر الخلايا في الحدث OnDrawCell
ارفق مثال حتى نرى
الرد
#5
هذا الكود يتأكد من تكرار الخلية
ولكن لا يقوم بتلوينها
كود :
procedure TForm1.Button1Click(Sender: TObject); var i,j:integer; begin for i:=1 to StringGrid1.rowcount-1; for j:=i+1 to StringGrid1.rowcount-1; if StringGrid1.Cells[2, i]=StringGrid1.Cells[2, j]; StringGrid1.Canvas.Brush.Color:= clLime; end;

ما الحل لم يقم بالتلوين
الرد
#6
السلام عليكم ورحمة الله تعالى وبركاته
هل من كود لتلوين خلية في DBGrid
حسب شرط معين
بمعنى اذا كانت قيمة الحقل " name " تساوي " x " إذًا لون الخلية يكون مثلا اصفر
الرد
#7
PHP كود :
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  aRect: TRect;
  myColumn: Integer;
begin
  myColumn 
:= 2;

  if StringGrid1.Cells[myColumn, ARow] = StringGrid1.Cells[myColumn, ARow + 1]
  then
  begin
    
// Get cell TRect
    aRect := StringGrid1.CellRect(myColumn, ARow);

    // Colors should be different than normal cells
    if gdSelected in State then
    begin
      StringGrid1
.Canvas.Brush.Color := clTeal;
      StringGrid1.Canvas.Font.Color := clWhite;
    end
    
else
    begin
      StringGrid1
.Canvas.Brush.Color := clMoneyGreen;
      StringGrid1.Canvas.Font.Color := clBlack;
    end;

    StringGrid1.Canvas.FillRect(aRect);
    StringGrid1.Canvas.TextOut(aRect.Left + 3, aRect.Top + 5,
      StringGrid1.Cells[myColumn, ARow]);
  end;
end; 
الرد
#8
بارك الله فيك

ولكن هذا الكود سيظر بمجرد تشغيل البرنامج وانا اريده بعد ضغط الزر  الذي سيقوم بالبحث على التكرار في حالة الضغط عليه
على حسب قولك لا يمكن تلوين الخلية بضغط الزر
الرد
#9
طريقة ليست جيدة لكن تفي بالغرض لانه ليس لدي الوقت, وهو الاحتفاظ بالحدث في متغير واعادة تعيينه عند الحاجة

مثال ضع على الفورم TCheckBox ثم في الحدث OnClick الخاص به, احتفظ بحدث stringgrid في متغير ثم عينه ان كان TCheckBox في حالة checked

PHP كود :
procedure TForm1.CheckBox1Click(Sender: TObject);
var
  tempEvent: TDrawCellEvent;
begin
  tempEvent 
:= StringGrid1.OnDrawCell;

  if not CheckBox1.Checked then
    StringGrid1
.OnDrawCell := nil
  
else
    StringGrid1.OnDrawCell := tempEvent;
end; 
الرد
#10
بارك الله فيكم
الكود يخص stringGrid
لكن المطلوب الكود الذي يخص DBGrid
الرد


التنقل السريع :


يقوم بقرائة الموضوع: بالاضافة الى ( 1 ) ضيف كريم