none
Change Startp Form - alternate method

    Question

  • I have a sandbox project that I use for trying out code. I often switch the startup form. Problem is the Project, properties, startup form dropdown has a long list of randomly ordered form names.

    Is there a way to sort the items in the dropdown?

    Search instead of list?

    Alternate way to set startup form? e.g. right click form name, Set as Startup?

    VS Community 2015 sp3 VB.Net


    • Edited by SLOAPCD IT Friday, July 29, 2016 4:07 PM version info
    Friday, July 29, 2016 4:06 PM

Answers

  • I created a "switchboard" form that presents a sorted list. If someone has an ear at the VS table please ask them to make the form dropdown sorted.

    Option Explicit On
    Imports System.Reflection
    Public Class frmMain
        'credit: http://stackoverflow.com/questions/26478165/calling-a-separate-windows-form-using-its-name-as-a-string
        ' add  controls - Dropdown(simple,sorted), button for open seelcted form and close main form
        ' add Seeting "PreviousForm" to Project
        Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
            For Each t As Type In Me.GetType().Assembly.GetTypes()
                If t.BaseType IsNot Nothing AndAlso t.BaseType.Name = "Form" Then
                    Debug.Print(t.Name)
                    ComboBox1.Items.Add(t.Name)
                End If
                ComboBox1.Text = My.Settings.Item("PreviousForm")
            Next
        End Sub
        Public Function CreateObjectInstance(ByVal objectName As String) As Object
            Dim obj As Object
            Try
                If objectName.LastIndexOf(".") = -1 Then
                    objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
                End If
                obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
            Catch ex As Exception
                obj = Nothing
            End Try
            Return obj
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            My.Settings.Item("PreviousForm") = ComboBox1.Text
            Try
                Dim frm As New Form
                frm = DirectCast(CreateObjectInstance(ComboBox1.Text), Form)
                frm.Show()
            Catch ex As Exception
                MsgBox(ex.Message,, ComboBox1.Text)
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class

    • Marked as answer by SLOAPCD IT Wednesday, September 07, 2016 3:01 PM
    Wednesday, September 07, 2016 3:00 PM

All replies

  • Hi SLOAPCD IT,

    >>Is there a way to sort the items in the dropdown? Search instead of list?

    I can't find the startup form dropdown in the Project, proerties. Did you mean the startup form dropdown list in the Solution, Properties as shown in the screenshot below:

    If so, I am afraid it is not support to soft the items in the dropdown list and also there is no way to change it to a serach box instead of list. This is by default.

    >>Alternate way to set startup form? e.g. right click form name, Set as Startup?

    You could right click the project, Set as the StatUp project.

    If I misunderstood your issue, please feel free to let me konw.

    Best Regards,

    Lake Xiao

    Monday, August 01, 2016 5:43 AM
    Moderator
  • I was referring to the startup Form for a Project:

    0 right click Project in Solution Explorer

    0 Properties

    0 Application tab, third row - Startup Form

    VS Community 2015 sp3 VB.Net WinForm project

    Tuesday, September 06, 2016 4:20 PM
  • I created a "switchboard" form that presents a sorted list. If someone has an ear at the VS table please ask them to make the form dropdown sorted.

    Option Explicit On
    Imports System.Reflection
    Public Class frmMain
        'credit: http://stackoverflow.com/questions/26478165/calling-a-separate-windows-form-using-its-name-as-a-string
        ' add  controls - Dropdown(simple,sorted), button for open seelcted form and close main form
        ' add Seeting "PreviousForm" to Project
        Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
            For Each t As Type In Me.GetType().Assembly.GetTypes()
                If t.BaseType IsNot Nothing AndAlso t.BaseType.Name = "Form" Then
                    Debug.Print(t.Name)
                    ComboBox1.Items.Add(t.Name)
                End If
                ComboBox1.Text = My.Settings.Item("PreviousForm")
            Next
        End Sub
        Public Function CreateObjectInstance(ByVal objectName As String) As Object
            Dim obj As Object
            Try
                If objectName.LastIndexOf(".") = -1 Then
                    objectName = [Assembly].GetEntryAssembly.GetName.Name & "." & objectName
                End If
                obj = [Assembly].GetEntryAssembly.CreateInstance(objectName)
            Catch ex As Exception
                obj = Nothing
            End Try
            Return obj
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            My.Settings.Item("PreviousForm") = ComboBox1.Text
            Try
                Dim frm As New Form
                frm = DirectCast(CreateObjectInstance(ComboBox1.Text), Form)
                frm.Show()
            Catch ex As Exception
                MsgBox(ex.Message,, ComboBox1.Text)
            End Try
        End Sub
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Me.Close()
        End Sub
    End Class

    • Marked as answer by SLOAPCD IT Wednesday, September 07, 2016 3:01 PM
    Wednesday, September 07, 2016 3:00 PM