如何更改按钮区域中的颜色和文本 [英] How can I change color and text in a button region

查看:70
本文介绍了如何更改按钮区域中的颜色和文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有2个按钮的表单,名为btnGreen,btnRed是back.colors Green和Red。我的代码绘制按钮,在每个按钮上放置一个圆形GraphicsPath并创建一个区域。



单击按钮两个按钮都应该改变颜色和文本。颜色不会更改,文本只会在一个按钮上更改。以下是我的代码。我希望有一个简单的答案,因为我是一名新手程序员



问候

补丁



 私有  Sub  btnGreen_paint( ByVal 发​​件人作为 对象 ByVal  e  As  _ 
System.Windows.Forms.PaintEventArgs) Handles _
MyBase .Paint

Dim myGraphicsPath < span class =code-keyword> As System.Drawing.Drawing2D.GraphicsPath = New _
System.Drawing.Drawing2D.GraphicsPath( )

myGraphicsPath.AddEllipse(矩形(btnGreen.Width / 4 , btnGreen.Height / 4 125 125 ))
btnGreen.BackColor = Color.Chartreuse
btnGreen.Size = System.Drawing.Size( 256 256
btnGreen.Region = 区域(myGraphicsPath)
结束 Sub

私人 Sub btnRed_pa​​int( ByVal sender As 对象 ByVal e As _
System.Windows.Forms.PaintEventArgs)句柄 _
MyBase 。油漆

Dim myGraphicsPath As System.Drawing.Drawing2D.GraphicsPath = New _
System.Drawing.Drawing2D.GraphicsPath()

myGraphicsPath.AddEllipse( New Rectangle(btnRed.Width / 4 ,btnRed.Height / 4 125 125 ))
btnRed.BackColor = Color.Red
btnRed.Size = System.Drawing .Size( 256 256
btnRed.Region = 区域(myGraphicsPath)
结束 Sub

私有 Sub btnGreen_Click( ByVal 发件人作为系统。对象 ByVal e < span class =code-keyword> As System.EventArgs)句柄 btnGreen.Click
btnRed.Text = 绿色点击
btnGreen.Text = < span class =code-string>绿色点击
btnRed.BackColor = Color.Green
btnGreen.BackColor = Color.Violet
结束 Sub

私有 Sub btnRed_Click( ByVal sender As System。 Object ByVal e As System.EventArgs)哈ndles btnRed.Click
btnRed.Text = 红色点击
btnGreen.Text = 红色点击
btnRed.BackColor = Color.Yellow
btnGreen.BackColor = Color.Red
结束 Sub
结束

解决方案

< blockquote>颜色不起作用的原因是因为你覆盖了 Paint 事件中的背景颜色



所以将那些背景颜色的初始设置移动到Form loading

 私有  Sub  Form1_Load(发件人作为 对象,e 作为甚至tArgs)句柄  MyBase  .Load 
btnRed.BackColor = Color.Red
btnGreen .BackColor = Color.Green
结束 Sub



并从Paint方法中删除它们

 私有  Sub  btnGreen_paint( ByVal  sender 作为 对象 ByVal  e  As  Windows.Forms.PaintEventArgs) Handles  _ 
MyBase .Paint

Dim myGraphicsPath < span class =code-keyword> As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()

myGraphicsPath.AddEllipse( 矩形(btnGreen.Width / 4 ,btnGreen.Height / 4 125 125 ))
' btnGreen.BackColor = Color.Chartreuse
btnGreen.Size = New 大小( 256 256
btnGreen.Region = 区域(myGraphicsPath)
结束 Sub

私有 Sub btnRed_pa​​int( ByVal sender 作为 对象 ByVal e As Windows.Forms.PaintEventAr gs)句柄 _
MyBase .Paint

Dim myGraphicsPath As Drawing2D.GraphicsPath = Drawing2D.GraphicsPath ()

myGraphicsPath.AddEllipse( New Rectangle(btnRed.Width / 4 ,btnRed.Height / 4 125 125 ))
' btnRed.BackColor = Color.Red
btnRed。大小= 大小( 256 256
btnRed.Region = 区域(myGraphicsPath)
结束 Sub



我不完全确定原因,但这似乎也解决了文本问题 - 我可能会回到这个问题上来了!


I have a form with 2 buttons named btnGreen and btnRed with back.colors Green and Red. My code paints the buttons putting a circular GraphicsPath on each button and creating a region.

Clicking a button both buttons should change color and text. The color does not change and the text changes only on one button. Below is my code. I hope there is a simple answer as I am a novice programmer

regards
Patch

Private Sub btnGreen_paint(ByVal sender As Object, ByVal e As  _
    System.Windows.Forms.PaintEventArgs) Handles _
    MyBase.Paint

        Dim myGraphicsPath As System.Drawing.Drawing2D.GraphicsPath = New  _
     System.Drawing.Drawing2D.GraphicsPath()

        myGraphicsPath.AddEllipse(New Rectangle(btnGreen.Width / 4, btnGreen.Height / 4, 125, 125))
        btnGreen.BackColor = Color.Chartreuse
        btnGreen.Size = New System.Drawing.Size(256, 256)
        btnGreen.Region = New Region(myGraphicsPath)
    End Sub

    Private Sub btnRed_paint(ByVal sender As Object, ByVal e As  _
  System.Windows.Forms.PaintEventArgs) Handles _
  MyBase.Paint

        Dim myGraphicsPath As System.Drawing.Drawing2D.GraphicsPath = New  _
     System.Drawing.Drawing2D.GraphicsPath()

        myGraphicsPath.AddEllipse(New Rectangle(btnRed.Width / 4, btnRed.Height / 4, 125, 125))
        btnRed.BackColor = Color.Red
        btnRed.Size = New System.Drawing.Size(256, 256)
        btnRed.Region = New Region(myGraphicsPath)
    End Sub

    Private Sub btnGreen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGreen.Click
        btnRed.Text = "Green click"
        btnGreen.Text = "Green click"
        btnRed.BackColor = Color.Green
        btnGreen.BackColor = Color.Violet
    End Sub

    Private Sub btnRed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRed.Click
        btnRed.Text = "Red click"
        btnGreen.Text = "Red click"
        btnRed.BackColor = Color.Yellow
        btnGreen.BackColor = Color.Red
    End Sub
End Class

解决方案

The reason that the colours don't work is because you are overriding the background colour in the Paint events

So move the intial setting of those backcolor to Form load

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    btnRed.BackColor = Color.Red
    btnGreen.BackColor = Color.Green
End Sub


and remove them from the Paint methods

  Private Sub btnGreen_paint(ByVal sender As Object, ByVal e As Windows.Forms.PaintEventArgs) Handles _
  MyBase.Paint

      Dim myGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()

      myGraphicsPath.AddEllipse(New Rectangle(btnGreen.Width / 4, btnGreen.Height / 4, 125, 125))
      'btnGreen.BackColor = Color.Chartreuse
      btnGreen.Size = New Size(256, 256)
      btnGreen.Region = New Region(myGraphicsPath)
  End Sub

  Private Sub btnRed_paint(ByVal sender As Object, ByVal e As Windows.Forms.PaintEventArgs) Handles _
MyBase.Paint

      Dim myGraphicsPath As Drawing2D.GraphicsPath = New Drawing2D.GraphicsPath()

      myGraphicsPath.AddEllipse(New Rectangle(btnRed.Width / 4, btnRed.Height / 4, 125, 125))
      'btnRed.BackColor = Color.Red
      btnRed.Size = New Size(256, 256)
      btnRed.Region = New Region(myGraphicsPath)
  End Sub


I'm not entirely sure why, but that also seems to fix the text problem - I might come back on that subject!


这篇关于如何更改按钮区域中的颜色和文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆