Cannot get focus on the parent form while clicking button in modeless dialog in C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • samsanjay
    New Member
    • Apr 2010
    • 8

    Cannot get focus on the parent form while clicking button in modeless dialog in C#

    Hi everyone,

    i m a beginner in c#. i m trying to develop a notepad editor application in c# using windows forms.. i have created a find modeless dialog. when i click the find button, it is able to find the index of the text but the text in the main form is not getting highlighted or selected.. pls help me how to do this.
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    How did you select text in you main form? What control did you use?
    If you're using a TextBox, you can select text that way:
    Code:
    textBox1.SelectionStart = index; //index is int
    textBox1.SelectionLength = length; //length is int, lengthof string to be searched for
    Also you have to care of the TextBox's property HideSelection, if it's true, the selection will be discarded if the control (form) looses focus

    Comment

    • samsanjay
      New Member
      • Apr 2010
      • 8

      #3
      thanks ChBinder..

      But i hav used a richtextbox in mainform.
      In my find dialog form, once the find button is clicked, it finds the text index and passes it thro an event handler that invokes the TextFound method in mainform.

      Finddialog code:

      Notepad n = new Notepad();
      private void find_Click(obje ct sender, EventArgs e)
      {
      this.index=hold Text.IndexOf(te xtBox1.Text);
      if (index >= 0)
      {
      this.length = textBox1.Text.L ength;
      TextFound += new TextFoundEventH andler(n.dlg_Te xtFound);
      TextFound(this, EventArgs.Empty );
      }

      }

      TextFound method in Mainform:

      public void dlg_TextFound(o bject sender, EventArgs e)
      {
      FindDialog dlg = (FindDialog)sen der;
      richTextBox1.Fo cus();
      richTextBox1.Se lect(dlg.index, dlg.length);
      richTextBox1.Fo cus();
      }

      Comment

      • Christian Binder
        Recognized Expert New Member
        • Jan 2008
        • 218

        #4
        RichTextBox also has the property HideSelection. Set it to False (in designer) and it should work.

        Comment

        • samsanjay
          New Member
          • Apr 2010
          • 8

          #5
          I have set it to false, even its not working..

          I am creating a new instance of the parent form and calling the method in the find dialog. Do that cause any problem here?

          Comment

          • Christian Binder
            Recognized Expert New Member
            • Jan 2008
            • 218

            #6
            Set a breakpoint here
            Code:
            public void dlg_TextFound(object sender, EventArgs e)
            {
              FindDialog dlg = (FindDialog)sender;
              richTextBox1.Focus();
              richTextBox1.Select(dlg.index, dlg.length); // SET BREAKPOINT!
              richTextBox1.Focus();
            }
            Does your program break here, does dlg.index and dlg.length have right values? Maybe the event-handler is not working correctly?

            Comment

            • samsanjay
              New Member
              • Apr 2010
              • 8

              #7
              sorry to disturb u......

              yes. the program breaks there and the values are passed correctly... still the text is not selected in that richTextBox.

              Comment

              • samsanjay
                New Member
                • Apr 2010
                • 8

                #8
                hi chbinder...
                thanks for ur concern... i got the solution..
                As i said before, the instance of the parent form created in the finddialog created the problem.
                Now i have used the owner property of finddialog .

                In Main form, finddialog.show (this);

                In finddialog, Notepad n = (Notepad)this.O wner;

                All other codes are the same...Now the text is getting selected..

                Thanks a lot..

                Comment

                • Christian Binder
                  Recognized Expert New Member
                  • Jan 2008
                  • 218

                  #9
                  Nice that you've got the solution.
                  I haven't seen this mistake, but it's right, you've created a new Notepad and made the selection there and not in the originial (parent/owener) Notepad.

                  One more thing to say:

                  Code:
                  private void find_Click(object sender, EventArgs e)
                  {
                    this.index=holdText.IndexOf(textBox1.Text);
                    if (index >= 0)
                    {
                      this.length = textBox1.Text.Length; 
                      TextFound += new TextFoundEventHandler(n.dlg_TextFound); //Attaching to event-handler
                      TextFound(this,EventArgs.Empty);
                    }
                  }
                  You do multiple attachments to this event-handler. This should not be necessary. It's better to attach to this event-Handler in Notepad-class and do this only once. Otherwise you'd call n.dlg_TextFound multiple times each time you do a click on Search-button and then calling TextFound(...).

                  Comment

                  • samsanjay
                    New Member
                    • Apr 2010
                    • 8

                    #10
                    I have created both the delegate and event handler in finddialog onlly. so how can i attach the eventhandler in Notepad class...

                    sorry i unable to get wat u say..
                    could u please explain it..

                    Comment

                    • Christian Binder
                      Recognized Expert New Member
                      • Jan 2008
                      • 218

                      #11
                      Code of Main-Form (Notepad)
                      Code:
                      // ...
                      
                      void ShowFindForm() {
                      //this method is called when you click on find-button or symbol in main-form
                      
                        FindForm findForm  = new FindForm();
                        _findForm.TextFound += new TextFoundEventHandler(dlg_TextFound);
                        _findForm.Show(this);
                      }
                      Code in FindForm
                      Code:
                      private void find_Click(object sender, EventArgs e)
                      {
                        this.index=holdText.IndexOf(textBox1.Text);
                        if (index >= 0)
                        {
                          this.length = textBox1.Text.Length; 
                          //no attaching here ...
                      
                          if(TextFound != null) //check if there are subscribers to this event
                            TextFound(this,EventArgs.Empty);
                        }
                      }
                      The event TextFound must be accessible (keyword public or internal) out of main-form, also the TextFoundEventH andler must be.

                      Comment

                      • samsanjay
                        New Member
                        • Apr 2010
                        • 8

                        #12
                        I have incorporated the changes as u suggested...
                        Thank you very much ChBinder..

                        Comment

                        Working...