none
Form Regeneration RRS feed

  • Question

  • I use the following code to populate a set of radio buttons and checkboxes in a load event.  I thought that I understood what was going on. The code works as intended providing a form with a set of radio boxes and check boxes, as well as other control variables, that are set to default values.

    Public Class frmPizza
        Dim MyPizza As Pizza
        Dim Topping(6) As CheckBox
        Dim Loading As Boolean = True
        'EVENTS
        'Form Load Event
        Private Sub frmPizza_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            MyPizza = New Pizza()
            'Define an array of topping check boxes
            Topping(1) = chkCheese
            Topping(2) = chkMushrooms
            Topping(3) = chkOlives
            Topping(4) = chkOnions
            Topping(5) = chkPeppers
            Topping(6) = chkTomatoes
            Loading = False
        End Sub
    End Class


    Public Class Pizza
        Public PizzaSize As String
        Public PizzaCrust As String
        Public PizzaWhere As String
        Public PizzaTopping(6) As String
    
        Public Sub New()
            Me.PizzaCrust = "Small"
            Me.PizzaCrust = "Thin Crust"
            Me.PizzaWhere = "Eat In"
            Me.PizzaTopping(1) = ""
            Me.PizzaTopping(2) = ""
            Me.PizzaTopping(3) = ""
            Me.PizzaTopping(4) = ""
            Me.PizzaTopping(5) = ""
            Me.PizzaTopping(6) = ""
        End Sub
    End Class

    So now I want to add a button that would reset the form to it's default values after the user had changed the checkboxes, radio buttons, etc.

    I had thought that by adding a button tool and the code below that this might get the job done.  It did not.  The screen display appears to not regenerate itself.  Obviously, I do not understand what is going on or what is required to remediate this.

        Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
            Me.frmPizza_Paint()
        End Sub
    
    
        Private Sub frmPizza_Paint()
            MyPizza = New Pizza()
            'Define an array of topping check boxes
            Topping(1) = chkCheese
            Topping(2) = chkMushrooms
            Topping(3) = chkOlives
            Topping(4) = chkOnions
            Topping(5) = chkPeppers
            Topping(6) = chkTomatoes
        End Sub


    gwboolean

    Saturday, January 26, 2019 6:25 PM

Answers

  • In regards to "regenerating", there is no such thing without re-creating the form, what I showed was resetting a set of RadioButton controls, same needs to happen for all other controls or using DataBinding to do the reset but that is really overkill for this. I would complete an order then re-create the form thus reset per-say pretty much how a cash register works.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by gwboolean Monday, January 28, 2019 4:40 PM
    Monday, January 28, 2019 2:47 AM
    Moderator

All replies

  • I would take a different approach which is a basic example.

    Public Class Form1
    
        Private Sub newOrderButton_Click(sender As Object, e As EventArgs) Handles newOrderButton.Click
    
            CrustGroupBGox.Controls.OfType(Of RadioButton).ToList()
    
            Dim crustRadioButtons = CrustGroupBGox.Controls.OfType(Of RadioButton).ToList()
            For index As Integer = 0 To crustRadioButtons.Count - 1
                crustRadioButtons(index).Checked = False
            Next
    
            Dim pizza As New Pizza
            PizzaBindingSource.DataSource = pizza
    
        End Sub
    
        Private Sub smallCrustRadioButton_CheckedChanged(sender As Object, e As EventArgs) _
            Handles smallCrustRadioButton.CheckedChanged, thinCrustRadioButton.CheckedChanged
    
            If PizzaBindingSource.DataSource IsNot Nothing Then
                Dim rbSelected = CrustGroupBGox.Controls.OfType(Of RadioButton).
                        FirstOrDefault(Function(rb) rb.Checked)
    
                If rbSelected IsNot Nothing Then
                    CType(PizzaBindingSource.Current, Pizza).PizzaCrust = CStr(rbSelected.Tag)
                End If
    
            End If
    
        End Sub
        Private Sub Form1_Shown(sender As Object, e As EventArgs) Handles Me.Shown
            smallCrustRadioButton.Checked = False
        End Sub
    
        Private Sub orderPizzaButton_Click(sender As Object, e As EventArgs) _
            Handles orderPizzaButton.Click
    
            If PizzaBindingSource.DataSource IsNot Nothing Then
                MessageBox.Show(CType(PizzaBindingSource.Current, Pizza).PizzaCrust)
            End If
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Sunday, January 27, 2019 12:08 AM
    Moderator
  • OK, I think I understand what you have here.  These appear to me to be several event methods for button controls. 

    I am having trouble with the the OfType(). I get an error stating that OfType is not a member of Control.ControlCollection.  I think I can make it work once I understand how to deal with the Collection I appear to be missing.

    Also, I don't see how any of that addresses the issue of regenerating a default form display. 


    gwboolean

    Monday, January 28, 2019 2:30 AM
  • If OfType is not recognized then you are using a much earlier version of the .NET Framework, in this case you would need to get RadioButton controls the old fashion way.

    Call the following method passing in the container which could be the form, a GroupBox or a Panel which contains the RadioButton controls.

    Public Function GetRadioButtons(ByVal container As Control) As List(Of RadioButton)
        Dim theList As New List(Of RadioButton)
    
        For Each ctrl As Control In container.Controls
            theList.Add(DirectCast(ctrl, RadioButton))
        Next
    
        Return theList
    
    End Function


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 28, 2019 2:44 AM
    Moderator
  • In regards to "regenerating", there is no such thing without re-creating the form, what I showed was resetting a set of RadioButton controls, same needs to happen for all other controls or using DataBinding to do the reset but that is really overkill for this. I would complete an order then re-create the form thus reset per-say pretty much how a cash register works.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Marked as answer by gwboolean Monday, January 28, 2019 4:40 PM
    Monday, January 28, 2019 2:47 AM
    Moderator
  • I am using Netframe 2.0.  I can upgrade if needed.  I use the word regenerate because that was the first word off the top of my lil' punkin head.  I have been using 2.0 because I had issues with SQL when using a later version.

    I was wondering about the databinding and what that was for.  Thanks, I will check all of that out and see if I can solve the problem by upgrading and finally figure out why I have SQL problems with a higher Netframe version.

    By the way, was up in your neck of the woods last week camping at Silver Falls.  Good stuff.


    gwboolean

    Monday, January 28, 2019 4:39 PM

  • By the way, was up in your neck of the woods last week camping at Silver Falls.  Good stuff.


    gwboolean

    We (our Miata Club) love driving through the falls in the summer time,

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Monday, January 28, 2019 6:39 PM
    Moderator