更改标签颜色 [英] changing color of label

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

问题描述

Imports System.Data.OleDb
Imports System.Data
Imports System.Threading
Imports System.Data.SqlClient

Public Class Form1

 Dim bm As BindingManagerBase
 Dim ds As New DataSet
 Dim adp As OleDb.OleDbDataAdapter
 
 Sub showrecord(ByVal pos As Integer)
  Button1.Text = ds.Tables(0).Rows(pos)("Question")
  Button2.Text = ds.Tables(0).Rows(pos)("Option1")
  Button3.Text = ds.Tables(0).Rows(pos)("Option2")
  Button4.Text = ds.Tables(0).Rows(pos)("Option3")
  Button5.Text = ds.Tables(0).Rows(pos)("Option4")
 End Sub
 
 Private Sub ButtonX1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonX1.Click
  LabelX1.BackColor = Color.White
  ds = New DataSet
  Dim dt As New DataTable
  Dim con As New OleDb.OleDbConnection("provider=sqloledb; server=MICROSOF-AE7D21\SQLEXPRESS; database=mahaveer; userid=; password=;Trusted_Connection=yes;")
  con.Open()
  adp = New OleDb.OleDbDataAdapter("SELECT TOP 10 * FROM kbc ORDER By NEWID()", con)
  adp.Fill(ds, "kbc")
  bm = Me.BindingContext(ds, "kbc")
  bm.Position = 0
  showrecord(bm.Position)
 End Sub
 
 Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  Dim result As DialogResult = MessageBox.Show("Are You Sure", "Millionaire", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
  If result = Windows.Forms.DialogResult.OK Then
   If Button2.Text = Button6.Text Then
    bm.Position += 1
    showrecord(bm.Position)
   Else
    Label2.Text = "Sorry Wrong Answer"
   End If
  End If
 End Sub
 
 Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
  Dim result As DialogResult = MessageBox.Show("Are You Sure", "Millionaire", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
  If result = Windows.Forms.DialogResult.OK Then
   If Button3.Text = Button6.Text Then
    bm.Position += 1
    showrecord(bm.Position)
   Else
    Label2.Text = "Sorry Wrong Answer"
   End If
  End If
 End Sub
 
 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
  Dim result As DialogResult = MessageBox.Show("Are You Sure", "Millionaire", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
  If result = Windows.Forms.DialogResult.OK Then
   If Button4.Text = Button6.Text Then
    bm.Position += 1
    showrecord(bm.Position)
   Else
    Label2.Text = "Sorry Wrong Answer"
   End If
  End If
 End Sub
 
 Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
  Dim result As DialogResult = MessageBox.Show("Are You Sure", "Millionaire", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
  If result = Windows.Forms.DialogResult.OK Then
   If Button5.Text = Button6.Text Then
    bm.Position += 1
    showrecord(bm.Position)
   Else
    Label2.Text = "Sorry Wrong Answer"
   End If
  End If
 End Sub

End Class



我已经用这段代码编写了一个问答游戏.我有12个标签,从LabelX1到LabelX12.
在这里,当用户单击正确的答案时,我希望其背景颜色一一白色.
我该怎么做?



i have written a quiz game in this code. i have 12 labels from LabelX1 to LabelX12.
here i want their background color white one by one when a user click on the correct answer.
how can i do that ?

推荐答案

只要有这样的东西,最好使用某种类型的集合,而不是依赖于单独地对每个控件进行寻址.

假设使用数组,则可以使用"pos"参数引用数组中的每个标签,该参数用于设置按钮的文本值.

使用您拥有的代码,我将添加一个私有成员,
whenever you have something like this, it is best to use a collection of some sort, rather than relying on addressing each control individually.

Assuming you use an array, then you can reference each label within the array using your ''pos'' parameter that is used to set up the buttons'' text values.

Using the code you have, I would add a private member,
Private _currentQuestionNuber as Integer



并将其设置为ShowRecord例程中的pos值.

然后,我将编写一个Sub来检查答案是否正确,这将采用字符串参数



and set that to the value of pos in your ShowRecord routine.

I would then write a Sub that checks if an answer is correct, which would take a string parameter

Private Sub CheckAnswer(byVal answerText as string)
Dim result As DialogResult = MessageBox.Show("Are You Sure", "Millionaire", MessageBoxButtons.OKCancel, MessageBoxIcon.Question)
 If result = Windows.Forms.DialogResult.OK Then
  If answerText = Button6.Text Then
   bm.Position += 1
   showrecord(bm.Position)
   SetLabelsColour()
  Else
   Label2.Text = "Sorry Wrong Answer"
  End If
 End If



End Sub



因此,在每个按钮单击事件中,您只需要使用按钮的文本来调用此子项-如果您愿意,甚至可以使用单个事件处理程序.

SetLabelsColour()子例程将需要在索引_currentQuestionNumber
上设置标签的背景颜色.
例如



So in each button click event you just need to call this sub with the text of the button - you could even use a single event handler if you feel up to it.

The SetLabelsColour() subroutine would need to set the background colour of the label at index _currentQuestionNumber

e.g.

LabelsArray(_CurrentQuestionNumber).BackColor = Color.Green




您的LabelArray可以定义为




Your LabelArray can be defined as

Dim LabelArray(12) As Label



并填充了



and populated with

LabelArray(0) = Label1
LabelArray(1) = Label2
LabelArray(2) = Label3
LabelArray(3) = Label4

...等



希望这足以将您指向正确的方向...

... etc



Hope that''s enough to point you in the right direction...


这篇关于更改标签颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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