Results 1 to 9 of 9

Thread: [RESOLVED] Custom Dialog Box - Buttons

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Resolved [RESOLVED] Custom Dialog Box - Buttons

    Hi Gurus,
    I've created through a new form a custom dialog box.
    I've added the following code within the dialog box class:

    Code:
    Private PromtMsg As String
        Sub New(ByVal promtmsg As String)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            ' set global field with the argument that was passed to the constructor
            Me.PromtMsg = promtmsg
        End Sub
    
     Private Sub Info_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' set the msg label
            Me.LblIMessaggio.Text = Me.PromtMsg
        End Sub
    
        Private Sub BtnIOk_Click(sender As Object, e As EventArgs) Handles BtnIOk.Click
            ' user choosed yes - return DialogResult.Yes
            Me.DialogResult = DialogResult.Yes
            Me.Close()
        End Sub
    
        Private Sub BtnICancella_Click(sender As Object, e As EventArgs) Handles BtnICancella.Click
            ' user choosed no - DialogResult.no
            Me.DialogResult = DialogResult.No
            Me.Close()
        End Sub
    And then I'm calling it where needed with the following code:

    Code:
    Dim customMsgbox = New Info("Message")
            If customMsgbox.ShowDialog() = DialogResult.Yes Then
    
                Me.Close()
    
            Else
    
                Me.Close()
    
            End If
    Now, I would like at runtime to change the text of the two buttons initialized with the dialog box. I've tried changing them in the following way when I call the dialog box:

    Code:
    Dim customMsgbox = New Info("Message")
    Info.BtnIOk.Text = "Yes"
            If customMsgbox.ShowDialog() = DialogResult.Yes Then
    
                Me.Close()
    
            Else
    
                Me.Close()
    
            End If
    but it retrieves me an error.

    Where should I have to change the code to allow me to change the text of the buttons and/or make one of them visible or not?

    Thanks,
    A.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Custom Dialog Box - Buttons

    Code:
        Sub New(ByVal promtmsg As String, ByVal btn1Text As String, ByVal  btn2Text As String)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            ' set global field with the argument that was passed to the constructor
            Me.LblIMessaggio.Text = promtmsg
    
            Me.BtnIOk.Text = btn1Text
            Me.BtnICancella.Text = btn2Text
    
        End Sub
    
        Private Sub BtnIOk_Click(sender As Object, e As EventArgs) Handles BtnIOk.Click
            ' user choosed yes - return DialogResult.Yes
            Me.DialogResult = DialogResult.Yes
        End Sub
    
        Private Sub BtnICancella_Click(sender As Object, e As EventArgs) Handles BtnICancella.Click
            ' user choosed no - DialogResult.no
            Me.DialogResult = DialogResult.No
        End Sub
    Code:
    Dim customMsgbox = New Info("Message", “Yes”, “Cancel”)
            If customMsgbox.ShowDialog() = DialogResult.Yes Then
    
                Me.Close()
    
            Else
    
                Me.Close()
    
            End If
    Last edited by .paul.; Sep 15th, 2019 at 08:59 PM.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: Custom Dialog Box - Buttons

    Generally speaking, for a Cancel Button you should use DialogResult.Cancel
    After setting your dialog form’s DialogResult, you don’t need to call Me.Close

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: Custom Dialog Box - Buttons

    You can get rid of those Click event handlers altogether. In the designer, assign those Buttons to the AcceptButton and CancelButton properties of the form and also set their DialogResult properties to OK and Cancel. Clicking one of the Buttons will then implicitly set the DialogResult property of the form, which will cause it to close. No code required. You only need code if you want to do something in addition or alternative to set the form's DialogResult.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: Custom Dialog Box - Buttons

    Thanks both for your suggestions - I perfectly got the suggestions from .paul and I'll make some tries with the suggestion from jmcilhinney.

    Maybe the only doubt which remains to me is if it possible to have only one button (e.g. only "Ok" button) depending on where I call the form.

    Thanks,
    A.

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: [RESOLVED] Custom Dialog Box - Buttons

    Dear jmcilhinney,
    I've tried what you suggested, but I guess I'm doing something wrong.
    So, I set the dialog result properties for both the buttons, one to Ok and the other to Cancel, then I set the form properties AcceptButton and CancelButton.

    Now, I'm calling the form at a certain point of the code and I'm getting strange results:
    - If I call the form through .showdialog() the form is displayed without the custom message I'd like to show;
    - if I call it as per below code and then I click "Ok" button, nothing happens

    Code:
    Info.Show()
                    Info.LblIMessaggio.Text = "Nome utente o password errati. Riprovare"
                    Info.BtnICancella.Visible = False
    
    
                    TxtUsername.Text = "Username"
                    TxtPassword.Text = "Password"
                    TxtUsername.ForeColor = Color.FromArgb(132, 132, 132)
                    PnlUserName.BackColor = Color.FromArgb(176, 176, 176)
                    TxtPassword.ForeColor = Color.FromArgb(132, 132, 132)
                    PnlPassword.BackColor = Color.FromArgb(176, 176, 176)
    What am I doing wrong?

    Thanks,
    A.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Sep 2018
    Posts
    90

    Re: [RESOLVED] Custom Dialog Box - Buttons

    Solved everything using .showdialog() and putting the code to change label text before...

    Sorry for the confusion on the thread.

    Thanks,
    A.

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: [RESOLVED] Custom Dialog Box - Buttons

    Try it this way...

    Code:
        Sub New(ByVal promtmsg As String, ByVal btn1Text As String, ByVal  btn2Text As String)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            ' set control values with the arguments that were passed to the constructor
    
            Me.LblIMessaggio.Text = promtmsg
    
            Me.BtnIOk.Text = btn1Text
            Me.BtnICancella.Text = btn2Text
    
            Me.BtnIOk.DialogResult = DialogResult.Yes
            Me.BtnICancella.DialogResult = DialogResult.No
    
            Me.AcceptButton = Me.BtnIOk
            Me.CancelButton = Me.BtnICancella
    
        End Sub
    Last edited by .paul.; Sep 16th, 2019 at 07:13 PM.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,480

    Re: [RESOLVED] Custom Dialog Box - Buttons

    Code:
    Public Class Info
    
        Sub New(ByVal promtmsg As String, ByVal btn1Text As String, ByVal btn2Text As String, ByVal showButtons() As Boolean)
    
            ' This call is required by the designer.
            InitializeComponent()
    
            ' Add any initialization after the InitializeComponent() call.
            ' set control values with the arguments that were passed to the constructor
    
            Me.LblIMessaggio.Text = promtmsg
    
            Me.BtnIOk.Text = btn1Text
            Me.BtnICancella.Text = btn2Text
    
            Me.BtnIOk.DialogResult = DialogResult.Yes
            Me.BtnICancella.DialogResult = DialogResult.No
    
            Me.AcceptButton = Me.BtnIOk
            Me.CancelButton = Me.BtnICancella
    
            Me.BtnIOk.Visible = showButtons(0)
            Me.BtnICancella.Visible = showButtons(1)
    
            Me.LblIMessaggio.Select()
    
        End Sub
    
    End Class
    Code:
    'show dialog with 'Yes' button and 'Cancel' button
    Dim customMsgbox as New Info("Message", "Yes", "Cancel", New Boolean() {True, True})
    If customMsgbox.ShowDialog() = DialogResult.Yes Then
        MsgBox("result = yes")
    Else
        MsgBox("result = cancelled")
    End If
    
    'show dialog with just 'OK' button
    Dim customMsgbox as New Info("Message", "OK", "", New Boolean() {True, False})
    If customMsgbox.ShowDialog() = DialogResult.Yes Then
        MsgBox("result = yes")
    Else
        MsgBox("result = cancelled")
    End If

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