3 Replies - 1886 Views - Last Post: 10 March 2009 - 01:23 PM Rate Topic: -----

#1 versatile36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 02-October 08

Problem in implementing FIND module in a notepad

Posted 08 March 2009 - 04:04 AM

Hi,

I preparing a small notepad application,I have a problem is implementing FIND module.

I named the main notepad form as MainForm and Find dialog as FindForm, iam importing the mainform object into find form using parameter constructor and iam trying to access that object to search the word but not working. Iam i doing anything wrong or any semantic problem .Please guide me.

	public partial class FindForm : Form
	{
	   MainForm mForm;
	   public FindForm()
	   {
		   InitializeComponent();
	   }

		public FindForm(MainForm mfr)
		{
			InitializeComponent();
			mForm = mfr;	 
		}

		private void FindBtn_Click(object sender, EventArgs e)
		{
			try
			{
				int Position;
				StringComparison searchType;
				if (MatchCaseChkBx.Checked == true)
				{
					searchType = StringComparison.Ordinal;
				}
				else
				{
					searchType = StringComparison.OrdinalIgnoreCase;
				}
			 
  Position=  

/*here i want to write "Position=mForm.richTextBox1. ...." but the refelection window is not showing the richtextbox1 object after mForm. please help me out ..*/
			  
			}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in implementing FIND module in a notepad

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1632
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Problem in implementing FIND module in a notepad

Posted 08 March 2009 - 09:00 AM

Here's the way I accomplished the same thing. In my main form I cann my find & replace form like so (probably the same way you do)

private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
	try
	{
		FindAndReplace find = new FindAndReplace(this);
		find.ShowDialog();
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message);
	}
}



I also made the modifiers on the RichTextBox on Form1 (my main form) public so I can access them on a different form. In my constructor on the find & replace form looks like this (like you do)

public FindAndReplace(Form1 f)
{
	InitializeComponent();
	frm = f;
}



Now we will see why I made them public, my Find method takes the text we're looking for, a bool value of matchCase and a RichTextBox as parameters. My find method looks like

/// <summary>
/// method for searching for specified text in a RichTextBox
/// </summary>
/// <param name="text">text we're looking for</param>
/// <param name="matchCase">match case or not?</param>
/// <param name="rtb">RichTextBox we're searching for</param>
public static void Find(string text, bool matchCase, RichTextBox rtb)
{
	try
	{
		StringComparison type;

		type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;

		int startPos = rtb.Text.IndexOf(text, type);

		if (!(startPos > 0))
		{
			MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			return;
		}
		else
		{
			rtb.Select(startPos, text.Length);
			rtb.ScrollToCaret();
			rtb.Focus();
		}
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message, "Search Error");
	}
}



Maybe that will help some. And sicne you're making a Notepad application I'll also share with you the method I use for Find Next

public static void FindNext(string text, bool matchCase, RichTextBox rtb)
{
	try
	{
		int startPos = rtb.Selectionstart + 2;
		StringComparison type;

		type = matchCase == true ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase;
		startPos = rtb.Text.IndexOf(text, startPos, type);

		if (startPos == 0 || startPos < 0)
		{
			MessageBox.Show("Search text: '" + text + "' could not be found", "Text Not Found", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
			return;
		}
		else
		{
			rtb.Select(startPos, text.Length);
			rtb.ScrollToCaret();
			rtb.Focus();
		}
	}
	catch (Exception ex)
	{
		MessageBox.Show(ex.Message, "Search Error");
	}
}



Hope some of that halps :)
Was This Post Helpful? 0
  • +
  • -

#3 versatile36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 02-October 08

Re: Problem in implementing FIND module in a notepad

Posted 10 March 2009 - 12:40 PM

Thanks for the reply PsychoCoder ...i will try to implement and let you know the result :)
Was This Post Helpful? 0
  • +
  • -

#4 versatile36  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 32
  • Joined: 02-October 08

Re: Problem in implementing FIND module in a notepad

Posted 10 March 2009 - 01:23 PM

Even though i change the Form1 controls to public/internal iam not able to access in the FINDFORM ...:(
should i change any where else?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1