Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,412 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 2,454 people online right now. Registration is fast and FREE... Join Now!




Find Dialogue

 
Reply to this topicStart new topic

Find Dialogue

wingot
15 Oct, 2008 - 05:34 AM
Post #1

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 38

Hey guys,

Just trying to create a Find Dialogue for my application. The code for when the user clicks the Edit->Find menu option so far looks like:

CODE

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Find FindDialogue = new Find();
            FindDialogue.Show(this);
        }


Find (Which is just another form) has a public string property named TextToFind, and if I am just using Find as a ShowDialog (so that Find can't lose the focus), then FindDialogue.TextToFind is set to the value in the textbox. The (working) code for this is:

CODE

        private void findToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Find FindDialogue = new Find();
            FindDialogue.ShowDialog(this);

            MessageBox.Show(FindDialogue.TextToFind);
        }


This code displays in a messagebox the text entered as expected.

But if I just use Show as displayed originally (and as I want to), then how do I determine when the Find buton has actually been pressed? Do I need to use a Delegate/Event that triggers the primary form to do the search? That is what I'm thinking is required, but would like to confirm that I'm not just overcomplicating the issue?

Thanks for your help guys.

EDIT: I have already allocated DialogResult's to the various buttons in the form, but for the modeless version I still think the event/delegate is required before these would be of any value.

This post has been edited by wingot: 15 Oct, 2008 - 05:35 AM
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Find Dialogue
15 Oct, 2008 - 05:47 AM
Post #2

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Take a look at PsycoCoder's snippet:
http://www.dreamincode.net/code/snippet2466.htm


First go to your find form and select the button that will be the OK button. Scroll down the Properties to DialogResult. Set it to OK. Then enter this code in your Main Form

csharp

Find FindDialogue = new Find();
if(FindDialogue.ShowDialog(this) == DialogResult.OK)
{
//TODO Code
}


This post has been edited by gbertoli3: 15 Oct, 2008 - 05:50 AM
User is offlineProfile CardPM
+Quote Post

wingot
RE: Find Dialogue
15 Oct, 2008 - 07:25 AM
Post #3

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 38

QUOTE(gbertoli3 @ 15 Oct, 2008 - 06:47 AM) *

First go to your find form and select the button that will be the OK button. Scroll down the Properties to DialogResult. Set it to OK. Then enter this code in your Main Form


Hey,

Yeah, that code would work, but as stated I specifically want to use Show and not ShowDialog. ShowDialog is tested and working, but for a find dialogue (and probably others in my applications) I would like for it to be modeless, hence the need for Show (and implicitly why testing for the DialogResult will not work).

In fact, I even posted code showing that I had already done the ShowDialog method and that it works, but isn't modeless smile.gif.

EDIT: BTW, the link you provided looks handy for the actual find functionality, although I was looking at doing a regex search as it is much faster from testing. Will of course do a more exhaustive test before forming a definite opinion though (with varying length inputs/search terms as well, especially since from my understanding regex becomes much quicker than IndexOf for long strings). The part in the else looks very handy. Will take a closer look tomorrow.

This post has been edited by wingot: 15 Oct, 2008 - 07:29 AM
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Find Dialogue
15 Oct, 2008 - 02:31 PM
Post #4

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
Why don't you just add the functionality to the Find Dialog instead of the Main Form, It would be much easier.
Like So:
csharp

//FindDialogue Form Code

private void findButton_Click(object sender, EventArgs e)
{
//Find Code Here
}

//Main Form Code
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
FindDialogue find = new FindDialogue();
find.Show();
}


Hope this helps
User is offlineProfile CardPM
+Quote Post

wingot
RE: Find Dialogue
15 Oct, 2008 - 05:46 PM
Post #5

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 38

QUOTE(gbertoli3 @ 15 Oct, 2008 - 03:31 PM) *

Why don't you just add the functionality to the Find Dialog instead of the Main Form, It would be much easier.
Like So:
csharp

//FindDialogue Form Code

private void findButton_Click(object sender, EventArgs e)
{
//Find Code Here
}

//Main Form Code
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
FindDialogue find = new FindDialogue();
find.Show();
}


Hope this helps


Hmm, good point. However, how would I reference the "calling form" within the Find Button code? The Find Button code should not have the main forms details hard coded into it.

/me hunts around

Looks like I already have the solution worked into the code. I'm calling Find with Find.Show(this), so the owner should be set to the value of the main form. However, how do I tell it which text box to modify while still remaining decoupled? I'll probably need to add a property that is a reference to the text box. Or is there a simpler way?
User is offlineProfile CardPM
+Quote Post

gbertoli3
RE: Find Dialogue
15 Oct, 2008 - 05:55 PM
Post #6

DIC at Heart + Code
Group Icon

Joined: 23 Jun, 2008
Posts: 1,046



Thanked: 17 times
Dream Kudos: 950
My Contributions
You could set all of the Modifier Properties for all of the TextBoxes on the Find Form to Public. Then they would be visible through the calling of another form.
User is offlineProfile CardPM
+Quote Post

wingot
RE: Find Dialogue
15 Oct, 2008 - 07:18 PM
Post #7

New D.I.C Head
*

Joined: 13 Oct, 2008
Posts: 38

QUOTE(gbertoli3 @ 15 Oct, 2008 - 06:55 PM) *

You could set all of the Modifier Properties for all of the TextBoxes on the Find Form to Public. Then they would be visible through the calling of another form.


Actually, I meant the other way around (from what I understand you're saying make the textboxes on the find form public so that the main form can reference them and do the find itself, which extremely couples the main form to Find, which is really bad practice). From the Find dialogue, using the owner reference to reference the calling form, how do I reference the textbox on the main form (the owner)? I'm thinking that I probably need to create a property in the Find dialogue called "textBoxToSearch" or something with a setter which takes a ref to the text box as a parameter in order to set the textbox to search and do the various functions of setting the cursor/selection parts of the code. It sounds wordy, but from a code point of view isn't particularly complex, but even still I'm wondering whether I'm needlessly complicating the issue.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:29PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month