Results 1 to 6 of 6

Thread: [2005] testing colors in VB

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    24

    [2005] testing colors in VB

    Hi - newbie to VB - am using Textboxes to dispolay text and have the textboxes colored yellow, white and red depending upon their designation. I want to use the double-click event processing of the textboxes (using event handler) and take different actions depending upon the color of the textbox. I set the color as shown (this works OK):

    TextBox1.BackColor = Color.Yellow

    I attemp to test the color with the following (this does not work)

    if TextBox1.BackColor = Color.Yellow then
    ' do something'
    end if
    if TextBox1.BackColor = Color.Red then
    ' do something else'
    end if


    This creates an error and does not work and I have not been able to find the way to test this and take appropriate action. Help would be appreciated in the way to test the BackColor existing and take action depending on what this color is.

    Thanks and cheers

    Ross

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142

    Re: [2005] testing colors in VB

    use the .Equals method like this ...
    VB Code:
    1. [COLOR=Blue]Private Sub[/COLOR] Form1_Load([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] System.Object, [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] MyBase.Load
    2.         TextBox1.BackColor = Color.Yellow
    3.     [COLOR=Blue]End Sub[/COLOR]
    4.  
    5.     [COLOR=Blue]Private Sub[/COLOR] TextBox1_DoubleClick([COLOR=Blue]ByVal[/COLOR] sender [COLOR=Blue]As[/COLOR] [COLOR=Blue]Object[/COLOR], [COLOR=Blue]ByVal[/COLOR] e [COLOR=Blue]As[/COLOR] System.EventArgs) [COLOR=Blue]Handles[/COLOR] TextBox1.DoubleClick
    6.         [COLOR=Blue]If[/COLOR] TextBox1.BackColor.[B]Equals[/B](Color.Yellow) [COLOR=Blue]Then[/COLOR]
    7.             MessageBox.Show([COLOR=Purple]"yellow!"[/COLOR])
    8.         [COLOR=Blue]ElseIf[/COLOR] TextBox1.BackColor.[B]Equals[/B](Color.Red) [COLOR=Blue]Then[/COLOR]
    9.             MessageBox.Show([COLOR=Purple]"red!"[/COLOR])
    10.         [COLOR=Blue]End If[/COLOR]
    11.     End Sub
    ~
    if a post is resolved, please mark it as [Resolved]
    protected string get_Signature(){return Censored;}
    [vbcode][php] please use code tags when posting any code [/php][/vbcode]

  3. #3
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] testing colors in VB

    Quote Originally Posted by Ross007
    Hi - newbie to VB - am using Textboxes to dispolay text and have the textboxes colored yellow, white and red depending upon their designation. I want to use the double-click event processing of the textboxes (using event handler) and take different actions depending upon the color of the textbox. I set the color as shown (this works OK):

    TextBox1.BackColor = Color.Yellow

    I attemp to test the color with the following (this does not work)

    if TextBox1.BackColor = Color.Yellow then
    ' do something'
    end if
    if TextBox1.BackColor = Color.Red then
    ' do something else'
    end if


    This creates an error and does not work and I have not been able to find the way to test this and take appropriate action. Help would be appreciated in the way to test the BackColor existing and take action depending on what this color is.

    Thanks and cheers

    Ross
    Hi,

    You could do something like this also;

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If TextBox1.BackColor = Color.Yellow Then
    3.             ' do something'
    4.             MsgBox("Yellow")
    5.         End If
    6.         If TextBox1.BackColor = Color.Red Then
    7.             ' do something else'
    8.             MsgBox("Red")
    9.         End If
    10.  
    11.     End Sub

    Change the color of yout textbox with another button and see the result.

    Wkr,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  4. #4
    Member Max bayne's Avatar
    Join Date
    Jul 2006
    Location
    Egypt
    Posts
    59

    Re: [2005] testing colors in VB

    thanks man

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jan 2007
    Posts
    24

    Re: [2005] testing colors in VB

    Thanks man - cheers

  6. #6
    PowerPoster JuggaloBrotha's Avatar
    Join Date
    Sep 2005
    Location
    Lansing, MI; USA
    Posts
    4,286

    Re: [2005] testing colors in VB

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    If TextBox1.BackColor = Color.Yellow Then
    ' do something'
    MsgBox("Yellow")
    End If
    If TextBox1.BackColor = Color.Red Then
    ' do something else'
    MsgBox("Red")
    End If

    End Sub

    could be:
    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.   Select Case TextBox1.BackColor
    3.     Case Colors.Yellow
    4.       'Yellow color code
    5.     Case Colors.Red
    6.       'Red Color code
    7.   End Select
    8. End Sub
    Currently using VS 2015 Enterprise on Win10 Enterprise x64.

    CodeBank: All ThreadsColors ComboBoxFading & Gradient FormMoveItemListBox/MoveItemListViewMultilineListBoxMenuButtonToolStripCheckBoxStart with Windows

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