In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to achieve Listbox self-drawing function of C#, I hope you have something to gain after reading this article, let's discuss it together!
Use the DrawMode property of the control to realize the self-drawing of the control, first set the DrawMode of C # Listbox to OwnerDrawVariable, and then implement the DrawItem, MeasuerItem method.
Write the following code:
Private void listBox1_DrawItem (object sender, DrawItemEventArgs e) {e.DrawBackground (); Rectangle r = new Rectangle (0,0, lbCustomDraw.Width, 100); bool selected = ((e.State & DrawItemState.Selected) = = DrawItemState.Selected); LinearGradientBrush lgb = null If (! selected) {lgb = new LinearGradientBrush (r, Color.Red, Color.Yellow, LinearGradientMode.Horizontal);} else {lgb = new LinearGradientBrush (r, Color.Cyan, Color.White, LinearGradientMode.Horizontal);} e.Graphics.FillRectangle (lgb, e.Bounds) E.Graphics.DrawRectangle (SystemPens.WindowText, e.Bounds); Rectangle R2 = e.bounds; string displayText = (string) lbCustomDraw.Items [e.Index]; SizeF size = e.Graphics.MeasureString (displayText, this.Font); r2.Y = (int) (r2.Height / 2)-(int) (size.Height / 2) + e.bounds.Y; r2.X = 2 E.Graphics.DrawString (displayText, this.Font, Brushes.Black, R2); e.DrawFocusRectangle ();} private void listBox1_MeasureItem (object sender, MeasureItemEventArgs e) {string displayText = (string) lbCustomDraw.Items [e.Index]; SizeF size = e.Graphics.MeasureString (displayText, this.Font); size.Height + = 10 E.ItemHeight = (int) size.Height;}
Final result:
After reading this article, I believe you have a certain understanding of "how to achieve Listbox self-drawing function in C#". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.