Results 1 to 6 of 6

Thread: partial BackColor

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    10

    partial BackColor

    Hello everyone

    So im makeing class, where i need to change BackColor for part of form..
    What i meen is, i need to change forms BackColor, but not for all form, but for, example 20px from top..
    is it posible?

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: partial BackColor

    try this:

    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.     e.Graphics.FillRectangle(New SolidBrush(SystemColors.Control), New Rectangle(0, 0, Me.ClientRectangle.Width, 20))
    3.     e.Graphics.FillRectangle(Brushes.Red, New Rectangle(0, 20, Me.ClientRectangle.Width, Me.ClientRectangle.Height))
    4. End Sub

    for a fade in effect:

    vb Code:
    1. Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    2.     Using Brush As New LinearGradientBrush(Me.ClientRectangle, SystemColors.Control, Color.Red, Drawing2D.LinearGradientMode.Vertical)
    3.         Dim b = New ColorBlend(4)
    4.         b.Colors = New Color() {SystemColors.Control, _
    5.                                 SystemColors.Control, _
    6.                                 Color.Red}
    7.         b.Positions = New Single() {0.0F, 0.1F, 1.0F}
    8.         Brush.InterpolationColors = b
    9.         e.Graphics.FillRectangle(Brush, Me.ClientRectangle)
    10.     End Using
    11. End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    10

    Re: partial BackColor

    Okey, that works, but how about this?

    I have external file (dll, or just other class added), in i make a sub or function, where i set the draw function..

    For example, class1.FillRectangle(sizeX, sizeY, startX, startY, Me)

    where sizeX, sizeY, startX, startY is the values to set, and Me, go's for form1..

    Is this posible?

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: partial BackColor

    try something like this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    4.         Class1.fillRectangle(SystemColors.Control, Color.Red, 20, Me)
    5.     End Sub
    6.  
    7. End Class

    vb Code:
    1. Public Class Class1
    2.  
    3.     Public Shared Sub fillRectangle(ByVal color1 As Color, ByVal color2 As Color, ByVal splitPoint As Integer, ByVal frm As Form)
    4.         Dim gr As Graphics = frm.CreateGraphics
    5.         gr.FillRectangle(New SolidBrush(color1), New Rectangle(0, 0, frm.ClientRectangle.Width, splitPoint))
    6.         gr.FillRectangle(New SolidBrush(color2), New Rectangle(0, splitPoint, frm.ClientRectangle.Width, frm.ClientRectangle.Height))
    7.     End Sub
    8.  
    9. End Class

  5. #5

    Thread Starter
    New Member
    Join Date
    Nov 2010
    Posts
    10

    Re: partial BackColor

    ok, so, i wrote exactly as you, but it didnt worked

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
           Class1.fillRectangle(SystemColors.Control, Color.Red, 50, Me)
        End Sub
    End Class
    Code:
    Public Class Class1
        Public Shared Sub fillRectangle(ByVal color1 As Color, ByVal color2 As Color, ByVal splitPoint As Integer, ByVal frm As Form)
            Dim gr As Graphics = frm.CreateGraphics
            gr.FillRectangle(New SolidBrush(color1), New Rectangle(0, 0, frm.ClientRectangle.Width, splitPoint))
            gr.FillRectangle(New SolidBrush(color2), New Rectangle(0, splitPoint, frm.ClientRectangle.Width, frm.ClientRectangle.Height))
        End Sub
    End Class

    I tried to put it in actual form, that needs to change the forecolor, but no luck there too..

    Code:
    Public Class Form1
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Class1.fillRectangle(SystemColors.Control, Color.Red, 50, Me)
    
            Dim gr As Graphics = Me.CreateGraphics
            gr.FillRectangle(New SolidBrush(SystemColors.Control), New Rectangle(0, 0, Me.ClientRectangle.Width, 20))
    
        End Sub
    
    End Class
    i just cant understand why..
    I searched the ms help, but didnt find there eather how to do this..

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: partial BackColor

    it's because you called it in the Form1_Load event, which runs before the _paint event which will erase it
    try:

    vb Code:
    1. Class1.fillRectangle(SystemColors.Control, Color.Red, 50, Me)
    in a button_click event

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