c# excel addin workbook close event - c#

i make an Excel COM Add-In.
Now i want to Control the WorkbookBeforeClose Event.
But the Solution that i found on Stackoverflow (c# using the event handler WorkbookOpen) works not for me.
In my Add-In there is no InternalStartup Sektion, there is only a Load Sektion.
I can only Start the Eventhandler by pushing a Button in my Ribbon Bar.
When i push the Button 1x the Eventhandler do what i want all the Time Excel is loaded.
When Excel is closed and restart i must Push the Button again.
Is there a Way that the Eventhandler ist Starting while Excel loads the first empty Workbook ???
Is there some Help or any Solution for me ???
Thanks and Regards
Stephan
Here is my Code, that i use for the EventHandler:
public partial class Excel2SE
{
Excel.AppEvents_WorkbookBeforeCloseEventHandler EventDel_BeforeBookClose;
private void Excel2SE_Load(object sender, RibbonUIEventArgs e)
{
EventDel_BeforeBookClose = new Excel.AppEvents_WorkbookBeforeCloseEventHandler(BeforeBookClose);
}
private void btn_Start_Click(object sender, RibbonControlEventArgs e)
{
wbEventhandlerStart();
}
private void wbEventhandlerStart()
{
Excel.Application app = (Excel.Application)Marshal.GetActiveObject("Excel.Application");
app.WorkbookBeforeClose += EventDel_BeforeBookClose;
}
// This Works when Excel Close
private void Excel2SE_Close(object sender, EventArgs e)
{
MessageBox.Show("Excel close");
}
// This works after Pushing the Start Button and the Workbook Close
private void BeforeBookClose(Excel.Workbook Wb, ref bool Cancel)
{
MessageBox.Show("WB close");
}

Is this a Visual Studio Office Add-In? It sounds like it is, if so check ThisAddIn.cs for the following 2 methods:
private void ThisAddIn_Startup(object sender, EventArgs e)
{
}
private void ThisAddIn_Shutdown(object sender, EventArgs e)
{
}
You can add the code you want to run on close to the ThisAddIn_Shutdown method. There's no way to ensure this code runs however, it will only run if the user closes Excel 'properly'. If they close using the task manager etc, all bets are off.

Related

Why when I close a sub-form in a Windows forms Add-in on Revit , it closes the entire add-in?

I have a Windows Forms Add-in on Revit using Revit-API and C#. The project contains two forms ( Main form and About form). the About form opens by a button click in the main form. The problem is that when I try to close the About form through the Control box or by using a button inside it About.ActiveForm.Close() it automatically closes all the Forms. I tried About.ActiveForm.Hide() and also doesn't work. Also, I tried to link the Cancel Button of the about form to that button and it didn't work.
Code of initiating the About form
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog(this);
}
Code of Closing the About Form
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
I also tried this for the closing part
private void button3_Click(object sender, EventArgs e)
{
Close();
}
and I tried this also for the initiating part
private void button3_Click(object sender, EventArgs e)
{
About abt = new About();
abt.ShowDialog();
}

C# VSTO Excel Task Pane not showing when embedding Excel in Word

Currently I'm working on a Excel Add-In where the user can select an XML file. I read the XML and add nodes to a TreeView. This TreeView is placed within the Task Pane. When starting Excel on its own everything works, but when I add a Chart or an Embedded Excel in Word and open it in Excel, the Task Pane is not showing up.
This code eventually triggers the event for opening the TaskPane
private void ShowTaskPane_TglBtn_Click(object sender, RibbonControlEventArgs e)
{
Globals.ThisAddIn.TaskPane.Visible = ((RibbonToggleButton)sender).Checked;
}
This code opens the Task Pane for real:
public partial class ThisAddIn
{
public UserControl1 taskPaneControl1;
private Microsoft.Office.Tools.CustomTaskPane taskPaneValue;
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
taskPaneControl1 = new UserControl1();
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "Task Pane");
taskPaneValue.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
}
private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
Globals.Ribbons.Ribbon1.ShowTaskPane_TglBtn.Checked =
taskPaneValue.Visible;
}
public Microsoft.Office.Tools.CustomTaskPane TaskPane
{
get
{
return taskPaneValue;
}
}
}

in c#, how can you disable a button after the 1st click, and enable it again after you click another button?

I cannot figure this out
i'm making a windows form application with visual basic in c#
i have a scan button and it scans everything in the folder and lists all of the files in the listbox
if you click it another time the list of files appear again
how can you make it so you can only press the scan button once, and then you can press it again if you click the browse button?
the browse button is to select the folder you want to scan
thanks
This is pretty trivial
private void ScanButtonClick(object sender, EventArgs e)
{
// do something
(sender as Button).Enabled = false;
}
private void BrowseButtonClick(object sender, EventArgs e)
{
ScanButton.Enabled = true;
}
Its a bit unclear if you're writing in C# or vb.net, but since the question is tagged as C#...
private void btnScan_Click(object sender, EventArgs e) {
btnScan.Enabled = false;
// other code here
}
private void btnBrowse_Click(object sender, EventArgs e) {
btnScan.Enabled = true;
//other code here
}
I tried this in my windows form application in C# and it works fine!
private void button3_Click_1(object sender, EventArgs e)
{
int count = 0;
count++;
//add your code here
if (count == 1) {
button3.Enabled = false;
//only one click allowed
}
}

How do I make a button show the listbox when clicked?

I am making a form where if a button is clicked it will go to my list box and run the functions I have in there though I am a bit confused on how to make it realise when the button has been clicked and for the listbox to work. Here is my code >_>
private void button1_Click(object sender, EventArgs e)
{
}
public void ListBox_SelectedIndexChanged(object sender, EventArgs e)
{
Ping.PlayConsole();
}
You just need:
private void button1_Click(object sender, EventArgs e)
{
Ping.PlayConsole();
}
It's ok to call the same function under different handlers.
Try this :
private void button1_Click(object sender, EventArgs e)
{
ListBox_SelectedIndexChanged(sender,e);
}
Good Luck !!
private void button1_Click(object sender, EventArgs e)
{
ListBox.Focus();
Ping.PlayConsole();
}
Both event handlers share the same signature void (object , EventArgs), so they are call-compatible.
If you are connecting the event visually using the form designer:
Go to the Property Inspector's Events pane and instead of double-clicking it to create an event handler stub for button1.Click, click the dropdown box icon that appears on the right side. Visual Studio will show all the event handlers present in the form that have a compatible signature, you should be able to choose ListBox_SelectIndexChanged for the button1.Click handler. They will be sharing the same handler.
If you are connecting the handler by code then this should work too:
ListBox1.SelectIndexChanged += new System.EventHandler(ListBox_SelectedIndexChanged);
button1.Click += new System.EventHandler(ListBox_SelectedIndexChanged);

How do I close an open file?

This button opens a text file which has error log information:
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
Process.Start(AppVars.ErrorLogFilePath);
}
When the user goes to do any processing in the application, I want to check whether the file is open or not, if it is open, then I want to close it. How would I go about doing this?
This example is almost identical to what you're trying to do:
Process.Close Method
Process myProcess;
private void btnViewErrorLogFile_Click(object sender, EventArgs e)
{
myProcess.Start(AppVars.ErrorLogFilePath);
}
private void doSomething()
{
if (!myProcess.HasExited)
{
myProcess.CloseMainWindow();
myProcess.Close();
}
// Do whatever you need with the file
}
Shows how to check if it's running and how to close it.

Categories

Resources