473,324 Members | 2,257 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.
Apr 1 '10 #1

✓ answered by samsanjay

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.Owner;

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

Thanks a lot..

11 3111
Christian Binder
218 Expert 100+
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:
Expand|Select|Wrap|Line Numbers
  1. textBox1.SelectionStart = index; //index is int
  2. textBox1.SelectionLength = length; //length is int, lengthof string to be searched for
  3.  
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
Apr 1 '10 #2
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(object sender, EventArgs e)
{
this.index=holdText.IndexOf(textBox1.Text);
if (index >= 0)
{
this.length = textBox1.Text.Length;
TextFound += new TextFoundEventHandler(n.dlg_TextFound);
TextFound(this,EventArgs.Empty);
}

}

TextFound method in Mainform:

public void dlg_TextFound(object sender, EventArgs e)
{
FindDialog dlg = (FindDialog)sender;
richTextBox1.Focus();
richTextBox1.Select(dlg.index, dlg.length);
richTextBox1.Focus();
}
Apr 1 '10 #3
Christian Binder
218 Expert 100+
RichTextBox also has the property HideSelection. Set it to False (in designer) and it should work.
Apr 1 '10 #4
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?
Apr 1 '10 #5
Christian Binder
218 Expert 100+
Set a breakpoint here
Expand|Select|Wrap|Line Numbers
  1. public void dlg_TextFound(object sender, EventArgs e)
  2. {
  3.   FindDialog dlg = (FindDialog)sender;
  4.   richTextBox1.Focus();
  5.   richTextBox1.Select(dlg.index, dlg.length); // SET BREAKPOINT!
  6.   richTextBox1.Focus();
  7. }
  8.  
Does your program break here, does dlg.index and dlg.length have right values? Maybe the event-handler is not working correctly?
Apr 1 '10 #6
sorry to disturb u......

yes. the program breaks there and the values are passed correctly... still the text is not selected in that richTextBox.
Apr 1 '10 #7
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.Owner;

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

Thanks a lot..
Apr 1 '10 #8
Christian Binder
218 Expert 100+
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:

Expand|Select|Wrap|Line Numbers
  1. private void find_Click(object sender, EventArgs e)
  2. {
  3.   this.index=holdText.IndexOf(textBox1.Text);
  4.   if (index >= 0)
  5.   {
  6.     this.length = textBox1.Text.Length; 
  7.     TextFound += new TextFoundEventHandler(n.dlg_TextFound); //Attaching to event-handler
  8.     TextFound(this,EventArgs.Empty);
  9.   }
  10. }
  11.  
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(...).
Apr 1 '10 #9
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..
Apr 1 '10 #10
Christian Binder
218 Expert 100+
Code of Main-Form (Notepad)
Expand|Select|Wrap|Line Numbers
  1. // ...
  2.  
  3. void ShowFindForm() {
  4. //this method is called when you click on find-button or symbol in main-form
  5.  
  6.   FindForm findForm  = new FindForm();
  7.   _findForm.TextFound += new TextFoundEventHandler(dlg_TextFound);
  8.   _findForm.Show(this);
  9. }
  10.  
  11.  
Code in FindForm
Expand|Select|Wrap|Line Numbers
  1. private void find_Click(object sender, EventArgs e)
  2. {
  3.   this.index=holdText.IndexOf(textBox1.Text);
  4.   if (index >= 0)
  5.   {
  6.     this.length = textBox1.Text.Length; 
  7.     //no attaching here ...
  8.  
  9.     if(TextFound != null) //check if there are subscribers to this event
  10.       TextFound(this,EventArgs.Empty);
  11.   }
  12. }
  13.  
The event TextFound must be accessible (keyword public or internal) out of main-form, also the TextFoundEventHandler must be.
Apr 1 '10 #11
I have incorporated the changes as u suggested...
Thank you very much ChBinder..
Apr 1 '10 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Nagachandra Sekhar Grandhi | last post by:
Hi, I am opening a dialog box (which is also a form) after clicking a button in the main form using ".ShowDialog(this)". There is a minimize button for the opened dialog box(form). I want to...
2
by: John Tyce | last post by:
When a button is clicked, a date is inserted or added into a combo box like this : ComboBox.Items.Add(string) or ComboBox.Items.Insert(0,string); Either way, the new string does not show up in the...
1
by: andrew | last post by:
Hi there, I'm having a problem with a modeless form in my app. I have a main form in my app and a socket that waits on data from a server (I use BeginReceive/EndReceive for that) and when I...
4
by: Steve | last post by:
I have the MDI MFC application ported to .NET. Now this application include mixed managed/unmanaged code. The application displays progress dialog with the cancel button during lenghtly...
11
by: Steve Cutting | last post by:
Hi all, Using the toolbox I added a tooltip provider to my form, and set the text for each of my buttons using the properties window. When I first show the form using .showdialog the tooltips...
2
by: proit_123 | last post by:
I am working on a windows forms application and have the following requirement. · I need to display a modeless dialog from the main form. o This allows user to continue to work with the...
11
by: Zytan | last post by:
I have created a new form from the main form. When I close the main form with the 'x' close button, its Form.FormClosed event is run, but not the dialog's. Is this normal? It is ok /...
5
by: GraffixNYC | last post by:
We have a model dialog box(well a bunch of them) and it seems that when you cancel one of the in our application it will activate and bring the focus to another open window (not in our application)...
1
by: sowen | last post by:
In IE, I am using showModalDialog to pop up a modeless window, then the object "opener" is undefined. well, I pass the parent window object as an argument to pop up, then I can redirect the...
5
by: Andrus | last post by:
Steps to reproduce issue: 1. Run code. 2. Enter some data to grid 3. Click other form caption 4. Click original form caption 5. Enter some characters Observed: entered characters are ignored
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.