I'm starting to learn C #, do simple programs and learn from it)

I created two forms, Form1 and Form3 , trying to pass a radioButton value that can be true or false .

Code Form1:

 public partial class Form1 : Form { public Form1() { InitializeComponent(); } public void Otvet1_Click(object sender, EventArgs e) { if (VariantNeVern1.Checked || VariantNeVern2.Checked || VariantVern.Checked) { Hide(); Form2 Vopros = new Form2(); Vopros.ShowDialog(); } else MessageBox.Show("Выбирите ответ"); Close(); } private void VariantNeVern1_CheckedChanged(object sender, EventArgs e) { Form3 Vopros = new Form3(); Vopros.Owner = this; } } 

Code Form3:

 public partial class Form3 : Form { public Form3() { InitializeComponent(); Form1 main = new Form1(); Rezult.Text = Convert.ToString(main.VariantNeVern1);//тут косяк } private void CloseTest_Click(object sender, EventArgs e) { Close(); } } 

I have to get the result true or false , to make sure it works or not, I constantly get false , and logically, if I selected a radioButton with the name VariantNeVern1 , I should get true .

  • Read about properties and what it is. - Alexander Puzanov

1 answer 1

You still have problems with OOP.

 Form1 main = new Form1(); // на самом деле, косяк тут Rezult.Text = Convert.ToString(main.VariantNeVern1);//тут не косяк 

You create a new object of type Form1. It has nothing to do with the main form of your application. Accordingly, all controls in it are in the initial state and you always pass False. If this is not a random error, I recommend starting with a console application, but with a good object model and a bunch of little classics, each of which has its own area of ​​responsibility. After this, return to the world of UI.

If we talk about WinForms, then it's 2017 outside. And if there is a desire to develop in the direction of the UI, I recommend starting right away with WPF.

If we talk about the very method of transmitting the control, then in the UI world this is not done. There are several basic design patterns that people try to adhere to (only without fanaticism). For example, MVC (model, view, controller) and MVVMC (model, view model, view, controller).

In your case, the model class that stores certain states is missing. It is important to understand that if you do not write an interface editor, then you do not need to pass a button between forms. You need to pass some mode of operation, for which it is responsible.

 public enum Variants { Correct = 1, Incorrect = 2 } 

These states are stored in a certain model class, a context that rummages between everyone.

 public class Model { public Variants CurrentVariant {get;set;} } 

And you initialize this model once, and then transfer it to all the forms that need to communicate with each other (in fact, usually, you transfer the controller — a smarter entity that contains not only data, but also some operations, actions, but This you read in Google on request MVC).

 Model model = new Model(); Form1 form1 = new Form1(model); Form2 form2 = new Form2(model); 

And they already use it and save the button state in it, not as "Checked", but as a specific state of the system.

 public partial class Form1 : Form { private readonly Model _model; public Form1(Model model) { _model = model; } private void VariantNeVern1_CheckedChanged(object sender, EventArgs e) { bool isChecked = VariantNeVern1.Checked; Variants variant = isChecked ? Variants.Incorrect : Variants.Correct; _model.CurrentVariant = variant; } } 

Accordingly, the second form can access this value.

At what point to create forms and how to transfer between them a data model or controller is the very problem for which design patterns are invented.

Read about MVC, most of the questions will disappear. If you switch to WPF, read about Binding and INotifyPropertyChanged. When you get comfortable, you can look in the direction of a more strict and difficult MVVMC.

Well, as part of your example, you can either create both forms at first, and then display and hide them using the Show and Hide methods, while in the second subscribe to the Shown event and at that moment read the value from the model; or create a second form inside the first one and transfer the model from it, then you can leave the initialization of your "Re z ult" in the constructor, again, getting the value from the model.

I hope that I could help, and not just confuse.

  • Concerning WPF it agree. You can even touch UWP / Xamarin. I started with UWP right away, for example - Arthur
  • Lunar Whisper, Thank you. I understand through time, the fact is that this is my first programming language in general, a lot of things are not clear, it means that I’m bad with OOP, I’ve been learning C # for about a month already :) What can I read or watch video tutorials in accessible language for teapots laid out all on the shelves. It turns out that it’s too early for me in the UI world and so far I only need to work with the console. - Anton
  • "C # 5.0 in a Nutshell", "CLR via C #" sergeyteplyakov.blogspot.ru/2014/04/best-books-on-cnet.html - Lunar Whisper