Single click on button not working but double click does the work

Single click on button not working but double click does the work

T52K-BIB
Enthusiast Enthusiast
5,531 Views
16 Replies
Message 1 of 17

Single click on button not working but double click does the work

T52K-BIB
Enthusiast
Enthusiast

hi everyone,

 

While I'm Click Button from my FORM, Single click on button not working but the double click does the work.

Please help me, How to resolve and let me know why for my knowledge.

 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.ApplicationServices;
using System.Diagnostics;

namespace RevitAddin3
{
    public partial class Form1 : System.Windows.Forms.Form
    {
        private UIApplication uiapp;
        private UIDocument uidoc;
        private Autodesk.Revit.ApplicationServices.Application app;
        private Document doc;

        public String rnumber;

        public Form1(ExternalCommandData commandData)
        {
            InitializeComponent();
            uiapp = commandData.Application;
            uidoc = uiapp.ActiveUIDocument;
            app = uiapp.Application;
            doc = uidoc.Document;

        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            rnumber = roomNameBox.Text;

            searchButton.DialogResult = DialogResult.OK;
            Debug.WriteLine("Search Clicked");

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            cancelButton.DialogResult = DialogResult.Cancel;
            Debug.WriteLine("Cancel Clicked");
        }
    }
}

 

 

thiru2jack_1-1612863853086.png

 

0 Likes
Accepted solutions (1)
5,532 Views
16 Replies
Replies (16)
Message 2 of 17

RPTHOMAS108
Mentor
Mentor

With Forms there is Button.Click and Button.DoubleClick, in C# you can't see which one you are handing unless you select the button in the designer or look at the other half of the partial class file created by the designer.

 

VB would look like below so you can instantly know and don't have to keep looking back and forth.

 

Public Class MainForm
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    End Sub

    Private Sub Button1_DoubleClick(sender As Object, e As EventArgs) Handles Button1.DoubleClick
    End Sub
End Class

 

That's the benefit of the WithEvents keyword that some say in C# is not required (it is designer generated 95% of the time).

 

Obviously you could name your sub anything to match but it gets messy in C# when they handle multiple events. += this += that..

 

Public Class MainForm
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles 
                                                        Button1.DoubleClick, 
                                                        Button1.Click
    End Sub
End Class

 

 

0 Likes
Message 3 of 17

T52K-BIB
Enthusiast
Enthusiast

@RPTHOMAS108  thanks, I'm in the learning stage on C#  now, So I also asked the question in StackOverflow but yet not get an answer. then change the MouseClick event instead of the Click event but still the same problem.

 

 

this.searchButton.MouseClick += new System.Windows.Forms.MouseEventHandler(this.searchButton_Click);
0 Likes
Message 4 of 17

T52K-BIB
Enthusiast
Enthusiast

@RPTHOMAS108 I check button Properties and i assign MouseEnter value now it's working.

thiru2jack_0-1613073449718.png

 

 

0 Likes
Message 5 of 17

RPTHOMAS108
Mentor
Mentor

Humm that doesn't sound like the right event but if it works it works.

 

System.Windows.Forms.Control.MouseEnter:

"Occurs when the mouse pointer enters the control."

 

i.e. the cursor moves into the region of the control, that is how I understood it. Your issue may be related to the button not having focus when you first click on it (takes two clicks). Mouse enter would bring it to focus but you still need mouse click. Other buttons on the form could have the same issue. There may be other ways of ensuring your control automatically has focus but would need to see the code in relation to how it is shown.

0 Likes
Message 6 of 17

T52K-BIB
Enthusiast
Enthusiast

@RPTHOMAS108 😊  yes, your right focus is not having value.

thanks for your valuable reply. still, I'm not sure is that correct? 

 

thiru2jack_0-1613119209967.png

 

0 Likes
Message 7 of 17

michael-coffey
Advocate
Advocate

@T52K-BIB   Don't use Focus or MouseClick, use Click event.

0 Likes
Message 8 of 17

T52K-BIB
Enthusiast
Enthusiast

@michael-coffey  thanks for the reply, apologies for my delay,

Already the click event is assigned as searchButton_Click but it is not working.

 

thiru2jack_0-1613503589864.png

below is the from and designer code.

 

  
// Form
  private void searchButton_Click(object sender, EventArgs e) 
        {

                rnumber = roomNameBox.Text;
                searchButton.DialogResult = DialogResult.OK;
                Debug.WriteLine("Search Clicked");

        }

//form Design

      this.searchButton.Cursor = System.Windows.Forms.Cursors.Hand;
            this.searchButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.searchButton.Location = new System.Drawing.Point(106, 144);
            this.searchButton.Name = "searchButton";
            this.searchButton.Size = new System.Drawing.Size(97, 31);
            this.searchButton.TabIndex = 0;
            this.searchButton.Text = "Search";
            this.searchButton.UseVisualStyleBackColor = true;
            this.searchButton.Click += new System.EventHandler(this.searchButton_Click);

 

 

0 Likes
Message 9 of 17

RPTHOMAS108
Mentor
Mentor

You need to resolve how your form is loosing focus, how are you showing it?

 

What does your implementation of IExternalCommand look like?

0 Likes
Message 10 of 17

michael-coffey
Advocate
Advocate

When you say it "doesn't work", what are you expecting to happen?  It doesn't look like its going to do much. 

0 Likes
Message 11 of 17

T52K-BIB
Enthusiast
Enthusiast

@michael-coffey  Code is Working. only the problem is button does not work in a single click while fixing the focus properties which is working on a single click. 

0 Likes
Message 12 of 17

T52K-BIB
Enthusiast
Enthusiast

my IExternalCommand will implement below image

 

Room Search.gif

@RPTHOMAS108  From the Floor / Ceiling Plan view to find a ROOM in a Large scale Project is too hard and only the option goes to schedule and highlight. so that I make this add-in for my own purpose.

0 Likes
Message 13 of 17

RPTHOMAS108
Mentor
Mentor
Accepted solution

Your button is highlighting with mouse over so it is nothing to do with focus.

 

I've noticed that in the button click event handler you have the following line (and cancel for cancel):

searchButton.DialogResult = DialogResult.OK;

Note that this is not the location to set this if you are using ShowDialog to get an OK result.

 

The above should be set in the properties of the control as below (will then be part of designer generated code). The effect of setting it in the handler is that the first time you click the button there is no result associated with it so OK will not be returned by ShowDialog (result is likely recorded prior to entering handler). The second time you press the button the control will have the result associated with it (from previous click) thus will work second time. This is not double click behaviour because double click behaviour requires quick succession of clicks.

 

210221.PNG  

 

I hope this is the answer, if not you need to show the IExternalCommand implementation (in terms of code). You don't need to associate cancel with any button really since cancel is done in many different ways (x button etc.). Should only look for an ok result in order to know something needs to be done. Any other form of closing the window is cancel by default if nothing further is done (just need to ensure window is hidden in response).

 

I can't replicate it myself.

0 Likes
Message 14 of 17

RPTHOMAS108
Mentor
Mentor

I've now replicated the behaviour so it is likely what I note above.

 

I don't use this dialogue result mechanism personally I like to set a property on the form prior to close and then manually close. Then I read that property externally. This can be more informative than just ok or cancel i.e. you can have multiple buttons to do different things none of which being ok, cancel, yes, no... etc.

0 Likes
Message 15 of 17

T52K-BIB
Enthusiast
Enthusiast

 

@RPTHOMAS108  IExternalCommand

namespace RevitAddin3
{
    [Transaction(TransactionMode.Manual)]
    public class Command : IExternalCommand
    {
        public Result Execute(
          ExternalCommandData commandData,
          ref string message,
          ElementSet elements)
        {
            UIApplication uiapp = commandData.Application;
            UIDocument uidoc = uiapp.ActiveUIDocument;
            Application app = uiapp.Application;
            Document doc = uidoc.Document;

          //get input from form

            Form1 form = new Form1(commandData);
            form.ShowDialog();

            string number = form.rnumber;

           
            // get Room Element 
            RoomFilter filter = new RoomFilter();
            FilteredElementCollector collector = new FilteredElementCollector(doc);
            IList<Element> rooms = collector.WherePasses(filter).ToElements();

            // Get the active view of the current document.
            Autodesk.Revit.DB.View view = doc.ActiveView;

            if (view is Autodesk.Revit.DB.ViewPlan || view is Autodesk.Revit.DB.ViewSection )
            {
         
                    foreach (Element ele in rooms)

                    {

                        String roomnumber = ele.get_Parameter(BuiltInParameter.ROOM_NUMBER).AsString();
                        if (roomnumber == number)
                        {

                            uidoc.ShowElements(ele);
                             return Result.Succeeded;

                    }
                    }
                TaskDialog.Show("Warning", "Invaild Room Number");
            }
            else
            {
                TaskDialog.Show("View", "Please make Sure View");
                return Result.Failed;
            }
               return Result.Succeeded;
        }
    }
}

 

0 Likes
Message 16 of 17

T52K-BIB
Enthusiast
Enthusiast

I assign the dialogueresult as OK for the Search button property which working and I check with the form designer code additionally the below line is added.

 

 

                       this.searchButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            

 

 

 

 

from Search button form Designer 

  this.searchButton.Cursor = System.Windows.Forms.Cursors.Hand;
            this.searchButton.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.searchButton.Font = new System.Drawing.Font("Microsoft Sans Serif", 10F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.searchButton.Location = new System.Drawing.Point(106, 144);
            this.searchButton.Name = "searchButton";
            this.searchButton.Size = new System.Drawing.Size(97, 31);
            this.searchButton.TabIndex = 0;
            this.searchButton.Text = "Search";
            this.searchButton.UseVisualStyleBackColor = true;
            this.searchButton.Click += new System.EventHandler(this.searchButton_Click);

 

 

once again thanks and I need to learn a lot of things deeply in C#

0 Likes
Message 17 of 17

RPTHOMAS108
Mentor
Mentor

The way you have it at the moment both buttons will do the same. A typical modal form design pattern would be as follows. ShowDialog returns a result and depending on what that is you do something.

 

The subtle thing is that if the Button has a dialog result associated with it then pressing the button will close the form if this isn't the case (as you originally had it) you need to close form as part of Button.Click event handler. For your purposes (using dialog result mechanism) you don't need to add such handlers.

 

 [Autodesk.Revit.Attributes.Regeneration(Autodesk.Revit.Attributes.RegenerationOption.Manual)]
    [Autodesk.Revit.Attributes.Transaction(Autodesk.Revit.Attributes.TransactionMode.Manual)]
    class ExtryPoint1 : Autodesk.Revit.UI.IExternalCommand
    {
        Result IExternalCommand.Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
        {
            Form1 MainForm = new Form1();

            if (MainForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                //Do work as OK result returned
            }
            else
            {
                //Nothing to do
            }

            return Result.Succeeded;
        }
    }

 

 

 

 

 

 

 

0 Likes