Click here to Skip to main content
15,891,375 members
Articles / Programming Languages / Visual Basic

Replacing MainMenu Controls With VS2005 MenuStrip Controls

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
27 Feb 2008CPOL1 min read 32.7K   385   15  
Code to do an automated replace of the MainMenu controls
Imports System.Collections.ObjectModel

Public Class frmMain

	Private Sub TextBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtFolder.Click
		FolderBrowserDialog1.ShowDialog()
		Dim lstr As String = FolderBrowserDialog1.SelectedPath
		txtFolder.Text = lstr
	End Sub

	Private Sub cmdReplace_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdReplace.Click
		Me.Cursor = Cursors.WaitCursor
		ReplaceMenuControls(txtFolder.Text)
		MsgBox("Done !", MsgBoxStyle.Information, "Success")
		Me.Cursor = Cursors.Arrow
	End Sub

	Private Function ReplaceMenuControls(ByVal pstrPath As String) As Boolean

		Dim lcolFiles As ReadOnlyCollection(Of String)
		Dim lstrFile As String
		Dim lstrLine As String
		'Dim lstrFileContents As String
		Dim lobjReader As System.IO.StreamReader
		Dim lblnDoThisFile As Boolean

		Try
			lcolFiles = My.Computer.FileSystem.GetFiles(pstrPath, FileIO.SearchOption.SearchAllSubDirectories, "*.vb")
			For Each lstrFile In lcolFiles
				lblnDoThisFile = False
				If Not lstrFile.ToLower.Contains("designer") Then
					'lstrFileContents = My.Computer.FileSystem.ReadAllText(lstrFile)
					lobjReader = My.Computer.FileSystem.OpenTextFileReader(lstrFile)

					Do While Not lobjReader.EndOfStream
						lstrLine = lobjReader.ReadLine
						If lstrLine.Contains("As System.Windows.Forms.MainMenu") Then
							AddStatus("Replacing MainMenu control in " & lstrFile)
							lblnDoThisFile = True
							Exit Do
						End If
					Loop
					lobjReader.Close()
					Application.DoEvents()
					If lblnDoThisFile Then
						ReplaceMenuControlsInFile(lstrFile)
					End If

				End If
			Next

		Catch ex As Exception
			MessageBox.Show(ex.ToString, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		End Try

	End Function

	Private Function ReplaceMenuControlsInFile(ByVal pstrFile As String) As Boolean

		Dim lstrFileContents, lstrBuffer, lstrTemp, lstrMainMenuName As String
		Dim lintStart, lintSTemp As Integer
		Dim lintEnd, lintETemp As Integer
		Dim lintYPos As Integer

		Try
			lstrFileContents = My.Computer.FileSystem.ReadAllText(pstrFile)
			lstrFileContents = lstrFileContents.Replace("MainMenu", "MenuStrip")
			lstrFileContents = lstrFileContents.Replace("= New System.Windows.Forms.MenuStrip(Me.components)", "= New System.Windows.Forms.MenuStrip()")
			lstrFileContents = lstrFileContents.Replace("MenuItem", "ToolStripMenuItem")
			lstrFileContents = lstrFileContents.Replace("ToolStripToolStripMenuItem", "ToolStripMenuItem")

			lintStart = lstrFileContents.IndexOf("#Region "" Windows Form Designer generated code """)
			lintEnd = lstrFileContents.IndexOf("#End Region", lintStart)
			lstrBuffer = lstrFileContents.Substring(lintStart, lintEnd + 12 - lintStart)

			'Get name of mainmenu
			lintETemp = lstrBuffer.IndexOf(" As System.Windows.Forms.MenuStrip")
			lstrTemp = lstrBuffer.Substring(0, lintETemp)
			lintSTemp = lstrTemp.LastIndexOf("Friend WithEvents ") + 18
			lstrMainMenuName = lstrBuffer.Substring(lintSTemp, lintETemp - lintSTemp)

			lstrBuffer = lstrBuffer.Replace("Me." & lstrMainMenuName & ".ToolStripMenuItems.AddRange", "Me." & lstrMainMenuName & ".Items.AddRange")
			lstrBuffer = lstrBuffer.Replace("ToolStripMenuItems.AddRange(New System.Windows.Forms.ToolStripMenuItem()", "DropDownItems.AddRange(New System.Windows.Forms.ToolStripItem()")
			lstrBuffer = lstrBuffer.Replace("Me.Menu = Me." & lstrMainMenuName, "Me.MainMenuStrip = Me." & lstrMainMenuName & Environment.NewLine & "		Me.Controls.Add(Me." & lstrMainMenuName & ")")

			'Replace .Index with .MergeIndex for menuitems
			lintETemp = 0
			Do While lintETemp < lstrBuffer.Length
				lintETemp = lstrBuffer.IndexOf(" As System.Windows.Forms.ToolStripMenuItem", lintETemp + 1)
				If lintETemp = -1 Then
					Exit Do
				End If
				lstrTemp = lstrBuffer.Substring(0, lintETemp)
				lintSTemp = lstrTemp.LastIndexOf("Friend WithEvents ") + 18
				lstrTemp = lstrBuffer.Substring(lintSTemp, lintETemp - lintSTemp)
				lstrBuffer = lstrBuffer.Replace("Me." & lstrTemp & ".Index", "Me." & lstrTemp & ".MergeIndex")

				'look for separators
				If lstrBuffer.IndexOf("Me." & lstrTemp & ".Text = ""-""") > -1 Then
					lstrBuffer = lstrBuffer.Replace("Friend WithEvents " & lstrTemp & " As System.Windows.Forms.ToolStripMenuItem", "Friend WithEvents " & lstrTemp & " As System.Windows.Forms.ToolStripSeparator")
					lstrBuffer = lstrBuffer.Replace("Me." & lstrTemp & " = New System.Windows.Forms.ToolStripMenuItem", "Me." & lstrTemp & " = New System.Windows.Forms.ToolStripSeparator")
				End If

				'Replace Shortcut if set
				lstrBuffer = lstrBuffer.Replace("Me." & lstrTemp & ".Shortcut", "Me." & lstrTemp & ".ShortcutKeys")
			Loop

			'Increase form size
			lintSTemp = lstrBuffer.IndexOf("Me.ClientSize = New System.Drawing.Size(", lintSTemp)
			lintSTemp = lstrBuffer.IndexOf(",", lintSTemp + 40) + 2
			lintETemp = lstrBuffer.IndexOf(")", lintSTemp) - 1

			lintYPos = 0
			Try
				lintYPos = CInt(lstrBuffer.Substring(lintSTemp, lintETemp - lintSTemp + 1))
				lintYPos += 24
				lstrBuffer = lstrBuffer.Substring(0, lintSTemp - 1) & lintYPos.ToString & lstrBuffer.Substring(lintETemp + 1, lstrBuffer.Length - lintETemp - 1)
			Catch ex As Exception
			End Try

			'Move all controls on form down by 24 

			lintSTemp = lstrFileContents.IndexOf("Public Class ") + 13
			lintETemp = lstrFileContents.IndexOf(Environment.NewLine, lintSTemp)
			Dim lstrFormName As String = lstrFileContents.Substring(lintSTemp, lintETemp - lintSTemp)

			lintSTemp = lstrBuffer.IndexOf("'" & lstrFormName)
			Do While lintSTemp < lstrBuffer.Length
				lintSTemp = lstrBuffer.IndexOf("Me.Controls.Add(", lintSTemp)
				If lintSTemp = -1 Then
					Exit Do
				End If
				lintSTemp += 16
				lintETemp = lstrBuffer.IndexOf(")", lintSTemp)
				Dim lstrControlName As String = lstrBuffer.Substring(lintSTemp, lintETemp - lintSTemp)

				Dim lintSTempBackup As Integer = lintSTemp

				If lstrControlName <> lstrMainMenuName Then
					lintSTemp = lstrBuffer.IndexOf(lstrControlName & ".Location = New System.Drawing.Point(")
					If lintSTemp <> -1 Then
						lintSTemp = lstrBuffer.IndexOf(",", lintSTemp + 37 + lstrControlName.Length) + 2
						lintETemp = lstrBuffer.IndexOf(")", lintSTemp) - 1

						lintYPos = 0
						Try
							lintYPos = CInt(lstrBuffer.Substring(lintSTemp, lintETemp - lintSTemp + 1))
							lintYPos += 24
							lstrBuffer = lstrBuffer.Substring(0, lintSTemp - 1) & lintYPos.ToString & lstrBuffer.Substring(lintETemp + 1, lstrBuffer.Length - lintETemp - 1)
						Catch ex As Exception
						End Try
					End If
				End If

				lintSTemp = lintSTempBackup
			Loop

			'put buffer back and write file
			lstrFileContents = lstrFileContents.Substring(0, lintStart - 1) & lstrBuffer & lstrFileContents.Substring(lintEnd + 12, lstrFileContents.Length - lintEnd - 12)

			Dim lobjWriter As System.IO.StreamWriter
			lobjWriter = My.Computer.FileSystem.OpenTextFileWriter(pstrFile, False)
			lobjWriter.Write(lstrFileContents)
			lobjWriter.Close()

			Application.DoEvents()

		Catch ex As Exception
			Throw ex
		End Try

	End Function

	Private Sub AddStatus(ByVal pstrStatus As String)
		txtStatus.Text = pstrStatus & Environment.NewLine & txtStatus.Text
	End Sub

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) SDT
South Africa South Africa
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions