Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Using LinearGradientBrush

Using LinearGradientBrush

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    JuanAlbertoMD
    wrote on last edited by
    #1

    I have a figure in a winform, a rectangle, and a NumericUpDown, its miniumum and maximun values are 0 and 100, i want that when i increase the NumericUpDown value the rectangle must change its color, for example when the value is 0 the rectangle color must be black and when i increase the value the rectangle color must turning into white, when the value is 100 the rectangle color must be completely white. I have seen some examples using LinearGradientBrush but i don't know exactly how to do that, somebody knows how could i do this? Regards, Alberto Martinez

    E 1 Reply Last reply
    0
    • J JuanAlbertoMD

      I have a figure in a winform, a rectangle, and a NumericUpDown, its miniumum and maximun values are 0 and 100, i want that when i increase the NumericUpDown value the rectangle must change its color, for example when the value is 0 the rectangle color must be black and when i increase the value the rectangle color must turning into white, when the value is 100 the rectangle color must be completely white. I have seen some examples using LinearGradientBrush but i don't know exactly how to do that, somebody knows how could i do this? Regards, Alberto Martinez

      E Offline
      E Offline
      Ed Poore
      wrote on last edited by
      #2

      If you want a solid colour which it looks like you do then you should use SolidBrush. In the Change / Click event of the NumericUpDown invalidate the region where you're drawing the rectangle. In the paint event handler do something like:

      using (SolidBrush brush = new SolidBrush((byte)(numericUpDown.Value * (100 / 255))...
      {
       graphics.FillRectangle(...)
      }

      basically you need to scale 0-100 up to 0-255 for each red, green and blue component of the colour. Regards Ed

      M 1 Reply Last reply
      0
      • E Ed Poore

        If you want a solid colour which it looks like you do then you should use SolidBrush. In the Change / Click event of the NumericUpDown invalidate the region where you're drawing the rectangle. In the paint event handler do something like:

        using (SolidBrush brush = new SolidBrush((byte)(numericUpDown.Value * (100 / 255))...
        {
         graphics.FillRectangle(...)
        }

        basically you need to scale 0-100 up to 0-255 for each red, green and blue component of the colour. Regards Ed

        M Offline
        M Offline
        mav northwind
        wrote on last edited by
        #3

        Sorry to interrupt, but the code above will not work. If your write (100 / 255) you'll get integer division resulting in 0, so the value of the numericUpDown won't have an effect for the color of the brush. Besides, to get values between 0 and 255 you'll have to reverse the fraction. So something like (int)(numericUpDown.Value*255/100) should work better. Regards, mav -- Black holes are the places where god divided by 0...

        E 1 Reply Last reply
        0
        • M mav northwind

          Sorry to interrupt, but the code above will not work. If your write (100 / 255) you'll get integer division resulting in 0, so the value of the numericUpDown won't have an effect for the color of the brush. Besides, to get values between 0 and 255 you'll have to reverse the fraction. So something like (int)(numericUpDown.Value*255/100) should work better. Regards, mav -- Black holes are the places where god divided by 0...

          E Offline
          E Offline
          Ed Poore
          wrote on last edited by
          #4

          I was thinking I needed more sleep and you just confirmed it :) I got my fraction upside down :omg: Ed

          J 1 Reply Last reply
          0
          • E Ed Poore

            I was thinking I needed more sleep and you just confirmed it :) I got my fraction upside down :omg: Ed

            J Offline
            J Offline
            JuanAlbertoMD
            wrote on last edited by
            #5

            Hi, Thanks both of you for your help i already could do what i needed :-D Regards, Alberto Martinez

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups