Results 1 to 11 of 11

Thread: ColorDialog1 not working with MenuStrip execute

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    ColorDialog1 not working with MenuStrip execute

    I am teaching myself VS from "Programming in Visual Basic 2010" by Julia C. Bradley & Anita C. Millspaugh. The textbook has a sample project that I coded to match the text. In the form MenuStrip execute, FontDialog1 and ColorDialog1 are used on the TextBoxes inside a GroupBox. When the color code is performed no color change occurs when executed. The Font change works as expected. I have successfully used the ColorDialog1 code before. I have confirmed the code is executed with a Debug break. Is there something in the properties for the GroupBox or TextBoxes that I have overlooked? TIA JP

    Code:
      Public Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object,
                                    ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
    
            With ColorDialog1
                ' Change the color of the total labels.
                .Color = SubTotalTextBox.ForeColor
                .ShowDialog()
                SubTotalTextBox.ForeColor = .Color
                TaxTextBox.ForeColor = .Color
                TotalTextBox.ForeColor = .Color
            End With
    
        End Sub
    Last edited by dday9; Feb 10th, 2017 at 10:14 AM. Reason: Added Code Tags

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: ColorDialog1 not working with MenuStrip execute

    I personally create a new instance of the ColorDialog and also use a conditional statement to check for the DialogResult returned from the ColorDialog first. This would be an example:
    Code:
    Using colorDialog1 As ColorDialog = New ColorDialog
        With colorDialog1
            'Set any properties of the dialog first
            .Color = SubTotalTextBox.ForeColor
    
            'Display the dialog
            If colorDialog1.ShowDialog = DialogResult.Ok Then
                'If the user clicked 'OK' then change the text color of the TextBox controls
                SubTotalTextBox.ForeColor = .Color
                TaxTextBox.ForeColor = .Color
                TotalTextBox.ForeColor = .Color
            End If
        End With
    End Using
    Also, for future reference, you can keep the formatting of your code in VBForums by wrapping your code in [CODE][/CODE] tags. I've editted your post to include them too.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: ColorDialog1 not working with MenuStrip execute

    Thanks for your response. I copied your code into my project, placed a debug break on line " TaxTextBox.ForeColor = .Color", the logic was executed but no color was changed! This color changing logic works intermittently with new project in studies. Really wonder how to trace this problem!

    Early in my studies after downloading/installing VB Studio community 2015 on my Win !0 (64 bit system) I experience compatibility problems after downloading VB PowerPacks. Is it possible some lingering incompatible software code is still in my computer? TIA JP

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: ColorDialog1 not working with MenuStrip execute

    You shouldn't need to, but try calling the Refresh method of the control after you set the ForeColor.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: ColorDialog1 not working with MenuStrip execute

    Thanks again for the reply. I inserted the Refresh() to no avail. I then deleted the tool ColorDialog1, saved project, reinserted tool ColorDialog1, saved project and tested - no color change. Would a deinstall of VB Studio Community 2015 and a reinstall be my next move (concerned about old VB PowerPacks contamination)? TiA JP

    (i.e.: Early in my studies after downloading/installing VB Studio community 2015 on my Win 10 (64 bit system) I experience compatibility problems after downloading VB PowerPacks. Is it possible some lingering incompatible software code is still in my computer? Geek Squad removed suspected problems like Win8 references.)

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: ColorDialog1 not working with MenuStrip execute

    To be completely honest with you, I'm not sure. I'm sure that uninstalling and then reinstalling couldn't hurt, but I don't know if that would work.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: ColorDialog1 not working with MenuStrip execute

    Does it matter if you code the ColorDialog1 procedure before you insert the tool ColorDialog1 at the bottom of the form? TIA JP

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: ColorDialog1 not working with MenuStrip execute

    The example that I provided should not require that you include the component at all.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: ColorDialog1 not working with MenuStrip execute

    Thanks for your replies! I uninstalled VS Community 2015, rebooted system and then reinstalled it. ColorDialog1 still does not work! I am wondering if it is a problem in my computer or a coding (?) issue. At a loss what to consider next! TIA JP

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,371

    Re: ColorDialog1 not working with MenuStrip execute

    Start a new project, do not add any controls to your form, and copy/paste the following code to see if it works:
    Code:
    Option Strict On
    Option Explicit On
    Public Class Form1
        'Controls
        Private SubTotalTextBox, TaxTextBox, TotalTextBox As TextBox
        Private ChangeColorButton As Button
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
            'Create new instances of the controls
            SubTotalTextBox = New TextBox With {.Location = New Point(5, 5), .Width = 100}
            TaxTextBox = New TextBox With {.Location = New Point(SubTotalTextBox.Left, SubTotalTextBox.Bottom + 5), .Width = SubTotalTextBox.Width}
            TotalTextBox = New TextBox With {.Location = New Point(SubTotalTextBox.Left, TaxTextBox.Bottom + 5), .Width = SubTotalTextBox.Width}
            ChangeColorButton = New Button With {.AutoSize = True, .Location = New Point(SubTotalTextBox.Left, TotalTextBox.Bottom + 5), .Text = "Color"}
    
            'Add the controls to the Form
            Me.Controls.AddRange({SubTotalTextBox, TaxTextBox, TotalTextBox, ChangeColorButton})
    
            'Generate the click event for the Button
            AddHandler ChangeColorButton.Click, AddressOf ChangeColorButton_Click
        End Sub
    
        Private Sub ChangeColorButton_Click(ByVal sender As Object, ByVal e As EventArgs)
            Using colorDialog1 As ColorDialog = New ColorDialog
                With colorDialog1
                    'Set any properties of the dialog first
                    .Color = SubTotalTextBox.ForeColor
    
                    'Display the dialog
                    If colorDialog1.ShowDialog = DialogResult.Ok Then
                        'If the user clicked 'OK' then change the text color of the TextBox controls
                        SubTotalTextBox.ForeColor = .Color
                        TaxTextBox.ForeColor = .Color
                        TotalTextBox.ForeColor = .Color
                    End If
                End With
            End Using
        End Sub
    
    End Class
    If this doesn't work, then I don't know what to tell you.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Nov 2016
    Posts
    125

    Re: ColorDialog1 not working with MenuStrip execute

    Thanks for your reply, I will study and test the code you provided.

    I was looking for differences in the Form1.Designer.vb code for a working ColorDialog project versus one that does not work and I noted quite a few missing (?) lines of code. Is this a valid place for examining? See code below:
    ColorDialog works in Feedback5.1 Practice

    Code:
     
    ‘           Notice both labels and textbox were used.
    
    '       Change the color of the total labels.
            With ColorDialog1
                .ShowDialog()
                FarmNameLabel.ForeColor = .Color
                NumberofFieldsLabel.ForeColor = .Color
                WidthLabel.ForeColor = .Color
                LengthLabel.ForeColor = .Color
                LengthTextBox.ForeColor = .Color
            End With
    
    ‘ Form1.Designer.vb   These Color Dialog references exist:
    
    1        <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
    2        Partial Class Form1
    
    
    24       Private Sub InitializeComponent()
    
    34       Me.ColorToolStripMenuItem = New System.Windows.Forms.ToolStripMenuItem()
    
    36       Me.ColorDialog1 = New System.Windows.Forms.ColorDialog()
    
    118      'ColorToolStripMenuItem
    
    120      Me.ColorToolStripMenuItem.Name = "ColorToolStripMenuItem"
    121      Me.ColorToolStripMenuItem.Size = New System.Drawing.Size(152, 22)
    122      Me.ColorToolStripMenuItem.Text = "C&olor"
    253      End Sub
    
    261      Friend WithEvents ColorToolStripMenuItem As ToolStripMenuItem
    
    264      Friend WithEvents ColorDialog1 As ColorDialog
    End Class]
    Malfunctioning Project Case Study CH 5Video Bonanza

    Code:
     
    57        Me.ColorDialog1 = New System.Windows.Forms.ColorDialog()
    
    291        'ColorToolStripMenuItem
    292        '
    293        Me.ColorToolStripMenuItem.Name = "ColorToolStripMenuItem"
    294        Me.ColorToolStripMenuItem.Size = New System.Drawing.Size(173, 22)
    295        Me.ColorToolStripMenuItem.Text = "&Color"
    
    374        Friend WithEvents ColorToolStripMenuItem As ToolStripMenuItem
    379        Friend WithEvents ColorDialog1 As ColorDialog
    
    381                End Class
    Last edited by justphilip; Feb 15th, 2017 at 12:55 PM.

Tags for this Thread

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