PDA

View Full Version : تغییر رنگ یک آیتم مشخص از listbox



aboulfazl950
یک شنبه 11 فروردین 1392, 20:51 عصر
سلام!
کسی کد تغییر رنگ آیتم i ام از لیست باکس را می داند.

esafb52
یک شنبه 11 فروردین 1392, 21:22 عصر
در چه زمانی ؟سوال رو مبهم طرح نکنید!

aboulfazl950
دوشنبه 12 فروردین 1392, 10:13 صبح
میخواهم تمام اعداد لیست باکس که کوچکتر از 10 هستند قرمز شوند

tooraj_azizi_1035
دوشنبه 12 فروردین 1392, 14:46 عصر
باید از رویداد DrawItem کمک بگیری. در این کد Condition شرط شماست:

private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
{
// Draw the background
e.DrawBackground();

// Get the item text
string text = ((ComboBox)sender).Items[e.Index].ToString();

// Determine the forecolor based on whether or not the item is selected
Brush brush;
if (Condition)// compare date with your list.
{
brush = Brushes.Red;
}
else
{
brush = Brushes.Green;
}

// Draw the text
e.Graphics.DrawString(text, ((Control)sender).Font, brush, e.Bounds.X, e.Bounds.Y);
}

aboulfazl950
دوشنبه 12 فروردین 1392, 16:04 عصر
ممنون ولی این منظورم نبود. میخواستم ببینم میشه رنگ چند تا آیتم لیست باکس قرمز و مابقی سیاه باشد. (کد بررسی کوچک بودن از 10 در یک تایمر هر 300 میلی ثانیه چک می شود.)

Mahmoud.Afrad
دوشنبه 12 فروردین 1392, 17:03 عصر
ممنون ولی این منظورم نبود. میخواستم ببینم میشه رنگ چند تا آیتم لیست باکس قرمز و مابقی سیاه باشد. (کد بررسی کوچک بودن از 10 در یک تایمر هر 300 میلی ثانیه چک می شود.)
تایمر برای چیه؟! رویداد DrawItem کافیه. در همون رویداد در قسمت if این کارو انجام بده. کدشو آقا تورج گذاشتن.

private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
ListBox lst = sender as ListBox;
int num = Convert.ToInt32(lst.Items[e.Index]);
Brush brush;
if (num < 10)
{
brush = Brushes.Red;
}
else
{
brush = Brushes.Black;
}
e.DrawBackground();
e.Graphics.DrawString(num.ToString(), lst.Font, brush, e.Bounds);
}