Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BUG: Can't choose dates on a DatePicker that fall outside a floating VSTO Add-In

I logged the issue with Microsoft here - the Repro is available for download: https://connect.microsoft.com/VisualStudio/feedback/details/741454/value-change-event-doesnt-fire-for-datetimepicker-controls-used-in-vsto-add-ins

If you put a DateTimePicker in a Excel VSTO floating Add-In and position it so when the calendar drops down, it is outside the edge of the add-in, see here:

enter image description here

Choosing any of the dates circled in the green works as expected, but when clicking any dates circled in red, it just closes the calendar drop down and doesn't set the date!

Does anyone know how I can fix this?

Edit

This SO user has experienced the problem using WPF: VSTO WPF ContextMenu.MenuItem Click outside a TaskPane not raised

enter image description here

The answer to that question shows the issue was reported to connect a while back but still no solution with VSTO 4.0 SP1: https://connect.microsoft.com/VisualStudio/feedback/details/432998/excel-2007-vsto-custom-task-pane-with-wpf-context-menu-has-focus-problems

One of the workarounds is to use the DispatcherFrame to pump messages and subscribe to GotFocusEvent and LostFocusEvent for the menu. http://blogs.msdn.com/b/vsod/archive/2009/12/16/excel-2007-wpf-events-are-not-fired-for-items-that-overlap-excel-ui-for-wpf-context-menus.aspx but this is all WPF code for menu's not a solution for Winform DateTimePicker.

Repro for Microsoft Connect:

New Project > Excel 2010 Add-In

using TaskPane;
using Microsoft.Office.Core;

namespace ExcelAddIn2
{
public partial class ThisAddIn
{
    TaskPaneView MyTaskView = null;
    Microsoft.Office.Tools.CustomTaskPane MyTaskPane = null;

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
    //setup custom taskpane
    MyTaskView = new TaskPaneView();
    MyTaskView.currentInstance = Globals.ThisAddIn.Application;
    MyTaskPane = this.CustomTaskPanes.Add(MyTaskView, "MyTaskView");
    MyTaskPane.DockPosition = Microsoft.Office.Core.MsoCTPDockPosition.msoCTPDockPositionFloating;
    MyTaskPane.DockPositionRestrict = MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
    MyTaskPane.Visible = true;
}
}

File Menu > Add > New Project > Class Library > named TaskPane

Then in the TaskPane project create a User Control called TaskPaneView

public partial class TaskPaneView : UserControl
{
    public TaskPaneView()
    {
        InitializeComponent();
    }

    public Microsoft.Office.Interop.Excel.Application currentInstance { get; set; }

    public TaskPaneCtrl getTaskPaneCtrl
    {
        get { return this.taskPaneCtrl1; }
    }  

}

Next create a User Control with a DateTimePicker, make sure the Calendar control is located toward the bottom right of the user control

public partial class TaskPaneCtrl : UserControl
{
public TaskPaneCtrl()
{
    InitializeComponent();
}
}

In the TaskPane class library Reference the Excel Interop (eg c:\Program Files x86\Microsoft Visual Studio 14.0\Visual Studio Tools for Office\PIA\Office14\Microsoft.Office.Interop.Excel.dll).

Build the solution. Commenting out parts that dont work. Build Solution.

Now drag and drop the TaskPaneCtrl onto the TaskPaneView and uncomment thing that failed to compile.

enter image description here

F5 and click the Calendar Control, now try to select a date that is outside the taskpane area. No Value Change event fires, it behaves like a click outside the calendar!

Note: I tried a drop downlist that falls off the control but its events DO FIRE!

like image 883
Jeremy Thompson Avatar asked May 10 '12 00:05

Jeremy Thompson


People also ask

How to reference selected date in custom datepicker control?

When you want to reference the selected date value from the custom datepicker control, please reference value through the _dateSelected variable directly. If this post helps, then please consider Accept it as the solution to help the other members find it more quickly. 07-07-2020 01:22 PM Hi, how are the days made available for users?

How to disable the weekdays in the datepicker?

As an alternative solution, you could consider custom a DatePicker control in your app, then you could disable the weekdays within the custom DatePicker based on your needs. I have customized a DatePicker in my app, and disable other weekdays in it except the Tuesday and Thursday. the screenshot as below:

How to filter out dates in the angular material datepicker?

The Angular Material Datepicker provides the matDatepickerFilter property, which may be set to a function to filter out dates.

How to convert selected date to text in flow?

You can use the Text function to convert the selected date in the date picker to the format that the flow expects. Something along the lines of 10-19-2017 08:04 AM You can use the Text function to convert the selected date in the date picker to the format that the flow expects. Something along the lines of


1 Answers

"Floating" is the key to the problem here. What's never not a problem (occasionally responsible for odd things) is relying on the message pump in Excel to dispatch Windows messages, the messages that make these controls respond to input. This goes wrong in WPF as much as Winforms, they have their own dispatch loop that filters messages before they are delivered to the window. Key things that go wrong when their respective dispatcher isn't used are stuff like tabbing and short-cut keystrokes.

And then some, this kind of problem would be induced by Excel doing its own filtering before dispatching messages. I'd guess at an anti-malware feature, Microsoft is forever worried about programs messing with Office apps.

The Winforms solution is the same one as the WPF workaround, you need to pump your own message loop. That requires some surgery, DateTimePicker isn't going to cooperate since it doesn't allow its DropDown event to be cancelled and it is raised after the calendar is already shown. The workaround is silly but effective, add a button to your form that looks just like the dropdown arrow on the DTP and make it overlap the arrow so it gets clicked instead of the arrow.

Some example code for getting the button to overlap the dropdown arrow:

    public Form1() {
        InitializeComponent();
        var btn = new Button();
        btn.BackgroundImage = Properties.Resources.DropdownArrow;
        btn.FlatStyle = FlatStyle.Flat;
        btn.BackColor = Color.FromKnownColor(KnownColor.Window);
        btn.Parent = dateTimePicker1;
        btn.Dock = DockStyle.Right;
        btn.Click += showMonthCalendar;
        dateTimePicker1.Resize += delegate {
            btn.Width = btn.Height = dateTimePicker1.ClientSize.Height;
        };
    }

The Click event handler needs to show a dialog that contains a MonthCalendar:

    private void showMonthCalendar(object sender, EventArgs e) {
        dateTimePicker1.Focus();
        using (var dlg = new CalendarForm()) {
            dlg.DateSelected += new DateRangeEventHandler((s, ea) => dateTimePicker1.Value = ea.Start);
            dlg.Location = dateTimePicker1.PointToScreen(new Point(0, dateTimePicker1.Height));
            dlg.ShowDialog(this);
        }
    }

With CalendarForm a form you add that's borderless and contains just a MonthCalendar:

public partial class CalendarForm : Form {
    public event DateRangeEventHandler DateSelected;

    public CalendarForm() {
        InitializeComponent();
        this.StartPosition = FormStartPosition.Manual;
        monthCalendar1.Resize += delegate {
            this.ClientSize = monthCalendar1.Size;
        };
        monthCalendar1.DateSelected += monthCalendar1_DateSelected;
    }

    void monthCalendar1_DateSelected(object sender, DateRangeEventArgs e) {
        if (DateSelected != null) DateSelected(this, e);
        this.DialogResult = DialogResult.OK;
    }
}
like image 103
Hans Passant Avatar answered Oct 05 '22 23:10

Hans Passant