Version

Add an MRU List to a Menu

A MRU (most recently used) list can be helpful in a number of situations, such as letting users access the most recent files that they saved. A good tool to use to accomplish this is with a List tool of the WinToolbarsManager™. As files are saved you can insert that file name into the ListToolItems collection. The ListTool has a MaxItemsToDisplay property to only list that many items. After that you will see another selection that displays the value for the MoreItemsText property to display another list of recently used files. You will also have to use the SaveAsBinary and LoadFromBinary methods on the UltraToolBarsManager to save the configuration of the tool bar to show these MRU files in the menu drop down the next time the application is run.

  1. To have a functional MRU list in your menu tool, first add a PopUpMenuTool and to this add a ListTool with the Key value of MRUList.

  2. Add a ButtonTool to the Popup Menu Tool. The button will be used to open a new file, so give it the Key of "New".

  3. Drag the Menu to a toolbar that has it’s IsMainMenuBar property set to True.

  4. Drag the List tool and drop it onto the PopUpMenu’s drop down.

  5. Drag the Button tool onto another toolbar. These toolbars will be on an MDI parent form called frmMain.

  6. Enter the code for frmMain as follows.

In Visual Basic:

Private LastDocumentNumber As Integer = 0
' Determine which tool was clicked
Private Sub UltraToolbarsManager1_ToolClick(ByVal sender As Object, _
  ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) _
  Handles UltraToolbarsManager1.ToolClick
	Select Case e.Tool.Key
		Case "New"
			' Change the image and Tag on the New tool to indicate the last
			' item chosen was an RTF Document
			Me.UltraToolbarsManager1.Tools("New"). _
			  SharedProps.AppearancesSmall.Appearance.Image = _
			  e.Tool.SharedProps.AppearancesSmall.Appearance.Image()
			Me.UltraToolbarsManager1.Tools("New").SharedProps.Tag = "New RTF Document"
			' Create a new RTF Doc
			NewRTFDocument()
		Case "MRUList"
			' User clicked an item on the MRU List tool.
			Dim aListTool As Infragistics.Win.UltraWinToolbars.ListTool
			Dim aListToolItem As Infragistics.Win.UltraWinToolbars.ListToolItem
			aListTool = e.Tool
			' Get the ListToolItem which was clicked
			aListToolItem = aListTool.SelectedItem()
			' Open the document based on the ListToolItem
			' The Key of the ListToolItem is the filename of the document
			' to load.
			OpenDocument(aListToolItem.Key)
	End Select
End Sub
Private Sub NewRTFDocument()
	Dim frm As New frmRTFDocument()
	LastDocumentNumber += 1
	frm.MdiParent = Me
	frm.Text = "New RTF Document " & LastDocumentNumber
	frm.Show()
End Sub
Private Sub OpenDocument(Optional ByVal FileName As String = "")
	Open(FileName, False)
End Sub
Private Sub Open(ByVal FileName As String, _
  Optional ByVal PromptToReOpen _
  As Boolean = True)
	Dim ExistingChildForm As Form
	ExistingChildForm = GetExistingChildForm(FileName)
	If Not ExistingChildForm Is Nothing Then
		Dim dlResult As DialogResult
		If PromptToReOpen Then
			dlResult = _
			  MessageBox.Show( _
			    "This file is already open. Do you want to reload it?", _
			  MsgBoxStyle.YesNo Or MsgBoxStyle.Question, "Confirm?")
			If dlResult = System.Windows.Forms.DialogResult.Yes Then
				ExistingChildForm.Close()
			Else
				Exit Sub
			End If
		Else
			ExistingChildForm.Focus()
			Exit Sub
		End If
	End If
	Dim File As IO.FileInfo
	File = New IO.FileInfo(FileName)
	Select Case File.Extension.ToLower
		Case ".rtf"
			Dim frm As New frmRTFDocument()
			frm.MdiParent = Me
			frm.Text = FileName
			frm.RichTextBox1.LoadFile(FileName)
			frm.HasFileName = True
			frm.Show()
	End Select
End Sub
' Tries to get an existing form based on the filename of the document
' This is mostly used to prevent opening the same document twice.
Private Function GetExistingChildForm(ByVal FileName As String) As Form
	Dim ChildForm As Form
	For Each ChildForm In Me.MdiChildren
		If ChildForm.Text = FileName Then
			Return ChildForm
		End If
	Next
	Return Nothing
End Function

In C#:

private void ultraToolbarsManager1_ToolClick(object sender,
  Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
{
	// Determine which tool was clicked
	switch (e.Tool.Key)
	{
		case "New":
		{
			// Change the image and Tag on the New tool to indicate the last
			// item chosen was an RTF Document
			this.ultraToolbarsManager1.Tools["New"].
			  SharedProps.AppearancesSmall.Appearance.Image =
			  e.Tool.SharedProps.AppearancesSmall.Appearance.Image;
			this.ultraToolbarsManager1.Tools["New"].SharedProps.Tag = "New RTF Document";
			// Create a new RTF Doc
			NewRTFDocument();
			break;
		}
		case "MRUList":
		{
			// User clicked an item on the MRU List tool.
			Infragistics.Win.UltraWinToolbars.ListTool aListTool;
			Infragistics.Win.UltraWinToolbars.ListToolItem
			  aListToolItem;
			// Cast e.Tool (which is a ToolBase) into a List too, so we can access
			// tool-specific properties
			aListTool =
			  (Infragistics.Win.UltraWinToolbars.ListTool)e.Tool;
			// Get the ListToolItem which was clicked
			aListToolItem = aListTool.SelectedItem;
			// Open the document based on the ListToolItem
			// The Key of the ListToolItem is the filename of the
			// document to load.
			OpenDocument(aListToolItem.Key);
			break;
		}
	}
}
private void NewRTFDocument()
{
            int LastDocumentNumber=0;
	frmRTFDocument frm = new frmRTFDocument();
	LastDocumentNumber += 1;
	frm.MdiParent = this;
	frm.Text = "New RTF Document " + LastDocumentNumber;
	frm.Show();
}
private void OpenDocument(string FileName)
{
	Open(FileName, false);
}
private void Open(string FileName, bool PromptToReOpen)
{
	Form ExistingChildForm;
	ExistingChildForm = GetExistingChildForm(FileName);
	if (!(ExistingChildForm == null))
	{
		DialogResult dlResult;
		if (PromptToReOpen)
		{
			dlResult = MessageBox.Show("This file is already open. Do you want to reload it?",
			  "Confirm?",  MessageBoxButtons.YesNo, MessageBoxIcon.Question );
			if (dlResult == System.Windows.Forms.DialogResult.Yes)
			{
				ExistingChildForm.Close();
			}
			else
			{
				return;
			}
		}
		else
		{
			ExistingChildForm.Focus();
			return;
		}
	}
	System.IO.FileInfo File;
	File = new System.IO.FileInfo(FileName);
	switch (File.Extension.ToLower())
	{
		case ".rtf":
			frmRTFDocument frmRTF = new frmRTFDocument();
			frmRTF.MdiParent = this;
			frmRTF.Text = FileName;
			frmRTF.richTextBox1.LoadFile(FileName);
			frmRTF.HasFileName = true;
			frmRTF.Show();
			break;
	}
}
// Tries to get an existing form based on the filename of the document
// This is mostly used to prevent opening the same document twice.
private Form GetExistingChildForm(string FileName)
{
	foreach (Form ChildForm in this.MdiChildren)
	{
		if (ChildForm.Text == FileName)
		{
			return ChildForm;
		}
	}
	return null;
}
  1. Add a second form called frmRTFDocument to the project.

  2. Add a RichTextBox control to frmRTFDocument.

  3. Add an UltraToolBarsManager to frmRTFDocument and name it RTFUltraToolbarsManager.

  4. Create a toolbar and add a Button tool with the Key of Save to it which be used to save the RTF file on the form.

  5. Add a SaveFileDialog component to the form.

  6. Enter the code for frmRTFDocument as follows:

In Visual Basic:

Public HasFileName As Boolean
Private Sub RTFUltraToolbarsManager_ToolClick(ByVal sender As Object, _
  ByVal e As Infragistics.Win.UltraWinToolbars.ToolClickEventArgs) _
  Handles RTFUltraToolbarsManager.ToolClick
	Select Case e.Tool.Key
		Case "Save"
			' Save the current RTF document to a file
			SaveDocument()
	End Select
End Sub
Private Sub SaveDocument()
	Dim dlResult As DialogResult
	If Me.HasFileName Then
		Save(Me.Text)
	Else
		Me.SaveFileDialog1.FileName = Me.Text
		Me.SaveFileDialog1.Filter = "$$*$$.rtf|$$* $$.rtf"
		dlResult = Me.SaveFileDialog1.ShowDialog()
		If dlResult = System.Windows.Forms.DialogResult.OK Then
			Save(Me.SaveFileDialog1.FileName)
		End If
	End If
End Sub
Private Sub Save(ByVal FileName As String)
	Dim frm As frmMain
	Dim ListTool As Infragistics.Win.UltraWinToolbars.ListTool
	Me.HasFileName = True
	Me.RichTextBox1.SaveFile(FileName)
	Dim SavedFile As New IO.FileInfo(FileName)
	frm = DirectCast(Me.MdiParent, frmMain)
	ListTool = frm.UltraToolbarsManager1.Tools("MRUList")
	If Not ListTool.ListToolItems.Exists(SavedFile.Name) Then
		ListTool.ListToolItems.Insert(0, SavedFile.FullName, SavedFile.Name)
	End If
	Me.Text = SavedFile.FullName
End Sub

In C#:

public bool HasFileName;
private void RTFUltraToolbarsManager_ToolClick(object sender,
  Infragistics.Win.UltraWinToolbars.ToolClickEventArgs e)
{
	switch (e.Tool.Key)
	{
		case "Save":
			// Save the current RTF document to a file
			SaveDocument();
			break;
	}
}
private void SaveDocument()
{
	DialogResult dlResult;
	if (this.HasFileName)
	{
		Save(this.Text);
	}
	else
	{
		this.saveFileDialog1.FileName = this.Text;
		dlResult = this.saveFileDialog1.ShowDialog();
		if (dlResult == System.Windows.Forms.DialogResult.OK)
		{
			Save(this.saveFileDialog1.FileName);
		}
	}
}
private void Save(string FileName)
{
	frmMain frm;
	Infragistics.Win.UltraWinToolbars.ListTool ListTool;
	this.HasFileName = true;
	this.richTextBox1.SaveFile(FileName);
	System.IO.FileInfo SavedFile = new System.IO.FileInfo(FileName);
	frm = (frmMain)this.MdiParent;
	ListTool = (Infragistics.Win.UltraWinToolbars.ListTool)frm.ultraToolbarsManager1.Tools["MRUList"];
	if (!(ListTool.ListToolItems.Exists(SavedFile.Name)))
	{
		ListTool.ListToolItems.Insert(0, SavedFile.FullName, SavedFile.Name);
	}
	this.Text = SavedFile.FullName;
}