addhandlers and addressofs
-
I'm having a problem with the Addressof part. Do i have to do a sub procedure for the click events, if so how? Do I use the performclick method? I have added the part of code i'm using. Any help or advice will be welcomed. Dim mnuFileRecent1 As New MenuItem Dim mnuFileRecent2 As New MenuItem Dim mnuFileRecent3 As New MenuItem Dim mnuFileRecent4 As New MenuItem Dim RecentFiles(3) As String Dim ThePath As String Dim I As Integer Private Function AppPath() As String Dim A As String 'Executing file location and name A = Reflection.Assembly.GetExecutingAssembly.Location 'Strip off file name A = Mid(A, 1, InStrRev(A, "\")) Return (A) End Function Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOprn.Click If dlgOpen.ShowDialog() = DialogResult.OK Then FileOpen(1, dlgOpen.FileName, OpenMode.Input) txtOutput.Text = InputString(1, CInt(LOF(1))) txtOutput.SelectionLength = 0 FileClose(1) ThePath = dlgOpen.FileName RearrangeFiles(ThePath) End If End Sub Private Sub RearrangeFiles(ByVal F As String) 'Reorder the four most recent files located in the menu structure Dim TempArray(4) As String Dim TempArrayCounter As Integer = 1 Dim i As Integer 'The first item is the name of the file just opened. TempArray(0) = F 'Loop the the origonal array, if the item is not the 'same as the new file opened add it to the temp array. For i = 0 To 3 If RecentFiles(i) <> F Then TempArray(TempArrayCounter) = RecentFiles(i) TempArrayCounter += 1 End If Next i 'Reassign the array RecentFiles = TempArray 'Re-populate the menu. mnuFileRecent1.Text = "1 " + RecentFiles(0) mnuFileRecent2.Text = "2 " + RecentFiles(1) mnuFileRecent3.Text = "3 " + RecentFiles(2) mnuFileRecent4.Text = "4 " + RecentFiles(3) End Sub Private Sub frmRecent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Try FileOpen(1, AppPath() + "Recent.dat", OpenMode.Input) mnuFileRecent.MenuItems.Add(mnuFileRecent1) mnuFileRecent.MenuItems.Add(mnuFileRecent2) mnuFileRecent.MenuItems.Add(mnuFileRecent3) mnuFileRecent.MenuItems.Add(mnuFileRecent4) 'AddHandler mnuFileRecent1.Click, AddressOf Input(1, RecentFiles(0)) Input(1, RecentFiles(1)) Input(1, RecentFiles(2)) Input(1, RecentFiles(3)) FileClose(1) Catch ex As Exception End Try Try For I = 0 To 3 Select Case I Case 0 mnuFileRec
-
I'm having a problem with the Addressof part. Do i have to do a sub procedure for the click events, if so how? Do I use the performclick method? I have added the part of code i'm using. Any help or advice will be welcomed. Dim mnuFileRecent1 As New MenuItem Dim mnuFileRecent2 As New MenuItem Dim mnuFileRecent3 As New MenuItem Dim mnuFileRecent4 As New MenuItem Dim RecentFiles(3) As String Dim ThePath As String Dim I As Integer Private Function AppPath() As String Dim A As String 'Executing file location and name A = Reflection.Assembly.GetExecutingAssembly.Location 'Strip off file name A = Mid(A, 1, InStrRev(A, "\")) Return (A) End Function Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click Me.Close() End Sub Private Sub mnuFileOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuFileOprn.Click If dlgOpen.ShowDialog() = DialogResult.OK Then FileOpen(1, dlgOpen.FileName, OpenMode.Input) txtOutput.Text = InputString(1, CInt(LOF(1))) txtOutput.SelectionLength = 0 FileClose(1) ThePath = dlgOpen.FileName RearrangeFiles(ThePath) End If End Sub Private Sub RearrangeFiles(ByVal F As String) 'Reorder the four most recent files located in the menu structure Dim TempArray(4) As String Dim TempArrayCounter As Integer = 1 Dim i As Integer 'The first item is the name of the file just opened. TempArray(0) = F 'Loop the the origonal array, if the item is not the 'same as the new file opened add it to the temp array. For i = 0 To 3 If RecentFiles(i) <> F Then TempArray(TempArrayCounter) = RecentFiles(i) TempArrayCounter += 1 End If Next i 'Reassign the array RecentFiles = TempArray 'Re-populate the menu. mnuFileRecent1.Text = "1 " + RecentFiles(0) mnuFileRecent2.Text = "2 " + RecentFiles(1) mnuFileRecent3.Text = "3 " + RecentFiles(2) mnuFileRecent4.Text = "4 " + RecentFiles(3) End Sub Private Sub frmRecent_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load Try FileOpen(1, AppPath() + "Recent.dat", OpenMode.Input) mnuFileRecent.MenuItems.Add(mnuFileRecent1) mnuFileRecent.MenuItems.Add(mnuFileRecent2) mnuFileRecent.MenuItems.Add(mnuFileRecent3) mnuFileRecent.MenuItems.Add(mnuFileRecent4) 'AddHandler mnuFileRecent1.Click, AddressOf Input(1, RecentFiles(0)) Input(1, RecentFiles(1)) Input(1, RecentFiles(2)) Input(1, RecentFiles(3)) FileClose(1) Catch ex As Exception End Try Try For I = 0 To 3 Select Case I Case 0 mnuFileRec
Hi, some comments: 1. you can add a single MenuItem Click handler for all four mnuFileRecent MenuItems. you can find its MenuItem from its sender argument, and the filename from its text. I see no need for Addressof. 2. you don't need PerformClick, it simulates clicking something. What I am missing is the code that sets the texts for mnuFileRecent1-4 3. You don't need tempArray: first check whether the new file is already in RecentFiles; if not, shift them down (from hi to lo index) before you add F at 0. if it is there, shift them down starting with overwriting the old F, upwards, then put F at 0. 4. You may want to improve the handling when fewer than 4 recent files are known (initially or after clearing the list, if that is offered). suggestion: set up a convention to store/load the situation to/from file; disable menu items that are not functional yet. 5. You might consider using Application.UserAppDataPath rather than your AppPath(), since the latter may be read-only, and the former automatically exists per user. :)
Luc Pattyn [Forum Guidelines] [My Articles]
this weeks tips: - make Visual display line numbers: Tools/Options/TextEditor/... - show exceptions with ToString() to see all information - before you ask a question here, search CodeProject, then Google