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

发布时间:2023-02-18 09:30

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

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

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;
                            }));

                        }


                    });

                });
 


  protected override void DoBuildChildren(LayoutPanelBase Layout)
        {
            base.DoBuildChildren (Layout);
            CaptionLabel.Dock = DockStyle.Top;

            mode = new ComboBox ();
            mode.Parent = ParentNotePanel;
            mode.DropDownStyle = ComboBoxStyle.DropDownList;
            mode.Dock = DockStyle.Top;
            mode.BringToFront ();

            mode.Items.Add (Loc.Instance.GetString ("Notes on This Layout"));
            mode.Items.Add (Loc.Instance.GetString ("All Layouts"));
            mode.Items.Add (Loc.Instance.GetString ("Notes on Current Layout"));

            mode.SelectedIndexChanged += HandleDropDownSelectedIndexChanged;

            SearchDetails = new Panel ();

            AdjustDockingOfSearchPanel();

            CurrentFilterDropDown = new ComboBox ();
            CurrentFilterDropDown.Dock = DockStyle.Bottom;

            // because we need the tables to be loaded we CANNOT load this data now
            LayoutDetails.Instance.UpdateAfterLoadList.Add (this);

            TextEditor = new ComboBox ();
            TextEditor.Dock = DockStyle.Bottom;
            TextEditor.KeyPress += HandleTextEditKeyPress;
            TextEditor.KeyUp += HandleKeyUp;

            // Do some cleanup on history item to keep the list reasonable.
            // This happens only on load to keep things simple
            if (History.Count > 10) {
                History.RemoveRange(9, (History.Count)-9);
            }

            foreach (string s in History) {
                TextEditor.Items.Add (s);
            }

            TextEditor.SelectedIndexChanged+= (object sender, EventArgs e) => 	Refresh ();;

            FullTextSearch = new CheckBox();
            FullTextSearch.Checked = false;
            FullTextSearch.Text = Loc.Instance.GetString ("Search All Text Fields");
            FullTextSearch.Dock = DockStyle.Bottom;

            SearchDetails.Controls.Add (TextEditor);
            SearchDetails.Controls.Add (CurrentFilterDropDown);
            SearchDetails.Controls.Add (FullTextSearch);

            list = new ListBox();
            //list.SelectedIndexChanged += HandleDropDownSelectedIndexChanged;
            list.Parent = ParentNotePanel;
            list.Width = 125;
            list.Dock = DockStyle.Fill;
            list.BringToFront();
            list.BindingContextChanged+= HandleBindingContextChanged;
            list.DoubleClick += HandleListBoxDoubleClick;
            list.Click+= HandleNoteListClick;

            ParentNotePanel.Controls.Add (SearchDetails);

            count = new Label();
            count.Parent = ParentNotePanel;
            count.Dock = DockStyle.Bottom;

            blurb = new Label();
            blurb.Parent = ParentNotePanel;
            blurb.Dock = DockStyle.Bottom;

            refresh = new Button();
            refresh.Text = Loc.Instance.GetString("Refresh");
            refresh.Dock = DockStyle.Bottom;
            refresh.Parent = ParentNotePanel;
            refresh.Click += HandleRefreshClick;

            //Menu Stuff

             TokenItem = new ToolStripMenuItem(Loc.Instance.GetString("Dock on Bottom?"));
            TokenItem.CheckOnClick = true;
            TokenItem.Click += (object sender, EventArgs e) => {
                searchDetailsDock = (sender as ToolStripMenuItem).Checked;
                AdjustDockingOfSearchPanel();
                this.SetSaveRequired(true);
            };
            if (searchDetailsDock) TokenItem.Checked = true; else TokenItem.Checked= false;

            properties.DropDownItems.Add (new ToolStripSeparator());
            properties.DropDownItems.Add (TokenItem);

            //	AdjustHeightOfLayoutSearchPanel (); This already gets called when the note type is chosen
        }

如下的代码:
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 上下顺序.目的。
 

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号