Jump to content

Need help for first project

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
19 replies to this topic

#1
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
Hey everyone, I'm quite new to this forum and thanks to some members including WingedPanther and Brandon W., I've been interesting in CSharp code. Now I have a little question on how to do something.

I'm currently working on a really advanced notepad. I've read the tutorial made by Xav I think on how to make a simple notepad and want to know how to add a feature. I have everything all set-up, design wise. So I was wondering how to add a "Find" function for the notepad where it could find the specific text in the body of the written. Something like that of the normal Notepad that comes with Windows. Also if anyone can, please show me how to add a "Replace" and "Go-To" feature. I would be really happy :). I might be asking for a lot but I really just need a explanation of how it works and I can then create it on my own.

#2
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
I'm not quite sure if this will help you but I found this code here;
How to: Search for Text in Documents
private void SelectionFind() 
{ 
    object findText = "find me";

    Application.Selection.Find.ClearFormatting();

    if (Application.Selection.Find.Execute(ref findText,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing)) 
    { 
        MessageBox.Show("Text found.");
    } 
    else
    { 
        MessageBox.Show("The text could not be located.");
    } 
}

All you would have to change is the MessageBox.Show function to whatever you want. Maybe you could have it highlight the text?

Wait, this is even better since it has Search and Replace;
private void SearchReplace() 
{ 
    object replaceAll = Word.WdReplace.wdReplaceAll; 

    Application.Selection.Find.ClearFormatting(); 
    Application.Selection.Find.Text = "find me"; 

    Application.Selection.Find.Replacement.ClearFormatting(); 
    Application.Selection.Find.Replacement.Text = "Found"; 

    Application.Selection.Find.Execute(
        ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
Link: How to: Search for and Replace Text in Documents

There you actually have search and then another one as search and replace. For the search, as I mentioned all you would have to do is maybe highlight the text and get it to go there. This link should help you with that too;
.NET - Search and Highlight Text in a RichTextBox

Just read through those links and you should be right :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#3
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
Wow man, +REP for that. Your really helpful XD. Anyways, I followed the second link and what I did was on the design side put a Find and Replace button then double clicked that for the code entry. In between the...

EDIT: Sorry, look below, I did something wrong.

Edited by Nickalbokid96, 22 July 2009 - 12:09 AM.


#4
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Wouldn't you when you click on the Replace button it opens up with a menu? Also the code would have to go in the right places, by the looks of it you need to define some variables and that too.
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#5
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
Ok, I re-did this and when I add the code I get these errors
Error	1	The name 'Word' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	134	25	MyNotePad
Error	2	'System.Windows.Forms.Application' does not contain a definition for 'Selection'	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	136	17	MyNotePad
Error	3	'System.Windows.Forms.Application' does not contain a definition for 'Selection'	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	137	17	MyNotePad
Error	4	'System.Windows.Forms.Application' does not contain a definition for 'Selection'	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	139	17	MyNotePad
Error	5	'System.Windows.Forms.Application' does not contain a definition for 'Selection'	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	140	17	MyNotePad
Error	6	'System.Windows.Forms.Application' does not contain a definition for 'Selection'	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	142	17	MyNotePad
Error	7	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	143	13	MyNotePad
Error	8	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	143	26	MyNotePad
Error	9	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	143	39	MyNotePad
Error	10	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	143	52	MyNotePad
Error	11	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	143	65	MyNotePad
Error	12	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	144	13	MyNotePad
Error	13	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	144	26	MyNotePad
Error	14	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	144	39	MyNotePad
Error	15	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	144	52	MyNotePad
Error	16	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	144	65	MyNotePad
Error	17	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	145	29	MyNotePad
Error	18	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	145	42	MyNotePad
Error	19	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	145	55	MyNotePad
Error	20	The name 'missing' does not exist in the current context	C:\Users\Nick\Documents\Visual Studio 2008\Projects\MyNotePad\MyNotePad\Form1.cs	145	68	MyNotePad


Heres my code
        private void Replace_Click(object sender, EventArgs e)
        {
            private void SearchReplace() 
{ 
    object replaceAll = Word.WdReplace.wdReplaceAll; 

    Application.Selection.Find.ClearFormatting(); 
    Application.Selection.Find.Text = "find me"; 

    Application.Selection.Find.Replacement.ClearFormatting(); 
    Application.Selection.Find.Replacement.Text = "Found"; 

    Application.Selection.Find.Execute(
        ref missing, ref missing, ref missing, ref missing, ref missing, 
        ref missing, ref missing, ref missing, ref missing, ref missing,
        ref replaceAll, ref missing, ref missing, ref missing, ref missing);
}
        }

    }
}

If someone could point out, what "The name 'missing' does not exist in the current context" means, that would be greatly appreciated or "'System.Windows.Forms.Application' does not contain a definition for 'Selection'
"
..I feel stupid =l

#6
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
I think I see what the problem is for the "ref missing" errors. Taking a lot at the functinon Application.Selection.Find.Execute, it seems to me that the parameters it takes are actually the text you wish to find. So what you must do is pass the user inputted value into this parameter. Does that make sense? If that works out, we shall work on the rest too :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#7
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
I read this topic here: Passing Parameters (C#) on how to Pass Value and Refs Parameter in C#. It's confusing though, I don't know what to do.

Edited by Nickalbokid96, 22 July 2009 - 10:00 AM.


#8
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
OK what you want to do is store the user input into a variable. The user input will what they want to search for so grab the value of that and store in a variable.

Then you would do something like this is IF the variable for user input was userInput;

Application.Selection.Find.Execute(userInPut)



#9
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
Ok I give up, I need to get a book, sit down and start studying. Honestly even if your giving me step-by-step instructions, I don't understand what some of these functions do. I'm going to forget about this for now I guess and study when I can... Any chance you have a msn, aim or anything we can chat over?

#10
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
OK mate, maybe you could make your own code then. I do have IM but I'm never on it, when I make a new one I'll send you a PM.

Care letting us know on your progress from learning C#? Maybe a blog about what you learn?
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!


#11
Nickalbokid96

Nickalbokid96

    Newbie

  • Members
  • PipPip
  • 22 posts
Thanks Brandon, your a good guy. I'll make a blog and blog after stuff I learn so that everyone can see my progress. Thanks for helping me man. :) Should I make a blog on the forum here, or rather my own blog for a free hoster since it will have more space for everything I learn? It would only take a few minutes for me to create a free blog.

By the way, do you recommend any books I should buy or read online? I'm just browsing a few of the Dummies.com ones lol

#12
Brandon W

Brandon W

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,828 posts
Your welcome mate :) I'm only here to help. Well it depends, if you want to make your own blog go ahead and just link us there from here. If you blog it directly here, you might get a bit more replies but even if it is at your own blog I still think you'll get quite a bit of replies :)
jQuery Selectors Tutorial - jQuery Striped Table tutorial - jQuery Events - jQuery Validation

Sorry if I don't post as often as I did, I'll try to get here as much as possible! I'm working my bum off to get this scholarship and other stuff!