There is a form with text nomer1 , nomer2 , etc. When a form is loaded, a file is read in which the box is registered or not.

 using (StreamReader reader = new StreamReader(@"C:\test\viseble.txt")) { if (reader.ReadLine() == "1 true") { nomer1.Visibility = Visibility.Visible; } else { nomer1.Visibility = Visibility.Hidden; } if (reader.ReadLine() == "2 true") { nomer2.Visibility = Visibility.Visible; } else { nomer2.Visibility = Visibility.Hidden; } } 

There are many textboxes and a very large piece of duplicate code is obtained. Please tell me how it can be simplified?

  • 1. The expression () ?: (expr)?Visible:Hidden 2. You can use ((WebControl)FindControl("nomer" + i)).Visibility - nick_n_a
  • Show a screenshot of the window to make it clearer. - Ev_Hyper

3 answers 3

Such settings are preferably stored as follows:

Right click on the project name -> Properties -> Settings

enter image description here

We create the necessary properties. Scope User — settings that can be changed; Scope Application — read-only settings.

 //чтение настройки this.textBox.Visibility = Properties.Settings.Default.ForTextBox1 ? Visibility.Visible : Visibility.Hidden; //запись настройки Properties.Settings.Default.ForTextBox1 = false; Properties.Settings.Default.Save(); 

    Even without changing the approach to storing settings, as suggested in the answer @Bulson, the code can be simplified.

     using (StreamReader reader = new StreamReader(@"C:\test\viseble.txt")) { TextBox tbs = { nomer1, nomer2 }; foreach (var tb in tbs) { bool vis = reader.ReadLine() == "true"; tb.Visibility = vis ? Visibility.Visible : Visibility.Hidden; } } 

    Here I have removed the need to have a number ( 1 or 2 ) in the settings file, since according to your logic, they are still arranged sequentially, i.e. The first line is the appearance of the first box, the second - the second box.

    • no, it is impossible to reduce, because Visibility is enum. But you can replace true by the values ​​from enum, and use Enum.Parse - Grundy
    • @Grundy well, it means it is impossible :) I assumed it could wrap from bool , it turns out, erroneously. - αλεχολυτ

    Thanks to everyone, this solution was found:

     using (StreamReader reader = new StreamReader(@"C:\visible.txt")) { for (int i = 1; i < 10; i++) { var box = (TextBox)FindName("textBox" + i); if (reader.ReadLine() == "true") { box.Visibility = Visibility.Visible; } else { box.Visibility = Visibility.Hidden; } } } 

    Any number of lines is read for any number of elements, depending on i .

    • How can there be any if you have a cycle of only 9 iterations? Well, FindName returns null if the item is not found and get NPE explicitly upon further access to the box fields. - αλεχολυτ
    • I wrote from i how many elements - so many iterations. You can make i <100, etc. And you can also do a cycle on reading the number of elements, but that's enough for me. - Alexander
    • Do not forget to vote for the answers (arrows on the left) and select the most suitable option by ticking. You can even your own. - αλεχολυτ