简体   繁体   English

即使将 IsEnabled 属性设置为 false,Excel 按钮也不会被禁用

[英]Excel button is not disabled even when IsEnabled property is set to false

On button click I want the button clicked to be disabled and after executing a process re-enable it.单击按钮时,我希望禁用单击的按钮,并在执行进程后重新启用它。

While it seems quite simple, for a reason the whole process executes successfully but the button is not disabled.虽然看起来很简单,但出于某种原因,整个过程成功执行但按钮并未禁用。 It gets disabled on specific occasions and not in the start of the button click.它在特定情况下被禁用,而不是在按钮开始时被禁用。

Below is the XAML code of the button下面是按钮的 XAML 代码

<Button 
    x:Name="PreviewReportButton"
    Click="PreviewExcelReportButton_Click"
    Background="{StaticResource ExportExcelButtonColor}"
    BorderBrush="{StaticResource ExportExcelButtonColor}"
    Focusable="False"
    IsEnabled="False"
    Width="80"
    Height="Auto"
    Margin="450,0,0,0"
    FontSize="9"
    FontWeight="DemiBold"
    HorizontalAlignment="Left"
    VerticalAlignment="Center"
    Grid.Column="3"
    Grid.Row="5"
    Cursor="Hand"
    VerticalContentAlignment="Center"
    HorizontalContentAlignment="Center">
    <TextBlock TextAlignment="Center">Button 1</TextBlock>
</Button>

The .cs file code in c# VS 2019 c# VS 2019 中的 .cs 文件代码

private void PreviewExcelReportButton_Click(object sender, RoutedEventArgs e)
{
    var btn_excel = (Button)sender;
    btn_excel.IsEnabled = false;
    //PreviewReportButton.IsEnabled = false; This is the same button as btn_excel (the button I want to click)

    Debug.WriteLine("Button must be disabled");

    try
    {
        //Check if file is open
        int IsMacroFileOpen = CheckFileIsOpen($@"{path}file_1.xlsm");
        int IsReportFileOpen = CheckFileIsOpen($@"{path}file_2.xlsm");
        int IsXLSXFileOpen = CheckFileIsOpen($@"{path}file_3.xlsx");

        Debug.WriteLine(IsMacroFileOpen);
        Debug.WriteLine(IsReportFileOpen);
        Debug.WriteLine(IsXLSXFileOpen);

        if (new[] { 1, 2 }.Contains(IsMacroFileOpen) || new[] { 1, 2 }.Contains(IsReportFileOpen) || new[] { 1, 2 }.Contains(IsXLSXFileOpen))
        {
            btn_excel.IsEnabled = true;
            return;
        }

        Mouse.OverrideCursor = Cursors.Wait;

        //Step 1: Create copy of standard report file
        CreateCopyReportServerNameDB($@"{path}file_1.xlsm");

        //Step 2: Run macro
        ExecuteExcelMacro($@"{path}file_2.xlsm");

        //Step 3: Open a copy of the xlsx updated file
        //Approach 1
        Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

        ExcelApp.DisplayAlerts = false;
        ExcelApp.Visible = true;

        Microsoft.Office.Interop.Excel.Workbook ExcelWorkBook = ExcelApp.Workbooks.Add($"{path}file_3.xlsx");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to execute Excel button.\nPlease contact application support", "Error produced", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
    }
    finally
    {
        Mouse.OverrideCursor = null;
        btn.IsEnabled = true;
    }
}

Don't pay so much attention on what each method (CreateCopyReportServerNameDB, ExecuteExcelMacro) do because it's not relevant to the question.不要太关注每个方法(CreateCopyReportServerNameDB、ExecuteExcelMacro)的作用,因为它与问题无关。 The whole functionality works.整个功能都有效。 What does not work is the button disable on top of my code.不起作用的是我的代码顶部的按钮禁用。 When I click the button, the Cursor changes to Wait since I use Mouse.OverrideCursor = Cursors.Wait;当我单击按钮时,由于我使用了Mouse.OverrideCursor = Cursors.Wait;因此 Cursor 更改为Wait Mouse.OverrideCursor = Cursors.Wait; . . The weird thing is that the button is only disabled when a file is open which is catched by CheckFileIsOpen method.奇怪的是,该按钮仅在CheckFileIsOpen方法CheckFileIsOpen的文件打开时才被禁用。 If no file is open the button is never disabled.如果没有打开文件,则永远不会禁用该按钮。 And I am sure that I disable the button only on the top of the class and I renable it only at the end.而且我确信我只在班级顶部禁用按钮,并且只在最后启用它。

Also you will notice that I have put a Debug.Writeline to write if the IsEnabled = false;您还会注意到,如果IsEnabled = false; ,我已经放置了一个Debug.Writeline来写入IsEnabled = false; successfully executes and indeed the line is written as output.成功执行,实际上该行被写入为输出。 Although the button is never disabled unless a file is open and get caught by the mehtod CheckFileIsOpen .尽管按钮永远不会被禁用,除非文件打开并被方法CheckFileIsOpen捕获。

Apologize that I reached the point to post a question for such a "dummy" think because to me it's obvious.抱歉,我已经到了为这样一个“虚拟”思考发布问题的地步,因为对我来说这很明显。 But I cannot understand why this is happening to the IsEnabled of the button.但我不明白为什么按钮的 IsEnabled 会发生这种情况。 Apologize if this is so simple, but I cannot figure out what is happening.如果这很简单,请道歉,但我无法弄清楚发生了什么。

The problem seems to be when I execute the second method ExecuteExcelMacro The code of this method问题好像是我执行第二种方法的时候ExecuteExcelMacro这个方法的代码

public void ExecuteExcelMacro(string sourceFile)
{
    var destinationFile = @"file_1";
    Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();

    ExcelWorkBook = ExcelApp.Workbooks.Open(sourceFile);

    string macro = "ThisWorkbook.Run_Code";
    try
    {
        ExcelApp.Run(macro);
        Debug.WriteLine("Macro: " + macro + " executed successfully");
    }
    catch (Exception)
    {
        this.Effect = new BlurEffect();
        bool? Result = new CustomMessageBox($"Unable to Run Macro: {macro}", "Cannot execute Macro", MessageType.Error, MessageButtons.Ok).ShowDialog();
        if (Result.Value)
        {
            this.Effect = null;
            return;
        }
        else
        {
            this.Effect = null;
            return;
        }
        //Debug.WriteLine("Unable to Run Macro: " + macro + " Exception: " + ex.Message);
    }

    ExcelApp.DisplayAlerts = false;
    ExcelApp.Visible = false;

    ExcelWorkBook.SaveAs($@"{path}{destinationFile}", Microsoft.Office.Interop.Excel.XlFileFormat.xlOpenXMLWorkbook, Type.Missing);
    ExcelWorkBook.Close(0);
    ExcelApp.Quit();

    if (ExcelWorkBook != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelWorkBook); }
    if (ExcelApp != null) { System.Runtime.InteropServices.Marshal.ReleaseComObject(ExcelApp); }
}

In ExecuteExcelMacro() method, after creating an instance of ExcelApp make ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false;在 ExecuteExcelMacro() 方法中,创建 ExcelApp 实例后,使ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false; ExcelApp.ScreenUpdating=false; ExcelApp.Calculation=manual; ExcelApp.DisplayAlerts=false; . .

Don't made ExcelApp.Visible=false;不要使ExcelApp.Visible=false;

And in final block of PreviewExcelReportButton_Click() method, do Mouse.OverrideCursor = Cursors.Arrow;在 PreviewExcelReportButton_Click() 方法的最后一个块中,执行Mouse.OverrideCursor = Cursors.Arrow;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM