code_junkie

How to get the “real” value of the Visible property?

Nov 13th, 2011
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. // dodgy; not recommended
  2. Panel query;
  3. Form form = new Form
  4. {
  5. Controls = {new Panel {
  6. Visible = false,
  7. Controls = {
  8. (query = new Panel {Visible = true})
  9. }
  10. }
  11. }
  12. };
  13. form.Show();
  14.  
  15. // this is the dodgy bit...
  16. bool visible = (bool)typeof(Control)
  17. .GetMethod("GetState", BindingFlags.Instance | BindingFlags.NonPublic)
  18. .Invoke(query, new object[] { 2 });
  19.  
  20. var frm2 = new Form {Text = "Form2"};
  21. var lbl = new Label {Visible = true};
  22. frm2.Controls.Add(lbl);
  23. frm2.Show();
  24.  
  25. var frm1 = new Form {Text = "Form1"};
  26. var lblVis = new Label { Text = lbl.Visible.ToString(), Left = 10, Top = 10};
  27. lbl.VisibleChanged += (sender, args) => MessageBox.Show("Label Visible changed");
  28. var btnShow = new Button {Text = "Show", Left = 10, Top = lblVis.Bottom + 10};
  29. btnShow.Click += (sender, args) =>
  30. {
  31. frm2.Visible = true;
  32. lblVis.Text = lbl.Visible.ToString();
  33. };
  34. var btnHide = new Button {Text = "Hide", Left = 10, Top = btnShow.Bottom + 10};
  35. btnHide.Click += (sender, args) =>
  36. {
  37. frm2.Visible = false;
  38. lblVis.Text = lbl.Visible.ToString();
  39. };
  40.  
  41. frm1.Controls.AddRange(new Control[] {lblVis, btnShow, btnHide});
  42.  
  43. Application.Run(frm1);
Add Comment
Please, Sign In to add comment