Ticket Q559647
Visible to All Users

How to set focus to a ParametrizedAction (Full Text Search) in code

created 11 years ago

Dear DevExpress Support,

Please suggest how to focus the Full Text Search Box on ListView activation.

The approach suggested in http://www.devexpress.com/Support/Center/Question/Details/Q141866 doesn't seem to work

C#
if (Frame.Template is DevExpress.ExpressApp.Win.Templates.MainForm) { BarManager manager = ((MainForm)Frame.Template).BarManager; manager.Items["Filter by Text"].PerformClick(); }

Merry Christmas and a Happy New Year to you.

Keep up the good work!

Thank you.

Comments (1)

    The above code does not work from an action either.

    Answers approved by DevExpress Support

    created 11 years ago (modified 6 years ago)

    Hello,

    To accomplish this task, you can access the corresponding bar item, as shown in the How to: Customize Action Controls (WinForms) topic, and call the BarItemLink.Focus method at an appropriate moment. In addition, when the TabbedMDI mode is enabled, you need to use the BarItem.GetVisibleLinks method to obtain an appropriate BarItemLink. Note that this method will work in this scenario only starting with version 16.1.8.

    To test it, include the Controller below into the YourSolutionName.Module.Win project and be sure to reference the DevExpress.XtraBars assembly:

    C#
    using System; using DevExpress.XtraBars; using System.Windows.Forms; using DevExpress.ExpressApp; using DevExpress.Persistent.Base; using DevExpress.ExpressApp.Actions; using DevExpress.ExpressApp.Templates; using DevExpress.XtraBars.Docking2010; using DevExpress.ExpressApp.SystemModule; using DevExpress.ExpressApp.Win.Templates; using DevExpress.XtraBars.Docking2010.Views; using DevExpress.ExpressApp.Win.SystemModule; using DevExpress.ExpressApp.Templates.ActionControls; namespace MainDemo.Module.Win.Controllers { public class FocusFullTextSearchController : WindowController { private const string STR_ViewIdToBeFocused = "Contact_ListView"; private FilterController filterController; protected override void OnActivated() { base.OnActivated(); Frame.TemplateChanged += Frame_TemplateChanged; if (!IsMdiMode()) { filterController = Frame.GetController<FilterController>(); if(filterController != null) { filterController.FullTextFilterAction.Changed += FullTextFilterAction_Changed; } } } private bool IsContactListView(DevExpress.ExpressApp.View view) { return view != null && view.Id == STR_ViewIdToBeFocused; } private bool IsMdiMode() { UIType type = ((IModelOptionsWin)Application.Model.Options).UIType; return type == UIType.TabbedMDI || type == UIType.StandardMDI; } private void Frame_TemplateChanged(object sender, EventArgs e) { if(IsMdiMode() && Frame.Template is IXafDocumentsHostWindow) { ((IXafDocumentsHostWindow)Frame.Template).DocumentManager.ViewChanged += DocumentManager_ViewChanged; ((IXafDocumentsHostWindow)Frame.Template).DocumentManager.ViewChanging += DocumentManager_ViewChanging; } } private void DocumentManager_ViewChanging(object sender, ViewEventArgs args) { args.View.DocumentActivated -= View_DocumentActivated; } private void DocumentManager_ViewChanged(object sender, ViewEventArgs args) { args.View.DocumentActivated += View_DocumentActivated; } private static BarItem GetFullTextSearchBarItem(Form form) { IActionControlsSite site = form as IActionControlsSite; foreach(IActionControlContainer actionContainer in site.ActionContainers) { if(actionContainer.ActionCategory == PredefinedCategory.FullTextSearch.ToString()) { IActionControl actionControl = actionContainer.FindActionControl(FilterController.FullTextSearchActionId); if(actionControl != null) { return actionControl.NativeControl as BarItem; } } } return null; } private void View_DocumentActivated(object sender, DocumentEventArgs e) { IViewHolder viewHolder = e.Document.Form as IViewHolder; BarItem barItem = GetFullTextSearchBarItem(e.Document.Form); if(viewHolder != null && barItem != null && IsContactListView(viewHolder.View)) { FocusFullTextSearch(barItem); } } private void FullTextFilterAction_Changed(object sender, ActionChangedEventArgs e) { ActionBase action = sender as ActionBase; BarItem barItem = GetFullTextSearchBarItem(Frame.Template as Form); if(e.ChangedPropertyType == ActionChangedType.Active && action.Active && barItem != null && IsContactListView(Frame.View)) { FocusFullTextSearch(barItem); } } private void FocusFullTextSearch(BarItem barItem) { if(Application.MainWindow == null) { return; } ((Form)Application.MainWindow.Template).BeginInvoke(new MethodInvoker(() => { BarItemLinkReadOnlyCollection visibleLinks = barItem.GetVisibleLinks(); if(visibleLinks.Count > 0) { visibleLinks[0].Focus(); } })); } protected override void OnDeactivated() { base.OnDeactivated(); Frame.TemplateChanged -= Frame_TemplateChanged; if(Frame.Template is IXafDocumentsHostWindow) { ((IXafDocumentsHostWindow)Frame.Template).DocumentManager.ViewChanged -= DocumentManager_ViewChanged; ((IXafDocumentsHostWindow)Frame.Template).DocumentManager.ViewChanging -= DocumentManager_ViewChanging; } if(filterController != null) { filterController.FullTextFilterAction.Changed -= FullTextFilterAction_Changed; } } } }

    This code supports the TabbedMDI, SingleSDI and MultipleWindowSDI UTType modes.

    See Also:
    How to focus a nested parametrized action

      Show previous comments (14)
      M M
      Martin Praxmarer - DevExpress MVP 9 years ago

        Sorry Guys - was my fault - works perferct now!

        thank you!

        Anatol (DevExpress) 9 years ago

          You are welcome!

          DevExpress Support Team 6 years ago

            Hello,

            We updated the solution to fix the issue described in the WinForms - NullReferenceException occurs in some circumstances in the code of the Q559647 ticket ticket.

            Thanks,
            Nat.

            Other Answers

            created 11 years ago
              Comments (2)

                Thanks Martin for your quick reply and a proposed solution.
                Well, it works for when the FormStyle is Ribbon but fails when FormStyle is Standard, in the demo project itself.
                Any other ideas?
                Happy New Year to you!

                M M
                Martin Praxmarer - DevExpress MVP 11 years ago

                  sorry - as i never dealed with form style Standard, i cannot help much here, but i guess it should not much differ from the ribbon style!
                  happy new year also to you!

                  Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

                  Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.