문제

메모장을 만들고 있습니다. 찾기 및 교체 양식이 있습니다. 형태가 열리는 버튼을 클릭하면 사용자는 두 개의 텍스트 상자에 두 개의 입력을 제공하고 버튼을 누릅니다. 그런 다음 기본 양식의 RichTextBoxes가 수정되어야합니다.

다음은 다음과 같습니다 FindAndReplace 형태 :

private void btnReplaceAll_Click(object sender, EventArgs e)
        {
            string findMe = txtFind.Text;
            string replaceMe = txtReplace.Text;
            Form1 f1 = new Form1();
            f1.MainText.Replace(findMe, replaceMe);
            //this.Hide();
        }

문제는 작동하지 않는다는 것입니다. f1.MainText.Replace(findMe, replaceMe);아이디어가 있습니까?

도움이 되었습니까?

해결책

여기에서 양식의 새 인스턴스를 만듭니다.

Form1 f1 = new Form1();

모든 속성은 기본값 (예 : 문자열에서 null)으로 초기화됩니다. 다음으로 당신은 전화를 시도합니다 Replace 방법에 대한 방법 MainText 재산입니다 null 그리고 당신은 예외를 얻습니다 :

f1.MainText.Replace(findMe, replaceMe);

이 속성을 먼저 초기화해야합니다.

f1.MainText = "blablabla";
f1.MainText = f1.MainText.Replace(findMe, replaceMe);

업데이트:

FindAndReplace 양식을 작성하면 텍스트의 현재 값을 생성자에게 전달할 수 있습니다.

public class Form1 : Form
{
    protected void FindAndReplace_Click(object sender, EventArgs e) 
    {
        var findAndReplaceForm = new FindAndReplaceForm(MainText.Text);
        findAndReplaceForm.ShowDialog();
        MainText.Text = findAndReplaceForm.NewText;
    }
}

public class FindAndReplaceForm : Form
{
    private readonly string _originalText;

    public FindAndReplaceForm(string originalText)
    {
        _originalText = originalText;
    }

    public string NewText 
    { 
        get 
        {
            return (_originalText ?? string.Empty).Replace(findMe, replaceMe);
        }
    }
}

다른 팁

귀하의 찾기 및 교체 양식은 귀하의 주요 양식에 대해 알아야합니다. 당신이하는 방식으로, 당신은 완전히 새로운 메인 형식을 만들고 있으며, 여기에는 본문 영역에 텍스트가 없습니다. 찾기 및 교체 양식을 만들 때, 부모 양식, 또는 본문을 찾아서 교체 양식으로 전달한 다음 방금 전달 된 양식에서 기본 양식 텍스트를 검색해야합니다.

당신은 다음과 같은 것을 원합니다 :

public class FindAndReplaceForm
{
    private Form1 MainForm;

    public FindAndReplaceForm(Form1 parentForm)
    {
        this.MainForm = parentForm;
        //The rest of you constructor here
    }

    private void btnReplaceAll_Click(object sender, EventArgs e)
    {
        string findMe = txtFind.Text;
        string replaceMe = txtReplace.Text;

        //The following line will search the parent form
        this.MainForm.MainText.Replace(findMe, replaceMe);
        //this.Hide();
    }
}

프로그램 클래스에 정적 양식 참조를 추가 할 수 있습니다.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        public static Form1 F1 { get; set; }
        public static Form2 F2 { get; set; }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            Form1 = new Form1();
            Form2 = new Form2();

            Application.Run(Form1);
        }
    }
}

그런 다음 응용 프로그램의 모든 양식에서 사용할 수 있습니다. Program.Form1 또는 Program.Form2 이미 인스턴스화 된 참조로.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top