C
C
CliverQwerty2018-06-08 06:59:58
C++ / C#
CliverQwerty, 2018-06-08 06:59:58

C# Windows Forms. Passing data between forms, how?

There are two forms, the first form is a text editor, and the second is its child form, which displays the search for given words in a text editor. I was able to do a search on one form, they said to do it on different ones, I can’t, I climbed so many forums, nothing works.
Here is the code if you do everything in one form:

string find = textBox2.Text;
            if (richTextBox1.Text.Contains(find))
            {
                int i = 0;
                while (i <= richTextBox1.Text.Length - find.Length)
                {
                    i = richTextBox1.Text.IndexOf(find, i);
                    if (i < 0) break;
                    richTextBox1.SelectionStart = i;
                    richTextBox1.SelectionLength = find.Length;
                    richTextBox1.SelectionColor = Color.Blue;
                    i += find.Length;
                }
            }
            else
            {
                // Окно сообщения будет отображаться, если введенное слово не найдено
                MessageBox.Show("Не найдено ни одного соответствия результатов");
            }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
vvovas, 2018-06-08
@vvovas

It looks like you need to do the following:

//событие для открытия новой формы
private void searchButton_click(object sender, EventArgs args){

using(var searchForm = new SearchForm()){

//передаем все что нужно на новую форму
searchForm.Text = richTextBox1.Text;

//показываем форму
searchForm.ShowDialog();

}

}

Well, your new form should have a public Text property:
public class SearchForm : Form{
public string Text {get;set;}
...
}

If you need to get data from the child form, then again add a public property with which we will work and use it after ShowDialog()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question