Results 1 to 8 of 8

Thread: Extract graphics object of listview control

  1. #1

    Thread Starter
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159

    Extract graphics object of listview control

    Hi,

    I am trying to extract the graphics object of a listview control. It is quite a simple project - I am populating a listView control with some data from a database and I would like to show some custom text in the center of the "whitespace" of the listview that says "please wait" or some other helpful comment to my user!!! I have tried the following

    Code:
    Graphics g = Graphics.FromHwnd(listView1.Handle);
    g.DrawText(.... code in here ... );
    g.Dispose();
    The above code does not error but it doesnt draw on the object... please can somebody help?! I saw somewhere that you can use a "SetStyles" method to allow on paint events but I can't seem to find enough to help me out - can anybody please help?

    Kind regards,
    DJ

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Extract graphics object of listview control

    Sounds like you are complicating matters unnecessarily. Why not just place a Label with the appropriate BackColor over the ListView for the short period while the data is loading?

  3. #3

    Thread Starter
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159

    Re: Extract graphics object of listview control

    Quote Originally Posted by jmcilhinney
    Sounds like you are complicating matters unnecessarily. Why not just place a Label with the appropriate BackColor over the ListView for the short period while the data is loading?
    How can you call three lines of code complicated?! It is probably less than creating a label. Anyway..... I do see your point and this solution would possibly work however I know there is a way to do this and would like to know the correct way to achieve the desired result.

    Thanks,
    DJ

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Extract graphics object of listview control

    Quote Originally Posted by DJ_Catboy
    How can you call three lines of code complicated?! It is probably less than creating a label. Anyway..... I do see your point and this solution would possibly work however I know there is a way to do this and would like to know the correct way to achieve the desired result.

    Thanks,
    DJ
    Quite right, you are. The question I was really asking was "why look for a more obscure solution when there is an obvious one right in front of you". Who's to say which is the "correct" way? I guess learning this now does arm you for the future when you may need that method, though. I apologise in advance to the GDI+ aficionados for calling it obscure.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Extract graphics object of listview control

    Unless I'm mistaken, you will need code similar to this:
    VB Code:
    1. Dim g As Drawing.Graphics = Drawing.Graphics.FromHwnd(Me.ListView1.Handle)
    2.         Dim f As Font
    3.         Dim b As Brush
    4.         Dim x As Integer
    5.         Dim y As Integer
    6.  
    7.         g.DrawString("Please wait...", f, b, x, y)
    8.         g.Dispose()
    You will have to create the font and brush yourself. The (slightly) complicated bit is working out x and y to get the text centred. You will probably have to use Graphics.MeasureString to determine the size of the text drawn with your chosen font.

  6. #6

    Thread Starter
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159

    Re: Extract graphics object of listview control

    Hi,

    Yeah that is what I have been experimenting with - however it would appear that the handle is not giving me the actual "whitespace" where the ListViewItems go - it is giving me the overall container - therefore when I draw on that it is being printed under another object...?! After discovering this your idea of using the label sounds better and better.....! Thanks for the help....

    DJ

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Extract graphics object of listview control

    If you still want to go your original route, you may (I haven't tested) find that Me.ListView1.CreateGraphics() gives you what you want.

  8. #8

    Thread Starter
    Addicted Member DJ_Catboy's Avatar
    Join Date
    Jan 2003
    Location
    Suffolk, UK
    Posts
    159

    Re: Extract graphics object of listview control

    Hi,

    I also found this method but I think it returns the same as the previous example - i.e. you get the graphics method of the underlying object and not the stuff on top of it. I have actually got around this problem in two ways: firstly i have added a label over the top of the control (it does look nice but...) and secondly I have made my "long operation" take a fraction of the time so now updating the user is not as important.

    However I am a purist and would really like to know if it is possible from somebody?!

    Many thanks for all of the help,
    DJ

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width