473,405 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Recent File Menu in VB.net

In VB.NET, I'm adding a recent file menu to my application. Reading / Writing the strings(file name + paths) to a txt file.

I'm just wondering, once in the document, how do you reorder them?

Is there someone I could email my code to to have a look at this problem, as its been driving me mad for ages! :-D

Appreciate any help,

Ben

[mail id removed]
Dec 11 '07 #1
2 2147
debasisdas
8,127 Expert 4TB
You can post your code here in this thread for recerence of our experts.
Dec 11 '07 #2
Im sort of getting there with it now, i think, just wondering can anyone tell me what is wrong with this now? its full of things going wrong!


Option Strict On
Option Explicit On

Public Class Form1
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents dlgSave As System.Windows.Forms.SaveFileDialog
Friend WithEvents dlgOpen As System.Windows.Forms.OpenFileDialog
Friend WithEvents MenuItem4 As System.Windows.Forms.MenuItem
Friend WithEvents MnuOpen As System.Windows.Forms.MenuItem
Friend WithEvents MnuFile As System.Windows.Forms.MenuItem
Friend WithEvents MnuSave As System.Windows.Forms.MenuItem
Friend WithEvents mnuMain As System.Windows.Forms.MainMenu
Friend WithEvents lblTemp As System.Windows.Forms.Label
Friend WithEvents lblDate As System.Windows.Forms.Label
Friend WithEvents ListBox1 As System.Windows.Forms.ListBox
Friend WithEvents btnEnter As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents txtTemp As System.Windows.Forms.TextBox
Friend WithEvents btnDelete As System.Windows.Forms.Button
Friend WithEvents MnuRec1 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec2 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec3 As System.Windows.Forms.MenuItem
Friend WithEvents MnuRec4 As System.Windows.Forms.MenuItem
Friend WithEvents lblFile As System.Windows.Forms.Label
Friend WithEvents MnuFileSep2 As System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.txtTemp = New System.Windows.Forms.TextBox
Me.lblTemp = New System.Windows.Forms.Label
Me.Label1 = New System.Windows.Forms.Label
Me.dlgSave = New System.Windows.Forms.SaveFileDialog
Me.dlgOpen = New System.Windows.Forms.OpenFileDialog
Me.mnuMain = New System.Windows.Forms.MainMenu
Me.MnuFile = New System.Windows.Forms.MenuItem
Me.MnuOpen = New System.Windows.Forms.MenuItem
Me.MnuSave = New System.Windows.Forms.MenuItem
Me.MenuItem4 = New System.Windows.Forms.MenuItem
Me.MnuRec1 = New System.Windows.Forms.MenuItem
Me.MnuRec2 = New System.Windows.Forms.MenuItem
Me.MnuRec3 = New System.Windows.Forms.MenuItem
Me.MnuRec4 = New System.Windows.Forms.MenuItem
Me.lblDate = New System.Windows.Forms.Label
Me.ListBox1 = New System.Windows.Forms.ListBox
Me.btnEnter = New System.Windows.Forms.Button
Me.Label2 = New System.Windows.Forms.Label
Me.btnDelete = New System.Windows.Forms.Button
Me.lblFile = New System.Windows.Forms.Label
Me.MnuFileSep2 = New System.Windows.Forms.MenuItem
Me.SuspendLayout()
'
'txtTemp
'
Me.txtTemp.Location = New System.Drawing.Point(136, 56)
Me.txtTemp.Name = "txtTemp"
Me.txtTemp.TabIndex = 8
Me.txtTemp.Text = ""
'
'lblTemp
'
Me.lblTemp.Location = New System.Drawing.Point(48, 48)
Me.lblTemp.Name = "lblTemp"
Me.lblTemp.Size = New System.Drawing.Size(72, 32)
Me.lblTemp.TabIndex = 10
Me.lblTemp.Text = "Highest Temperature"
Me.lblTemp.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(8, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(64, 16)
Me.Label1.TabIndex = 7
Me.Label1.Text = "Entry Date"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'dlgSave
'
Me.dlgSave.Filter = "Text Files (*.txt)|*.txt"
Me.dlgSave.Title = "Save"
'
'dlgOpen
'
Me.dlgOpen.Filter = "Text Files (*.txt)|*.txt"
Me.dlgOpen.Title = "Open"
'
'mnuMain
'
Me.mnuMain.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuFile})
'
'MnuFile
'
Me.MnuFile.Index = 0
Me.MnuFile.MenuItems.AddRange(New System.Windows.Forms.MenuItem() {Me.MnuOpen, Me.MnuSave, Me.MenuItem4, Me.MnuRec1, Me.MnuRec2, Me.MnuRec3, Me.MnuRec4, Me.MnuFileSep2})
Me.MnuFile.Text = "File"
'
'MnuOpen
'
Me.MnuOpen.Index = 0
Me.MnuOpen.Shortcut = System.Windows.Forms.Shortcut.CtrlO
Me.MnuOpen.Text = "Open"
'
'MnuSave
'
Me.MnuSave.Index = 1
Me.MnuSave.Shortcut = System.Windows.Forms.Shortcut.CtrlS
Me.MnuSave.Text = "Save"
'
'MenuItem4
'
Me.MenuItem4.Index = 2
Me.MenuItem4.Text = "-"
'
'MnuRec1
'
Me.MnuRec1.Index = 3
Me.MnuRec1.Text = ""
Me.MnuRec1.Visible = False
'
'MnuRec2
'
Me.MnuRec2.Index = 4
Me.MnuRec2.Text = ""
Me.MnuRec2.Visible = False
'
'MnuRec3
'
Me.MnuRec3.Index = 5
Me.MnuRec3.Text = ""
Me.MnuRec3.Visible = False
'
'MnuRec4
'
Me.MnuRec4.Index = 6
Me.MnuRec4.Text = ""
Me.MnuRec4.Visible = False
'
'lblDate
'
Me.lblDate.Location = New System.Drawing.Point(96, 8)
Me.lblDate.Name = "lblDate"
Me.lblDate.Size = New System.Drawing.Size(100, 16)
Me.lblDate.TabIndex = 12
Me.lblDate.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'ListBox1
'
Me.ListBox1.Location = New System.Drawing.Point(16, 128)
Me.ListBox1.Name = "ListBox1"
Me.ListBox1.ScrollAlwaysVisible = True
Me.ListBox1.Size = New System.Drawing.Size(280, 121)
Me.ListBox1.TabIndex = 14
'
'btnEnter
'
Me.btnEnter.Location = New System.Drawing.Point(160, 88)
Me.btnEnter.Name = "btnEnter"
Me.btnEnter.TabIndex = 15
Me.btnEnter.Text = "Enter"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(248, 56)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(32, 16)
Me.Label2.TabIndex = 13
Me.Label2.Text = "De C"
'
'btnDelete
'
Me.btnDelete.Location = New System.Drawing.Point(56, 88)
Me.btnDelete.Name = "btnDelete"
Me.btnDelete.TabIndex = 16
Me.btnDelete.Text = "Delete"
'
'lblFile
'
Me.lblFile.Location = New System.Drawing.Point(216, 8)
Me.lblFile.Name = "lblFile"
Me.lblFile.TabIndex = 17
'
'MnuFileSep2
'
Me.MnuFileSep2.Index = 7
Me.MnuFileSep2.Text = "-"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(320, 271)
Me.Controls.Add(Me.lblFile)
Me.Controls.Add(Me.btnDelete)
Me.Controls.Add(Me.btnEnter)
Me.Controls.Add(Me.ListBox1)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.lblDate)
Me.Controls.Add(Me.txtTemp)
Me.Controls.Add(Me.lblTemp)
Me.Controls.Add(Me.Label1)
Me.Menu = Me.mnuMain
Me.Name = "Form1"
Me.Text = "Temperatures"
Me.ResumeLayout(False)

End Sub

#End Region

Dim CurrentFile As String 'To keep track of the file we are working on.
Dim LastDateEntered As Date 'To know the last date entered to stop duplicates.
Dim rFiles(3) As String
Dim Arr(3) As MenuItem
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 Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblDate.Text = Date.Today.ToShortDateString
'check if a 'recent file' doc already exists
If System.IO.File.Exists(AppPath() + "recent.txt") Then
FillMenu()
Else
System.IO.File.Create(AppPath() + "recent.txt")
FillMenu()
End If

End Sub
Sub FillMenu()
Dim i As Integer
Dim info As String
Arr(0) = MnuRec1
Arr(1) = MnuRec2
Arr(2) = MnuRec3
Arr(3) = MnuRec4
FileOpen(2, AppPath() + "recent.txt", OpenMode.Input)
For i = 0 To 3
Input(2, info)
Arr(i).Text = info.ToString
Next
FileClose(2)
'create exit option as last item in FILE menu
Dim MnuExit As New MenuItem
MnuExit.Text = "E&xit"
MnuExit.Shortcut = Shortcut.AltF4
AddHandler MnuExit.Click, AddressOf MnuExit_Click
MnuFile.MenuItems.Add(MnuExit)
End Sub

Private Sub MnuExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If MessageBox.Show("Are you sure you wish to exit?", "Close", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.Yes Then
'This line only write one line to the file recreating the earlier problem.
'WriteLine(2, CurrentFile.ToString)
Dim i As Integer
FileOpen(1, AppPath() & "/recent.txt", OpenMode.Output)
For i = 0 To 3
WriteLine(1, rFiles(i))
Next i
FileClose(1)
End If
Me.Close()
End Sub
Public Function DegCToF(ByVal c As Double) As Double
Return (c * 1.8) + 32
End Function
Private Sub MnuOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuOpen.Click
Dim TempF As Double
Try

'Call your file open routine passing it the filename

If dlgOpen.ShowDialog() = DialogResult.OK Then
'If a file has been selected open it.
FileOpen(1, dlgOpen.FileName, OpenMode.Input)
'Keep track of the file that has been opened
CurrentFile = dlgOpen.FileName
'Now loop through it and read the values into the listbox
Do Until EOF(1)
Dim dte As Date
Dim dta As String
Input(1, dte) 'date
Input(1, dta) 'data (temperatures)
LastDateEntered = dte 'What was the last date entered
'The below is adding todays date + the value of the textbox which is nothing.
'ListBox1.Items.Add(Now.ToShortDateString + " - " + txtTemp.Text + " Deg C" + " - " + " " + TempF.ToString + " Deg F")

'Should be
ListBox1.Items.Add(dte.ToShortDateString + " - " + dta + " Deg C" + " - " + " " + DegCToF(CDbl(dta)).ToString + " Deg F")
Loop
Call RearrangeFiles(dlgOpen.FileName)
End If
'NB CORRECTED CATCHES
Catch IOExcept As IO.FileNotFoundException
MessageBox.Show("The file has not been found.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Catch MyException As Exception
MessageBox.Show("An error was detected. Please check, and try again.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
Finally
FileClose(1)
End Try

End Sub

Private Sub MnuSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuSave.Click
Dim i As Integer
Dim Message As String
Try
'Open the file we have been working on.
FileOpen(1, CurrentFile, OpenMode.Output)
Message = ("The document has been saved to " + ControlChars.Cr)
Message += CurrentFile.ToString + ControlChars.Cr
MessageBox.Show(Message, "Save Completed", MessageBoxButtons.OK, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1)

For i = 0 To ListBox1.Items.Count - 1
'Get the values from the listbox
Dim arr() As String = Split(ListBox1.Items(i).ToString, " ")
'Because we have altered the text in the listbox we need to alter
'the way we extract it.
WriteLine(1, arr(0).Trim, arr(4).Trim)
Next i

Catch MyException As IO.IOException
MessageBox.Show("An error has occured, and the Temperatures program needs to close.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
Me.Close()
Finally
FileClose(1)
End Try

End Sub

Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

'Use the function
'Dim TempC As String = txtTemp.Text.ToString
'Dim TempF As Double
'TempF = (CInt(TempC) * 1.8) + 32
If Now.Date = LastDateEntered Then
If MessageBox.Show("You have already entered information for today. Would you like to override the existing value? If not, the application will end.", "Error", _
MessageBoxButtons.YesNo, MessageBoxIcon.Error) = DialogResult.Yes Then
ListBox1.Items.RemoveAt(ListBox1.Items.Count - 1)
Else
Me.Close()
End If
End If
If CInt(txtTemp.Text) >= 99 Or CInt(txtTemp.Text) <= -20 Then
MessageBox.Show("Please enter a realistic temperature!", "Invalid Temperature", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)
txtTemp.Text = ""
Else

'Enter the new values
ListBox1.Items.Add(Now.ToShortDateString + " - " + txtTemp.Text + " Deg C" + _
" - " + " " + DegCToF(CDbl(txtTemp.Text)).ToString + " Deg F")
txtTemp.Text = ""
LastDateEntered = Now.Date 'Update the variable
End If

End Sub
Private Sub txtSold_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtTemp.KeyPress
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
'Allow numbers and backspace.
Case Asc("0") To Asc("9"), Asc(ControlChars.Back)
e.Handled = False
'Allow minus sign only if first entry.
Case Asc("-")
If Len(txtTemp.Text) = 0 Then
e.Handled = False
Else
e.Handled = True
End If
'Allow one decimal point
Case Asc(".")
If InStr(txtTemp.Text, ".") = 0 Then
e.Handled = False
Else
e.Handled = True
End If
'Disallow everything else
Case Else
e.Handled = True
End Select
End Sub

Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
ListBox1.Items.Clear()
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
Dim i As Integer

'The first item is the name of the file just opened.
TempArray(0) = F
TempArrayCounter = 1

'Loop the the origonal array, if the item is not the
For i = 0 To 3
If rFiles(i) <> F Then
TempArray(TempArrayCounter) = rFiles(i)
TempArrayCounter += 1
End If
Next i

'Read the temp array back into the origonal and the
'origonal back into the menu.
For i = 0 To 3
rFiles(i) = TempArray(i)
If rFiles(i) <> "" Then
Arr(i).Visible = True
Arr(i).Text = rFiles(i)
MnuFileSep2.Visible = True
End If
Next i
End Sub
End Class
Dec 13 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Suresh Kumaran | last post by:
How do you program in VB.NET to show the most recent used document in a dropdown menu? I am referring to something like in MS WORD or EXCEL where it shows the recent document the user was working...
3
by: Larry Bates | last post by:
I recently upgraded from 2.2 to 2.4 (ActiveState for Windows). I was accustomed to having the most recent 10 files that I had edited show up under File menu under Recent. After upgrading these...
1
by: Eric Robishaw | last post by:
In VS.net 2003, my recent files list will not update the Date. If I open a project from the list, modify it and close... the next time I load VS.net, the project shows the same old date. It...
7
by: William Payne | last post by:
Hello, have you seen a recent files menu in a GUI application? In many GUI applications there's a menu the displays the most recent files that has been opened by the program. Say such a menu has...
1
by: Nathan Bloomfield | last post by:
Is it possible to remove all or individual "recent files" from the "Recent files list" in MS Access XP? It would be preferred to be a programatic method using VBA as the system is locked down...
8
by: Maarten van Stam [MVP VSTO] | last post by:
I posted a feedback item in Microsoft Connect: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=277761 Please have a look at this, as I think it is a relative...
4
by: zacks | last post by:
Most applications whose purpose is to work with various types of files implement a "Most Recent Files" list, where the last, say, four files accessed by the application can quickly be re-opened by...
2
by: Anil Gupte | last post by:
I got as far as this; Private Sub PopulateRecentFilesList() For Each item As String In L3G.RecentFilesList mnuTopFile.MenuItems.Add(item, New EventHandler(AddressOf menuItem2_Click)) Next ...
7
by: PawelSokolowski | last post by:
Hi all, I migrated to VS2008, and I cannot find a way to get Recent Projects in File menu. I tried to customize the menu, but no success. Any ideas ? Regards Pawel
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.