PC Ekspert Forum

PC Ekspert Forum (https://forum.pcekspert.com/index.php)
-   Web dizajn, programiranje i ostalo (https://forum.pcekspert.com/forumdisplay.php?f=39)
-   -   C# MDI tekstualni editor (https://forum.pcekspert.com/showthread.php?t=260303)

Falcon11 10.06.2014. 14:30

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();
        }
    }
}


Ahura 13.06.2014. 20:42

Pozdrav,

u formi3 na klik gumba stvaraš novu formu2, te onda od te nove forme pretražuješ rtb za tekst.
Pošto je tek stvorena ona za tekst u rtb nema ništa.

Ti bi trebao pretražiti formu2 stvorenu na formi1 putem klika, tako da bi
Formi3 bi trebo adati referencu na formu koju treba pretražiti pošto kasnije očito želiš radit nešto sa njom.
A čini mi se da bi možda htjeo predati listu svih formi2 stvorenih.

Javni ako nije jasno za detaljnije pojašnjenje.


Sva vremena su GMT +2. Sada je 15:43.

Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
© 1999-2024 PC Ekspert - Sva prava pridržana ISSN 1334-2940
Ad Management by RedTyger