• C# 关于sendtoback()和bringtofront() 的特点说明


    在开发的时候经常会遇到使用这两种方法,调整控件的顺序,尤其是在自动生成控件的时候。有的时候会感觉不好用,这里提供一个解决方案。

    这两个方法必须要在添加控件之后才会好用,范例代码如下:

    label lbl=new label();

    //属性设定。。。。。

    control.add(lbl);

    lbl.bringtofront();


    控件置顶:控件名.BringToFront();
    控件置底:控件名.SendToBack();


    c#子线程和主线程创建窗体时顶层显示的区别

    主线程

    1.设置TopLevel,Topmost,bringfront属性。

    子线程

    2.在可在Load事件里注册循环设置TopLevel,Topmost,bringTofront属性的方法,如下

     frm.Load += new EventHandler((object ob, EventArgs ee) =>
                    {
                        Task.Factory.StartNew(() =>
                        {
                            while (true)
                            {
                                Thread.Sleep(10);
                                frm.Invoke(new Action(() =>
                                {

                                    frm.BringToFront();
                                    frm.TopMost = true;
                                    frm.TopLevel = true;
                                }));

                            }


                        });

                    });
     


    1. protected override void DoBuildChildren(LayoutPanelBase Layout)
    2. {
    3. base.DoBuildChildren (Layout);
    4. CaptionLabel.Dock = DockStyle.Top;
    5. mode = new ComboBox ();
    6. mode.Parent = ParentNotePanel;
    7. mode.DropDownStyle = ComboBoxStyle.DropDownList;
    8. mode.Dock = DockStyle.Top;
    9. mode.BringToFront ();
    10. mode.Items.Add (Loc.Instance.GetString ("Notes on This Layout"));
    11. mode.Items.Add (Loc.Instance.GetString ("All Layouts"));
    12. mode.Items.Add (Loc.Instance.GetString ("Notes on Current Layout"));
    13. mode.SelectedIndexChanged += HandleDropDownSelectedIndexChanged;
    14. SearchDetails = new Panel ();
    15. AdjustDockingOfSearchPanel();
    16. CurrentFilterDropDown = new ComboBox ();
    17. CurrentFilterDropDown.Dock = DockStyle.Bottom;
    18. // because we need the tables to be loaded we CANNOT load this data now
    19. LayoutDetails.Instance.UpdateAfterLoadList.Add (this);
    20. TextEditor = new ComboBox ();
    21. TextEditor.Dock = DockStyle.Bottom;
    22. TextEditor.KeyPress += HandleTextEditKeyPress;
    23. TextEditor.KeyUp += HandleKeyUp;
    24. // Do some cleanup on history item to keep the list reasonable.
    25. // This happens only on load to keep things simple
    26. if (History.Count > 10) {
    27. History.RemoveRange(9, (History.Count)-9);
    28. }
    29. foreach (string s in History) {
    30. TextEditor.Items.Add (s);
    31. }
    32. TextEditor.SelectedIndexChanged+= (object sender, EventArgs e) => Refresh ();;
    33. FullTextSearch = new CheckBox();
    34. FullTextSearch.Checked = false;
    35. FullTextSearch.Text = Loc.Instance.GetString ("Search All Text Fields");
    36. FullTextSearch.Dock = DockStyle.Bottom;
    37. SearchDetails.Controls.Add (TextEditor);
    38. SearchDetails.Controls.Add (CurrentFilterDropDown);
    39. SearchDetails.Controls.Add (FullTextSearch);
    40. list = new ListBox();
    41. //list.SelectedIndexChanged += HandleDropDownSelectedIndexChanged;
    42. list.Parent = ParentNotePanel;
    43. list.Width = 125;
    44. list.Dock = DockStyle.Fill;
    45. list.BringToFront();
    46. list.BindingContextChanged+= HandleBindingContextChanged;
    47. list.DoubleClick += HandleListBoxDoubleClick;
    48. list.Click+= HandleNoteListClick;
    49. ParentNotePanel.Controls.Add (SearchDetails);
    50. count = new Label();
    51. count.Parent = ParentNotePanel;
    52. count.Dock = DockStyle.Bottom;
    53. blurb = new Label();
    54. blurb.Parent = ParentNotePanel;
    55. blurb.Dock = DockStyle.Bottom;
    56. refresh = new Button();
    57. refresh.Text = Loc.Instance.GetString("Refresh");
    58. refresh.Dock = DockStyle.Bottom;
    59. refresh.Parent = ParentNotePanel;
    60. refresh.Click += HandleRefreshClick;
    61. //Menu Stuff
    62. TokenItem = new ToolStripMenuItem(Loc.Instance.GetString("Dock on Bottom?"));
    63. TokenItem.CheckOnClick = true;
    64. TokenItem.Click += (object sender, EventArgs e) => {
    65. searchDetailsDock = (sender as ToolStripMenuItem).Checked;
    66. AdjustDockingOfSearchPanel();
    67. this.SetSaveRequired(true);
    68. };
    69. if (searchDetailsDock) TokenItem.Checked = true; else TokenItem.Checked= false;
    70. properties.DropDownItems.Add (new ToolStripSeparator());
    71. properties.DropDownItems.Add (TokenItem);
    72. // AdjustHeightOfLayoutSearchPanel (); This already gets called when the note type is chosen
    73. }

    如下的代码:
    button2.Dock =DockStyle.Top;button1.Send ToBack();
    button1.Dock = DockStyle.Top;button3.Dock = DockStyle.Bottom;
    pulon3.vOCK -ocKolyie.Douom,
    运行后,3个button的顺序始终为:
    button1
    button2
    button3
    如果代码改为:
    button2.Dock = DockStyle.Top;
    button1.Dock = DockStyle.Top;
    button3.Dock = DockStyle.Bottom;
    运行后,3个button 的顺序始终为:

    button2
    button1

    button3
     


    Z顺序指的是:在 Windows中桌面以及Form都是二维的,也就是×和丫轴,那么Z轴在哪里呢?
    其实,Z轴就是垂直于桌面和 Form的方向,这个方向上的排列就叫Z顺序。
    Windows窗体的Z顺序就是桌面上所有窗口的在Z轴上的排列;
    而控件的Z顺序则是控件所在的父容器上的所有控件的在该父容器得的Z轴上的排列;
    .NET中,控件的Dock 属性是将控件做×和Y方向上的排列。这个属性设置后,排列的先后顺序,是与控件被加载的顺序有关的控件的SendToBack 和 BringToFront两个方法,就是用来改变Z方向上的排列顺序的,简单说就是控件或者Form在其父容器或桌面上的叠放层次。所以他们不是用来改变在×和Y方向上的排列的。
    要想改变那三个Button的前后顺序,可以在解决方案管理器中找到它所在的窗体,然后点击窗体节点前面的+号,将节点展开,然后双击“你的窗体名称.Designer.cs"打开窗体设计文件来修改调整这三个按钮被Controls.Add(...)的先后顺序,即可达到改变Dock 上下顺序.目的。
     

  • 相关阅读:
    Poco脚本的点击位置与点击偏移
    leetcode995. K 连续位的最小翻转次数
    机器学习案例(十一):水质分析与预测
    二进制明文字符串加密:还原与反还原
    python语言字符串练习题
    【Try to Hack】vulnhub DC1
    【C#】什么是并发,C#常规解决高并发的基本方法
    Dataset之mpg:mpg数据集的简介、下载、使用方法之详细攻略
    Java判断字符串是否是有效的括号
    AI-多模态-2021:DALL-E模型【文本生成图像工具】【OpenAI】
  • 原文地址:https://blog.csdn.net/weixin_67336587/article/details/125632956