Saving users quick access toolbar

Apr 2, 2011 at 2:51 PM

Hi There

 

What is the best way I can save and restore the users quick access toolbar buttons?

Thanks.

Coordinator
Apr 2, 2011 at 3:19 PM

If I'm not mistaking, there is a sample that show how to use the quick access tool-bar.
among other things it shows how to save and load QAT settings.

Apr 2, 2011 at 3:21 PM
Edited Apr 2, 2011 at 3:23 PM

That's what i was looking for. Thanks!

Apr 11, 2011 at 10:22 PM
Edited Apr 11, 2011 at 10:35 PM

Hi Arik and tompsonn,

tompsonn, I'm the guy that replied to your other post about using my.settings for the minimized state.

I've been trying to use my.settings to save the Quick Access Toolbar (QAT) settings, but haven't gotten it to work.  I was wondering if you had any luck with arik's suggestion looking at the QAT sample application.  I looked at the sample application and saving and loading settings seems to work for 1 session only.  In other words, after restarting the application, opening/loading the QAT settings has no effect (the settings aren't saved).

So, I've been trying to save the QAT settings stream as a string in my.settings so the settings would still be saved after restarting the application.

Here's what I've tried.

To save the settings (in form closing event):

        ' save ribbon QAT settings 
        Dim _stream As Stream = New MemoryStream()
        Ribbon1.SaveSettingsToStream(_stream)
        My.Settings.QuickAccessToolbarSettings = _stream.ToString() ' This is a problem, .ToString() returns "System.IO.MemoryStream"

 

To load the settings (in form load event):

        Dim _stream As Stream = New MemoryStream(System.Text.Encoding.ASCII.GetBytes(My.Settings.QuickAccessToolbarSettings))

        If _stream Is Nothing Then
            ' do nothing
        Else
            ' load ribbon QAT settings 
            _stream.Position = 0
            Ribbon1.LoadSettingsFromStream(_stream)
        End If
Apr 12, 2011 at 3:14 AM
Edited Apr 12, 2011 at 3:16 AM

You obviously can't convert a memory stream directly to a string...

You'd need to store the memory stream as a base 64 encoded byte[] string.

Apr 12, 2011 at 4:40 AM
Edited Apr 12, 2011 at 8:31 AM

FYI, this is how i did it (obviously you'll need to edit it to work with your app)

 

		/// <summary>
		/// 
		/// </summary>
		/// <param name="base64ByteStream"></param>
		public void LoadCustomizations( string base64ByteStream )
		{
			/* INIT */
			if ( this.thisRibbon != null )
			{
				Stream ribbonCustomizationsStream = new MemoryStream();
				byte[] byteStream;

				try
				{
					if ( ribbonCustomizationsStream != null )
					{
						if ( !string.IsNullOrEmpty( base64ByteStream ) )
						{
							try
							{
								/* Get the bytes back and write them */
								byteStream = Convert.FromBase64String( base64ByteStream );

								if ( byteStream != null )
								{
									ribbonCustomizationsStream.Write( byteStream, 0, byteStream.Length );
									ribbonCustomizationsStream.Position = 0;

									this.thisRibbon.LoadSettingsFromStream( ribbonCustomizationsStream );
								}
							}
							catch ( FormatException )
							{
								/* Do nothing */
							}
						}
					}
				}
				finally
				{
					/* Release any memory we were using */
					byteStream = null;

					if ( ribbonCustomizationsStream != null )
					{
						ribbonCustomizationsStream.Close();
						ribbonCustomizationsStream = null;
					}
				}
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public string SaveCustomizations()
		{
			/* INIT */
			string base64ByteStream = null;

			if ( this.thisRibbon != null )
			{
				Stream ribbonCustomizationsStream = new MemoryStream();
				byte[] byteStream;

				try
				{
					if ( ribbonCustomizationsStream != null )
					{
						this.thisRibbon.SaveSettingsToStream( ribbonCustomizationsStream );

						/* Get the bytes and convert it to base64 */
						if ( ribbonCustomizationsStream.Length > 0 )
						{
							byteStream = new byte[ ribbonCustomizationsStream.Length ];

							if ( byteStream != null )
							{
								ribbonCustomizationsStream.Position = 0;
								ribbonCustomizationsStream.Read( byteStream, 0, byteStream.Length );

								base64ByteStream = Convert.ToBase64String( byteStream, 0, byteStream.Length );
							}
						}
					}
				}
				finally
				{
					/* Release any memory we were using */
					byteStream = null;

					if ( ribbonCustomizationsStream != null )
					{
						ribbonCustomizationsStream.Close();
						ribbonCustomizationsStream = null;
					}
				}
			}

			return base64ByteStream;
		}


It also handles minimized/QAT below or above ribbon settings too :)
Apr 12, 2011 at 8:55 PM
Edited Apr 12, 2011 at 10:23 PM

Thanks for responding.  I did what you said initially and converting to base64string from stream/byte array and vice versa seems to work in the sample application.  However, for some reason the exact same code doesn't work in the main application I'm working on.  The reason is as follows.

 

        Dim _stream As Stream = New MemoryStream()
        Ribbon1.SaveSettingsToStream(_stream) ' This seems to be doing nothing
        MsgBox(_stream.Length) ' this always returns "0"

 

Whenever I run the above code whether from a ribbon button or in the formClosing event, it seems the settings are never written to the stream....  The stream length is always zero.  Again, in the sample application, this exact code always indicates that the stream length is 386 or higher.  This is the case even when no buttons have been added to the QAT.

Any possible idea why this would be happening?

Update:

Ribbon1.SaveSettingsToStream(_stream) only works for me in the Sample Applications.  I've tried creating brand new applications and it never works.  The stream length is always 0.
Any idea what the difference might be between the Sample Apps and the new ones I'm creating???
Apr 13, 2011 at 12:07 AM
Edited Apr 13, 2011 at 1:08 AM

Make sure you are resetting the stream position to zero.

My code works perfectly outside of the sample apps - perhaps you could use a C# to VB converter and tweak it to suit your app?

 

Alternatively could you post what you are using here?

Also...... make sure you are defining a quick access toolbar in the ribbon XML! I had the same problem now, come to think of it, and as soon as I defined the quick access toolbar in the XML, it worked fine.

Apr 14, 2011 at 12:11 AM

Oh my goodness, it worked.  I just added the QAT to Ribbon.xml.  You're a genius!  Thanks so much, tompsonn. :)

Apr 14, 2011 at 12:51 AM

In case anyone's wondering, here's the complete code I'm using to save and load the QAT settings in VB .Net using My.Settings (Tested and Works).  Just make sure you define the QAT in the ribbon XML like tompsonn said, and like I failed to do.  Thanks again, tompsonn.

How to save (in FormClosing Event)

        Dim _stream As MemoryStream = New MemoryStream()
        Ribbon1.SaveSettingsToStream(_stream)
        My.Settings.QuickAccessToolbarSettings = Convert.ToBase64String(_stream.ToArray, 0, _stream.Length)

How to load (in FormLoad Event)

        Dim _stream As MemoryStream = New MemoryStream(Convert.FromBase64String(My.Settings.QuickAccessToolbarSettings))

        If _stream Is Nothing Then
            ' do nothing
        Else
            ' load ribbon QAT settings 
            _stream.Position = 0
            Ribbon1.LoadSettingsFromStream(_stream)
        End If

 

 

 

Apr 14, 2011 at 3:15 AM

Glad to hear it worked
On a side note, it may be more "complete" to handle a few exceptions and make sure the memory stream is disposed of when you're done with it :)