私の質問は基本的に、WinForms の ComboBox に DrawItem を実装して、Text プロパティを変更しますか?
私のOwnerDrawイベントは、Textプロパティ「また」がItems []のすべてのアイテムと同じロジックに設定されることを除いて完全に機能するためです(つまり、以下のDrawItemイベントで実装されます)
コンテキストのために、リストに URL を示しますが、いくつかは非常に長いので、基本的にそれらを切り捨てて、最後にテキスト「...」を付けて、読みやすくします。クラス「DisplayUrl」の 1 つのプロパティをレンダリングするように DataSource を設定しましたが、実際の値には別の「Url」を使用します。(以下の MyUrl)
一部のコードの最後で、明示的に cmbUrl.Text = "THE FULL TEXT" を設定しました
しかし、どういうわけか、DrawItem イベントは "Text" プロパティにも影響を与えています。これは、このコードを実行した後でも、DrawItem イベントが終了すると、Text プロパティが Item[0] と同じに設定されるためです。つまり、「THE FULL T...」のように、テキストが切り取られています。
void cmbUrl_DrawItem(object sender, DrawItemEventArgs e)
{
var text = ((MyUrl)((ComboBox)sender).Items[e.Index]).DisplayUrl;
var brush = text.Contains("bla) ? Brushes.DarkGreen : Brushes.Black;
// Fill in the background
e.Graphics.FillRectangle(new SolidBrush(e.BackColor), e.Bounds);
if (e.Index < 0) return;
// Work out where every thing goes
int nX = e.Bounds.Left;
int nY = e.Bounds.Top;
const int nMarg = 2;
int nH = e.Bounds.Height - (2 * nMarg);
// Draw the Colour Gymph
var penFore = new Pen(e.ForeColor);
var rectGymph = new Rectangle(nX + nMarg, nY + nMarg, nH, nH);
e.Graphics.FillRectangle(brush, rectGymph);
e.Graphics.DrawRectangle(penFore, rectGymph);
var fullWidth = nX + nH + (2 * nMarg);
e.Graphics.DrawString(text, e.Font, brush, fullWidth, e.Bounds.Top);
}