I write the client part of the usual tests (quizzes), where dynamically, depending on the number of response options, radiobutton is created. In what way can / need to check which of them was checked? The selected answers will be placed in the List and then sent to the server, but I can’t understand how to select the designated answers.
public void Create_controls() { flowLayoutPanelTop.Controls.Clear(); int k = 0; Random random = new Random(); for (int j = 0; j < c; j++) { TextBox question = new TextBox(); question.AutoSize = true; question.Enabled = false; question.Name = "lb" + j.ToString(); flowLayoutPanelTop.Controls.Add(question); question.Font = new Font(question.Font, FontStyle.Bold); byte[] array = new byte[1]; // num of test array[0] = (byte)j; //get test Datagram getTest = new Datagram(Commands.GET_TEST, array); //get test getTest.Send(MainForm.socket); //get test getTest.ReceiveData(MainForm.socket); //get test string result = System.Text.Encoding.UTF8.GetString(getTest.data.ToArray()); Jsonn test = JsonConvert.DeserializeObject<Jsonn>(result); int num = test.Variants.Count; question.Text = test.Text; List<Dictionary<string, string>> dat = new List<Dictionary<string, string>>(); dat = test.Variants; List<int> val = new List<int>(); //list depends on num of variants for(int i = 0; i < num; i++) { val.Add(i); } for (int i = 0; i < val.Count; i++) //shuffle { int tmp = val[0]; val.RemoveAt(0); val.Insert(random.Next(val.Count), tmp); } val = val.OrderBy(v => random.Next()).ToList(); //one more time shuffle for (int i = 0; i < num; i++) // num for number of variants { RadioButton rb = new RadioButton(); rb.Name = "rb" + k.ToString() + i.ToString(); //name like rb41 mean 4 question 1 variant flowLayoutPanelTop.Controls.Add(rb); rb.AutoSize = true; int q = val[i]; var dictWithKey = dat.First(d => d.ContainsKey(q.ToString())); rb.Text = dictWithKey[q.ToString()]; k = k + 4; } num = 0; } Button btnSend = new Button(); btnSend.AutoSize = true; btnSend.Text = "Send"; flowLayoutPanelTop.Controls.Add(btnSend); }
foreach (var rb in flowLayoutPanelTop.Controls.OfType<RadioButton>()){...}? - tym32167