DrawListViewSubItemEventArgs.ItemState プロパティ
アセンブリ: System.Windows.Forms (system.windows.forms.dll 内)

Dim instance As DrawListViewSubItemEventArgs Dim value As ListViewItemStates value = instance.ItemState
親である ListViewItem の現在の状態を示す ListViewItemStates 値のビットごとの組み合わせ。

このプロパティを使用して、描画する ListViewItem.ListViewSubItem の親である ListViewItem が特定の状態にあるかどうかをチェックします。このプロパティは、親項目の基本的な状態情報のみを提供します。たとえばこのプロパティを使用して、親項目が選択されているか、オンになっているか、フォーカスを持っているかどうかを判断できます。さらに詳細な情報が必要な場合は、Item プロパティを使用して親項目を取得し、そのプロパティを直接確認します。

ListView コントロールのカスタム描画を行うアプリケーションで、ItemState プロパティを使用する方法を次のコード例に示します。この例では、ListView.DrawSubItem イベントのハンドラは、サブ項目テキスト値と、負の値を持つサブ項目のテキストおよび背景を描画します。
コード全体については、DrawListViewSubItemEventArgs の概要のリファレンス トピックを参照してください。
' Draws subitem text and applies content-based formatting. Private Sub listView1_DrawSubItem(ByVal sender As Object, _ ByVal e As DrawListViewSubItemEventArgs) _ Handles listView1.DrawSubItem Dim flags As TextFormatFlags = TextFormatFlags.Left Dim sf As New StringFormat() Try ' Store the column text alignment, letting it default ' to Left if it has not been set to Center or Right. Select Case e.Header.TextAlign Case HorizontalAlignment.Center sf.Alignment = StringAlignment.Center flags = TextFormatFlags.HorizontalCenter Case HorizontalAlignment.Right sf.Alignment = StringAlignment.Far flags = TextFormatFlags.Right End Select ' Draw the text and background for a subitem with a ' negative value. Dim subItemValue As Double If e.ColumnIndex > 0 AndAlso _ Double.TryParse(e.SubItem.Text, NumberStyles.Currency, _ NumberFormatInfo.CurrentInfo, subItemValue) AndAlso _ subItemValue < 0 Then ' Unless the item is selected, draw the standard ' background to make it stand out from the gradient. If (e.ItemState And ListViewItemStates.Selected) = 0 Then e.DrawBackground() End If ' Draw the subitem text in red to highlight it. e.Graphics.DrawString(e.SubItem.Text, _ Me.listView1.Font, Brushes.Red, e.Bounds, sf) Return End If ' Draw normal text for a subitem with a nonnegative ' or nonnumerical value. e.DrawText(flags) Finally sf.Dispose() End Try End Sub
// Draws subitem text and applies content-based formatting. private void listView1_DrawSubItem(object sender , DrawListViewSubItemEventArgs e) { TextFormatFlags flags = TextFormatFlags.Left; using (StringFormat sf = new StringFormat()) { // Store the column text alignment, letting it default // to Left if it has not been set to Center or Right. switch (e.Header.TextAlign) { case HorizontalAlignment.Center: sf.Alignment = StringAlignment.Center; flags = TextFormatFlags.HorizontalCenter; break; case HorizontalAlignment.Right: sf.Alignment = StringAlignment.Far; flags = TextFormatFlags.Right; break; } // Draw the text and background for a subitem with a // negative value. double subItemValue; if (e.ColumnIndex > 0 && Double.TryParse( e.SubItem.Text, NumberStyles.Currency, NumberFormatInfo.CurrentInfo, out subItemValue) && subItemValue < 0) { // Unless the item is selected, draw the standard // background to make it stand out from the gradient. if ((e.ItemState & ListViewItemStates.Selected) == 0) { e.DrawBackground(); } // Draw the subitem text in red to highlight it. e.Graphics.DrawString(e.SubItem.Text, listView1.Font, Brushes.Red, e.Bounds, sf); return; } // Draw normal text for a subitem with a nonnegative // or nonnumerical value. e.DrawText(flags); } }
// Draws subitem text and applies content-based formatting. private: void listView1_DrawSubItem( Object^ /*sender*/, DrawListViewSubItemEventArgs^ e ) { TextFormatFlags flags = TextFormatFlags::Left; StringFormat^ sf = gcnew StringFormat; try { // Store the column text alignment, letting it default // to Left if it has not been set to Center or Right. switch ( e->Header->TextAlign ) { case HorizontalAlignment::Center: sf->Alignment = StringAlignment::Center; flags = TextFormatFlags::HorizontalCenter; break; case HorizontalAlignment::Right: sf->Alignment = StringAlignment::Far; flags = TextFormatFlags::Right; break; } // Draw the text and background for a subitem with a // negative value. double subItemValue; if ( e->ColumnIndex > 0 && Double::TryParse( e->SubItem->Text, NumberStyles::Currency, NumberFormatInfo::CurrentInfo, subItemValue ) && subItemValue < 0 ) { // Unless the item is selected, draw the standard // background to make it stand out from the gradient. if ( (e->ItemState & ListViewItemStates::Selected) == (ListViewItemStates)0 ) { e->DrawBackground(); } // Draw the subitem text in red to highlight it. e->Graphics->DrawString( e->SubItem->Text, listView1->Font, Brushes::Red, e->Bounds, sf ); return; } // Draw normal text for a subitem with a nonnegative // or nonnumerical value. e->DrawText( flags ); } finally { if ( sf ) delete (IDisposable^)sf; } }
// Draws subitem text and applies content-based formatting. private void myListView_DrawSubItem(Object sender, DrawListViewSubItemEventArgs e) { TextFormatFlags flags = TextFormatFlags.Left; StringFormat sf = new StringFormat(); try { // Store the column text alignment, letting it default // to Left if it has not been set to Center or Right. if (e.get_Header().get_TextAlign(). Equals(HorizontalAlignment.Center)) { sf.set_Alignment(StringAlignment.Center); flags = TextFormatFlags.HorizontalCenter; } else { if (e.get_Header().get_TextAlign(). Equals(HorizontalAlignment.Right)) { sf.set_Alignment(StringAlignment.Far); flags = TextFormatFlags.Right; } } // Draw the text for a column header. if (e.get_ItemIndex() == -1) { Font myFont = new Font("Helvetica", 12, FontStyle.Bold); try { e.get_Graphics().DrawString(e.get_Item().get_Text(), myFont, Brushes.get_White(), new PointF((float)e.get_Bounds().get_X(), (float)e.get_Bounds().get_Y()), sf); } finally { myFont.Dispose(); } return; } // Draw the text and background for a subitem with a // negative value. double subItemValue = 0; if (e.get_ColumnIndex() > 0 && System.Double.TryParse( e.get_Item().get_SubItems().get_Item(e.get_ColumnIndex()). get_Text(), NumberStyles.Currency, NumberFormatInfo.get_CurrentInfo(), subItemValue) && subItemValue < 0) { // Unless the item is selected, draw the standard // background to make it stand out from the gradient. if (Convert.ToInt32(e.get_ItemState() & ListViewItemStates.Selected) == 0) { e.DrawBackground(); } // Draw the subitem text in red to highlight it. e.get_Graphics().DrawString(e.get_Item().get_SubItems(). get_Item(e.get_ColumnIndex()).get_Text(), ((ListView)sender).get_Font(), Brushes.get_Red(), RectangleF.op_Implicit(e.get_Bounds()), sf); return; } // Draw normal text for a subitem with a nonnegative // or nonnumerical value. e.DrawText(flags); } finally { sf.Dispose(); } } //myListView_DrawSubItem

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- DrawListViewSubItemEventArgs.ItemState プロパティのページへのリンク