with the vb.net default color dialogbox

how can you handle the cancel buttons click event ?

if the cancel button is clicked, i dont want it to make any changes with the selected color. as of now if i click cancel it still edits the colors.

thanks

Dani AI

Generated

Short answer: only apply the new color after the color dialog returns an OK result, and make sure you call the dialog exactly once. If Cancel still changes your UI it means the color is being applied before the result is checked (or the dialog is being re-shown).

was correct at the high level — apply changes only on OK — but the second example in that post has a logic bug: calling the dialog a second time inside an ElseIf will reopen it. Call ShowDialog once, capture its return, and then apply or ignore the color.

A practical, safe pattern (Windows Forms) is to save the current value, show the dialog once inside a Using block, capture the DialogResult, and only assign the dialog color to your control if the result is OK. For example:

Dim original As Color = myControl.BackColor

Using cd As New ColorDialog()
    cd.Color = original
    Dim dlg As DialogResult = cd.ShowDialog()
    If dlg = DialogResult.OK Then
        myControl.BackColor = cd.Color
    End If
End Using

Troubleshooting checklist if Cancel still changes colors:

  • Search for any assignment to the control color that runs before the dialog call.
  • Look for event handlers or data-binding that react immediately to the dialog object or its Color property.
  • Put a breakpoint on every place that writes the control color to see who changes it.
  • Avoid reusing the same ColorDialog instance across threads or in unusual lifecycle code.

Note: ColorDialog is a WinForms component — it is not available in ASP.NET pages. For web apps use an HTML5 color input or a JavaScript color-picker widget.

Recommended Answers

All 2 Replies

Made changes only Ok button is clicked

'Ignore the changes, if cancel button is clicked
    If ColorDialog.ShowDialog() = DialogResult.OK Then
        'Do some changes
    End If

or

If ColorDialog.ShowDialog() = DialogResult.OK Then
        'Process Ok button event
    ElseIf  ColorDialog.ShowDialog() = DialogResult.Cancel Then
        'Process Cancel button event
    End If

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.