commandbar scope

P

Prashant

Hi,

Actually I have three sheet in my excel workbook & for every sheet I want to
create different commandbars which have buttons.

I have used following code for sheet1 to create commandbar buttons:

private Office.CommandBar commandBar;
private Office.CommandBarButton downloadButton;
private Office.CommandBarButton uploadButton;

private void Sheet1_Startup(object sender, System.EventArgs e)
{
this.Deactivate += new
Microsoft.Office.Interop.Excel.DocEvents_DeactivateEventHandler(Sheet1_Deactivate);
this.ActivateEvent += new
Microsoft.Office.Interop.Excel.DocEvents_ActivateEventHandler(Sheet1_Activate);

CheckForToolbar();
}

private void Sheet1_Shutdown(object sender, System.EventArgs e)
{
this.Deactivate += new
Microsoft.Office.Interop.Excel.DocEvents_DeactivateEventHandler(Sheet1_Deactivate);

try
{
commandBar = this.Application.CommandBars["TrimsBar"];
commandBar.Visible = false;
}
catch
{
}
}

void Sheet1_Activate()
{
CheckForToolbar();
}

void Sheet1_Deactivate()
{
try
{
commandBar = this.Application.CommandBars["TrimsBar"];
commandBar.Visible = false;
}
catch
{
}
}

private void CheckForToolbar()
{
// Verify that the toolbar is not already present
try
{
commandBar = this.Application.CommandBars["TrimsBar"];

commandBar.Visible = true;
}
catch
{
// Not present, so create a new one
CreateToolbar();
}
}

private void CreateToolbar()
{
try
{
commandBar = Application.CommandBars.Add("TrimsBar",
Office.MsoBarPosition.msoBarTop, false, true);

// Word count button
downloadButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, true);
downloadButton.Style = Office.MsoButtonStyle.msoButtonCaption;
downloadButton.Caption = "Select Trims";
downloadButton.Tag = "SelectTrims";

downloadButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(btnSelectTrims_Click);

// Words per paragraph button
uploadButton =
(Office.CommandBarButton)commandBar.Controls.Add(1, missing, missing,
missing, true);
uploadButton.Style = Office.MsoButtonStyle.msoButtonCaption;
uploadButton.Caption = "Upload";
uploadButton.Tag = "Upload";
uploadButton.BeginGroup = true;

uploadButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(btnUploadButton_Click);

clearButton.Click += new
Office._CommandBarButtonEvents_ClickEventHandler(btnClearButton_Click);

// Make the command bar visible (a new command bar is
// not visible by default
commandBar.Visible = true;
}
catch (Exception e)
{
MessageBox.Show(e.ToString(), "Initialize error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

In the same way I have placed all these code to every sheet with different
name. As I start the excel application with sheet1 as start sheet, all
commandbars for each sheets get shown.

But as per my requirement I want that only commandbar specific to sheet1
should be visible as the application starts & when user click on sheet2, the
command bar for sheet1 should get invisible & respected to sheet2 get shown.
Same for all the sheet.

Please assist me show that I can make it clear.

Thank you
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top