1. 程式人生 > >動態建立控制元件佈局與自動大小跟隨

動態建立控制元件佈局與自動大小跟隨

有人認為用寫的動態建立控制元件很高階,其實我更喜歡拖放控制元件進行各種屬性和事件設定很方便,而不是用寫的進行各種屬性和事件的設定,結合以前寫的例子,對各類設定做了歸類,比較方便集中寫作。


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace 文字提取到資料檔案

{

    public partial class 文字提取到資料檔案 : Form

    {

        DataGridView 資料列表 = new DataGridView();

        TextBox 錄入框 = new TextBox();

        ToolTip 控制元件說明 = new ToolTip();

        TreeView 分類樹 = new TreeView();

        CheckBox 肝 = new CheckBox(), 心 = new CheckBox(), 脾 = new CheckBox(), 肺 = new CheckBox(), 腎 = new CheckBox(), 

            熱 = new CheckBox(), 溫 = new CheckBox(), 寒 = new CheckBox(), 涼 = new CheckBox(), 平 = new CheckBox();

 

        public 文字提取到資料檔案()

        {

            InitializeComponent();

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            跟隨(); 停靠(); 尺寸(); 佈局(); 初值(); 註明();

            動態文字框();

            動態表格(new string[] { "名稱", "別名", "性味", "歸經", "主治" });

        }

        //private void 動態選項框()

        //{

        //    int i = 分類樹.Width + 5;

        //    string[] 五臟五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "腎(鹹)" };

        //    foreach (string 名 in 五臟五味)

        //    {

        //        CheckBox 選項框 = new CheckBox();

        //        選項框.Text = 名;

        //        選項框.Name = 名.Remove(1);

        //        選項框.Location = new Point(i, 17);

        //        選項框.Anchor = (AnchorStyles.Bottom | AnchorStyles.Right);

        //        選項框.Parent = this;

        //        this.Controls.Add(選項框);

        //        i += 105;

        //    };

        //}

        private void 動態文字框()

        {

            錄入框.Text = "";

            錄入框.Name = string.Format("文字框{0}", 1);

            錄入框.AllowDrop = true;

            錄入框.Multiline = true;

            //this.Controls.Add(錄入框);

        }

        private void 動態表格(string[] 資料格)

        {

            DataTable 資料表 = new DataTable("資料表");

            foreach (string 格 in 資料格)

                資料表.Columns.Add(格, typeof(string));

 

            資料列表.AllowUserToAddRows = false;

            資料列表.AllowUserToDeleteRows = false;

            資料列表.AllowUserToOrderColumns = false;

            資料列表.AllowUserToResizeColumns = false;

            資料列表.AllowUserToResizeRows = false;

            資料列表.ReadOnly = true;

            資料列表.RowHeadersVisible = false;

            資料列表.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

            資料列表.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            資料列表.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;

            資料列表.RowsDefaultCellStyle.WrapMode = (DataGridViewTriState.True);

            資料列表.DefaultCellStyle.SelectionBackColor = Color.Transparent;

            資料列表.DefaultCellStyle.SelectionForeColor = Color.Red;/*.Blue.Gold*/

            資料列表.GridColor = Color.Lime;

            資料列表.DataSource = 資料表;

        }

 

        private void 文字提取到資料檔案_Resize(object sender, EventArgs e)

        {

            //MessageBox.Show("最大化!");

            //this.Invalidate();

            //this.Refresh();

            跟隨(); 停靠(); 尺寸(); 初值(); 佈局(); 註明();

 

            //System.IO.FileStream 開啟 = new System.IO.FileStream(Application.StartupPath + @"\新建文字文件.txt", System.IO.FileMode.Open);

            //System.IO.StreamReader 讀取 = new System.IO.StreamReader(開啟, Encoding.GetEncoding("GB2312"));

            //while (!讀取.EndOfStream)

            //    資料表.Rows.Add(讀取.ReadLine().Split(' '));

            //開啟.Close();

        }

        void 跟隨()

        {

            資料列表.Anchor =

                錄入框.Anchor =

                肝.Anchor = 心.Anchor = 脾.Anchor = 肺.Anchor = 腎.Anchor =

                熱.Anchor = 溫.Anchor = 寒.Anchor = 涼.Anchor = 平.Anchor =

                分類樹.Anchor = (AnchorStyles.Top | AnchorStyles.Left);

        }

        void 停靠()

        {

            資料列表.Parent =

                錄入框.Parent =

                肝.Parent = 心.Parent = 脾.Parent = 肺.Parent = 腎.Parent = 

                熱.Parent = 溫.Parent = 寒.Parent = 涼.Parent = 平.Parent =

                分類樹.Parent = this;

        }

        void 尺寸()

        {

            分類樹.Size = new Size(this.Width / 5, this.Height - 42);

            資料列表.Size = new Size(this.Width - 分類樹.Width - 24, this.Height / 2);

            錄入框.Size = new Size(this.Width - 分類樹.Width - 25, this.Height / 2 - 75);

        }

        void 佈局()

        {

            分類樹.Location = new Point(2, 2);

            資料列表.Location = new Point(分類樹.Width + 5, 2);

            錄入框.Location = new Point(分類樹.Width + 5, this.Height - 資料列表.Height + 5);

            肝.Location = new Point(分類樹.Width + 5, this.Height - 65);

            心.Location = new Point(肝.Location.X + 65, this.Height - 65);

            脾.Location = new Point(心.Location.X + 65, this.Height - 65);

            肺.Location = new Point(脾.Location.X + 65, this.Height - 65);

            腎.Location = new Point(肺.Location.X + 65, this.Height - 65);

            熱.Location = new Point(腎.Location.X + 65, this.Height - 65);

            溫.Location = new Point(熱.Location.X + 35, this.Height - 65);

            寒.Location = new Point(溫.Location.X + 35, this.Height - 65);

            涼.Location = new Point(寒.Location.X + 35, this.Height - 65);

            平.Location = new Point(涼.Location.X + 35, this.Height - 65);

        }

        void 初值()

        {

            this.BackColor = Color.Red;

            string[] 五臟五味 = { "肝(酸)", "心(苦)", "脾(甘)", "肺(辛)", "腎(鹹)", "熱", "溫", "寒", "涼", "平" };

            int 啊 = 五臟五味.Length;

            Control.ControlCollection 找選項 = this.Controls;

            foreach (Control 選項 in 找選項)

                if (選項 is CheckBox) 選項.Text = 五臟五味[--啊];

        }

        void 註明()

        {

            控制元件說明.SetToolTip(錄入框, "請輸入資料文字以便進行分割到資料檔案儲存。");

            控制元件說明.SetToolTip(分類樹, "顯示選擇的分類結構。");

        }

 

    }

}

相關推薦

動態建立控制元件佈局自動大小跟隨

有人認為用寫的動態建立控制元件很高階,其實我更喜歡拖放控制元件進行各種屬性和事件設定很方便,而不是用寫的進行各種屬性和事件的設定,結合以前寫的例子,對各類設定做了歸類,比較方便集中寫作。 using System; using System.Collections.

動態建立控制元件的序列化反序列化

 public Button   Btn = new Button(); //序列化儲存 savedButton = XamlWriter.Save(Btn); //反序列化 還原 if (!string.IsNullOrEmpty(savedButton

Qt動態建立控制元件並實現佈局

最近搞的一些需求有相關的設計,從網上找來一段程式碼,受到一些啟發,記下來備用。 有相關需求的可以參考 直接上程式碼: if(icreateControlNum<=display_num_Line) { rownum=1; display_num_Line=icr

excel動態建立控制元件

分配上單擊VBA函式到一個Excel的Userform中動態建立的按鈕 Sign InTop AuthorsTerms of ServiceContact UsBlog .htaccessAdmin Home    English Language Content分配上單擊

vb動態建立控制元件

例如,當你要新增一個TreeView控制元件時,VB會要求你證明你已經得到了合法的授權來建立該控制元件的例項。換句話說,VB要證明這個控制元件是買來的,而不是從其它附有該控制元件的程式中借來的。 要證明你經過了合法的授權有下面幾種方法:         在窗體上放置一個控制元件。這也是最簡單的方法。你完全沒有

MFC動態建立控制元件--CStatic

1.動態建立CStatic文字控制元件   建立方法:CStatic::Create     BOOL   Create(   LPCTSTR   lpszText,   DWORD   dwStyle,   const   RECT&   rect,   C

MFC動態建立控制元件(按鈕)及新增訊息響應

動態控制元件是指在需要時由Create()建立的控制元件,這與預先在對話方塊中放置的控制元件是不同的。 一、建立動態控制元件: 為了對照,我們先來看一下靜態控制元件的建立。 放置靜態控制元件時必須先建立一個容器,一般是對話方塊,這時我們在對話方塊編輯視

在C++Builder中動態建立控制元件

當開發類似解釋程式或實現視覺化操作功能等一類的應用程式時,都需要動態地建立控制元件(物件),而且還要為這些控制元件新增適當的事件處理程式,下面將介紹在C++Builder中如何實現基本的實現框架。 動態建立控制元件的一般過程:   TButton *btn = new TBu

ListView動態建立控制元件、新增資料(單個Activity)

因為專案需要這個功能,所以先寫了個Demo測試一下,Java學了半吊子然後直接上Android做專案,各位大神可以提出點意見,小弟虛心接納。這裡的刪除按鈕還沒實現功能,時間不夠了得回宿舍睡覺了,不想熬夜太難受。先上效果圖:下面說實現的大體思路:1、首先在 MainActivi

Delphi 動態建立控制元件,賦值,使用

 uses 中定義: type   TMyComponent = class(TComponent)   public     item1, item2, item3, item4, item5, item6: string;     constructor Crea

C++ Builder 動態建立控制元件

當開發類似解釋程式或實現視覺化操作功能等一類的應用程式時,都需要動態地建立控制元件(物件),而且還要為這些控制元件新增適當的事件處理程式,下面將介紹在C++Builder中如何實現基本的實現框架。動態建立控制元件的一般過程:  TButton *btn = new TButt

WPF 後臺動態建立控制元件

一、基本控制元件: 1、 Grid 2、 StackPanel 3、 Border 4、 Button 5、 CheckBox 6、 ComboBox 7、 DataGrid 8、 Image 9、 Label 10、

Delphi 程式碼建立控制元件事件動態繫結

unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TF

android 在java程式碼中動態設定控制元件的位置和設定片大小

需要動態改變佈局裡面控制元件的相對位置 如: 1.改變RelativeLayout佈局裡面某個控制元件的layout_toLeftOf 的屬性 RelativeLayout.Layoutparams params = (RelativeLayout.LayoutParams)view

如何實現控制元件隨對話方塊大小變化而自動調整大小和位置

1. 處理對話方塊的WM_SIZE,然後用MoveWindow或者SetWindowPos來把對話方塊上面的控制元件重新調整新位置和大小。  2. 基本的做法就是響應WM_SIZE訊息,並按照視窗大小比例來調整控制元件的位置和大小,控制元件的大小最好設定成視窗的百分之幾,這

android 動態設定控制元件的高度,使用對應佈局中的dp值

1. 獲取你要進行改變的控制元件的佈局 LinearLayout.LayoutParams linearParams =(LinearLayout.LayoutParams) myView.getLayoutParams(); 2.設定佈局的高度   後面的引數就是對應

Android中動態改變控制元件大小的一種方法

    在Android中有時候我們需要動態改變控制元件的大小。有幾種辦法可以實現  一是在onMeasure中修改尺寸,二是在onLayout中修改位置和尺寸。這個是可以進行位置修改的,onMeasure不行。 還有一種是用LayoutParams來進行修改。前兩種方法都

android 動態改變控制元件位置和大小

動態改變控制元件位置的方法: setPadding()的方法更改佈局位置。 如我要把Imageview下移200px:             ImageView.setPadding( ImageView.getPaddingLeft(),  ImageView.get

android 動態設定控制元件大小

ImageView view = new ImageView(context);view.setLayoutParams(new LinearLayout.LayoutParams(30, 30)); view.setpadding(0,0,0,0); LayoutP

Android 開發,JAVA動態設定控制元件大小

在我們的實際android開發中,我們經常需要對控制元件進行設定。在xml中設計大小,我們大家都很熟悉。那麼,使用java動態設定控制元件大小,該如何寫呢。以下是我寫的方法: /** * 設定控制元件大小 * @param view 控制元件