10.06.2014., 14:30
|
#1
|
Registered User
Datum registracije: Apr 2014
Lokacija: Split
Postovi: 8
|
C# MDI tekstualni editor
Pozz ekipa !
Radim MDI tekstualni editor i trebam napraviti find dialog box za pronalaženje stringa. Form1 mi ima toolbar, a Form2 richtextbox. Find dialog box sam riješio tako da sam dodao novu formu Form3. Međutim program mi uvijek vraća da string nije pronađen. Evo kod pa ako neko zna ne bi škodila pomoć
Form1
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Windows;
using Microsoft.Win32;
namespace proba_4
{
public partial class Form1 : Form
{
String path = "";
int broj = 1;
public Form1()
{
InitializeComponent();
}
public Form1(string text)
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.MdiParent = this;
f2.Text = "bilješka" + broj;
f2.Show();
broj++;
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.Title = "Open";
openFileDialog1.Filter = "Text Files(*.txt)|*.txt|All Files(*.*)|*.*";
DialogResult saveResult = openFileDialog1.ShowDialog();
if (saveResult == DialogResult.OK)
{
try
{
Form2 f2 = new Form2(File.ReadAllText(openFileDialog1.FileName));
f2.Text = openFileDialog1.SafeFileName;
f2.MdiParent = this;
f2.Show();
}
catch (IOException ioe)
{
MessageBox.Show("Error opening file:" + ioe.Message);
}
}
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = this.ActiveMdiChild as Form2;
if (path == String.Empty)
{
DialogResult saveResult = saveFileDialog1.ShowDialog();
if (saveResult == DialogResult.OK)
{
path = saveFileDialog1.FileName;
try
{
StreamWriter sw = new StreamWriter(path);
sw.Write(f2.richTextBox1.Text);
sw.Close();
}
catch (IOException ioe)
{
MessageBox.Show("Error saving file:" + ioe.Message);
}
}
}
else
{
try
{
StreamWriter sw = new StreamWriter(path);
sw.Write(f2.richTextBox1.Text);
sw.Close();
}
catch (IOException ioe)
{
MessageBox.Show("Error saving file:" + ioe.Message);
}
}
}
public void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = this.ActiveMdiChild as Form2;
if (f2 != null)
{
DialogResult saveResult = saveFileDialog1.ShowDialog();
if (saveResult == DialogResult.OK)
{
path = saveFileDialog1.FileName;
try
{
StreamWriter sw = new StreamWriter(path);
sw.Write(f2.richTextBox1.Text);
sw.Close();
}
catch(IOException ioe)
{
MessageBox.Show("Error saving file:" + ioe.Message);
}
}
}
}
private void fontToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 f2 = this.ActiveMdiChild as Form2;
if (f2 != null)
{
DialogResult saveResult = fontDialog1.ShowDialog();
if (saveResult == DialogResult.OK)
{
try
{
f2.richTextBox1.Font = fontDialog1.Font;
}
catch(IOException ioe)
{
MessageBox.Show("Error changing font" + ioe.Message);
}
}
}
}
private void findToolStripMenuItem_Click(object sender, EventArgs e)
{
Form3 find = new Form3();
find.Show(this);
}
}
}
Form2
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace proba_4
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(string text)
{
InitializeComponent();
richTextBox1.Text = text;
}
}
}
Form3
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace proba_4
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
int StartPosition;
StartPosition = f2.richTextBox1.Text.IndexOf(this.richTextBox1.Text);
string StartPosition1 = Convert.ToString(StartPosition);
MessageBox.Show(StartPosition1);
if (StartPosition == -1)
{
MessageBox.Show("String: " + this.richTextBox1.Text.ToString() + " not found", "No Matches",
MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
return;
}
f2.richTextBox1.Select(StartPosition, f2.richTextBox1.Text.Length);
f2.richTextBox1.ScrollToCaret();
f2.Focus();
}
}
}
|
|
|