Announcement

Collapse
No announcement yet.

UI loses control when displaying message (error or warning)

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    UI loses control when displaying message (error or warning)

    I created 3 applications that performs the same processing: "Load a family in revit"

    #1 FormElementHost (windows form + usercontrol wpf elementhost) : (lose focus)
    #2 FormNoElementHost (windows form) : (not lose focus)
    #3 WindowWPF (Window) : (lose focus)

    Even if I use an ElementHost to host a usercontrol wpf in which there is a wpf button, the focus does not work in the windows form (case #1)

    Link of video with the 3 examples

    Link project visual studio (addin revit + family 2015/2016/2017)
    Link forum revit api


    1)FormElementHost (windows form + usercontrol wpf elementhost) : (lose focus)
    -Command:
    Code:
    [Transaction(TransactionMode.Manual)]
            public class FormElementHostCommand : IExternalCommand
            {
                public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
                {
                    FormElementHost form = new FormElementHost(commandData);
    
                    form.ShowDialog();
    
                    return Result.Succeeded;
                }
            }
    -Form (System.Windows.Forms.Form) :
    Code:
    public partial class FormElementHost : Forms.Form
        {
            ExternalCommandData _commandData;
    
            public FormElementHost(ExternalCommandData commandData)
                : this()
            {
                _commandData = commandData;
            }
    
            public FormElementHost()
            {
                InitializeComponent();
                this.userControl11.ClickEvent += UserControl11_ClickEvent;
            }
    
            private void UserControl11_ClickEvent(object sender, EventArgs e)
            {
                Family family;
                string message;
                UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
            }
        }
    -UserControl WPF (System.Windows.Controls.UserControl) :
    Code:
    public partial class UserControl1 : UserControl
        {
            public event EventHandler ClickEvent;
    
            private void RaisesEventClick()
            {
                if (this.ClickEvent != null)
                    this.ClickEvent(this, new EventArgs());
            }
    
            public UserControl1()
            {
                InitializeComponent();
            }
    
            private void Button_Click(object sender, RoutedEventArgs e)
            {
                RaisesEventClick();
            }
        }



    2)FormNoElementHost (windows form) : (not lose focus)
    -Command:
    Code:
    [Transaction(TransactionMode.Manual)]
        public class FormNoElementHostCommand : IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                FormNoElementHost form = new FormNoElementHost(commandData);
    
                form.ShowDialog();
    
                return Result.Succeeded;
            }
        }
    -Form (System.Windows.Forms.Form) :
    Code:
    public partial class FormNoElementHost : Forms.Form
        {
            ExternalCommandData _commandData;
    
            public FormNoElementHost(ExternalCommandData commandData)
                : this()
            {
                _commandData = commandData;
            }
    
            public FormNoElementHost()
            {
                InitializeComponent();
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                Family family;
                string message;
                UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
            }
        }



    3)WindowWPF (Window) : (lose focus)
    -Command :
    Code:
     [Transaction(TransactionMode.Manual)]
        public class WindowWPFCommand : IExternalCommand
        {
            public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
            {
                WindowWPF  window = new WindowWPF(commandData);
    
                window.ShowDialog();
    
                return Result.Succeeded;
            }
        }

    -Window (System.Windows.Window) :
    Code:
    public partial class WindowWPF : Window
    {
        ExternalCommandData _commandData;
    
        public WindowWPF(ExternalCommandData commandData)
            : this()
        {
            _commandData = commandData;
        }
    
        public WindowWPF()
        {
            InitializeComponent();
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            Family family;
            string message;
            UtilsFamily.LoadFamily(_commandData.Application.ActiveUIDocument.Document, out family, out message);
        }
    }



    UtilsFamily :
    Code:
    public class UtilsFamily
        {
            public static bool LoadFamily(Document doc, out Family family, out string message)
            {
                family = null;
                message = null;
    
                // ask user to select a family file
                System.Windows.Forms.OpenFileDialog fileDlg = new System.Windows.Forms.OpenFileDialog();
                fileDlg.Filter = "Revit family (*.rfa)|*.rfa";
                fileDlg.Multiselect = false;
                if (fileDlg.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                    return false;
    
                using (Transaction tr = new Transaction(doc))
                {
                    tr.Start("Load family");
    
                    if (!doc.LoadFamily(fileDlg.FileName, new FamilyOption(), out family))
                    {
                        message = "Could not load family !";
                        tr.RollBack();
                        return false;
                    }
    
                    tr.Commit();
                }
                return true;
            }
        }

Related Topics

Collapse

Working...
X