最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C#?winForm自定義彈出頁(yè)面效果

 更新時(shí)間:2022年02月12日 11:05:15   作者:鈴鐺當(dāng)當(dāng)  
這篇文章主要為大家詳細(xì)介紹了C#?winForm自定義彈出頁(yè)面效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C# winForm自定義彈出頁(yè)面效果的具體代碼,供大家參考,具體內(nèi)容如下

在C#的windows窗體應(yīng)用程序中,添加彈出框效果.最后就是這樣的效果.

頁(yè)面Form2上有2個(gè)文本框,textBox1和textBox2.點(diǎn)擊任意一個(gè)文本框,根據(jù)準(zhǔn)備好的數(shù)據(jù),彈出Form1.其中Form1中的button個(gè)數(shù)是根據(jù)準(zhǔn)備好的數(shù)據(jù)生成的.并且Form1的彈出位置,應(yīng)該是文本框上面.最后,點(diǎn)擊任意一個(gè)按鈕,會(huì)將按鈕上的值,顯示到對(duì)應(yīng)的文本框中,然后彈出頁(yè)面關(guān)閉.

兩個(gè)文本框顯示的數(shù)據(jù)效果就是如下,這是textBox2.

這個(gè)是textBox1的效果.

主要做法就是,自定義了一個(gè)用戶控件.由于此代碼是在顏料板的基礎(chǔ)上改過(guò)來(lái)的,所以起名不大對(duì).這個(gè)用戶控件的作用就是根據(jù)數(shù)據(jù)生成button數(shù),并且給button綁定click事件,并且接收參數(shù)textBox.在click事件中,獲取button的text,然后給之前的textBox的Text賦值button的text,然后textBox初始化.這樣就可以在textBox上顯示button按鈕上的值.而生成的button是放在flowLayoutPanel1控件中,這樣他就會(huì)自動(dòng)的一個(gè)一個(gè)排列.

首先單獨(dú)新建一個(gè)windows窗體應(yīng)用程序,叫ColorHelper.然后在里面加自定義用戶控件ColorSelector.整個(gè)項(xiàng)目完成之后的截圖是這樣的.

然后再ColorSelector中,拖一個(gè)flowLayoutpanel控件就是flowLayoutPanel1。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Text;
using System.Windows.Forms;
?
namespace ColorHelper
{
? ? /// <summary>
? ? /// 自定義顏色控件
? ? /// </summary>
? ? public partial class ColorSelector : UserControl
? ? {
? ? ? ??
? ? ? ? //接收傳遞的參數(shù),文本框和鍵值對(duì)的數(shù)據(jù)。
? ? ? ? public Control textBox = new Control();
? ? ? ? public Dictionary<string, string> dict = new Dictionary<string, string>();
?
? ? ? ? public Dictionary<string, string> Dict
? ? ? ? {
? ? ? ? ? ? get { return dict; }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? dict = value;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? //構(gòu)造函數(shù)
? ? ? ? public ColorSelector()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
?
? ? ? ? //選中的Text值
? ? ? ? private string selectText;
?
? ? ? ? public string SelectedText
? ? ? ? {
? ? ? ? ? ? get { return selectText; }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? selectText = value;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? //選中的Key值
? ? ? ? private string selectKey;
?
? ? ? ? public string SelectedKey
? ? ? ? {
? ? ? ? ? ? get { return selectKey; }
? ? ? ? ? ? set
? ? ? ? ? ? {
? ? ? ? ? ? ? ? selectKey = value;
? ? ? ? ? ? }
? ? ? ? }
?
? ? ? ? /// <summary>
? ? ? ? /// 生成Button
? ? ? ? /// </summary>
? ? ? ? public void LoadButton()
? ? ? ? {
? ? ? ? ? ? //情況panel中原來(lái)的所有控件
? ? ? ? ? ? flowLayoutPanel1.Controls.Clear();
?
? ? ? ? ? ? //根據(jù)傳遞的dict鍵值對(duì)生成Button,并設(shè)置button的大小,Text,Tag值,以及綁定鼠標(biāo)點(diǎn)擊事件。
? ? ? ? ? ? foreach (KeyValuePair<string, string> temp in dict)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Button button1 = new Button();
? ? ? ? ? ? ? ? button1.Text = temp.Value;
? ? ? ? ? ? ? ? button1.Tag = temp.Key;
? ? ? ? ? ? ? ? button1.Font = new Font("宋體", 22);
? ? ? ? ? ? ? ? button1.AutoSize = true;
? ? ? ? ? ? ? ? button1.Width = 120;
? ? ? ? ? ? ? ? button1.MouseClick += new MouseEventHandler(button_MouseClick);
? ? ? ? ? ? ? ? flowLayoutPanel1.Controls.Add(button1);
? ? ? ? ? ? }
? ? ? ? }
?
??
? ? ? ? /// <summary>
? ? ? ? /// 綁定到button上的事件
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void button_MouseClick(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //聲明一個(gè)button,獲取到button上的Text和Tag上的值,分別是value和key。
? ? ? ? ? ? Button cb = (Button)sender; ? ? ? ?
? ? ? ? ? ? selectText = cb.Text;
? ? ? ? ? ? selectKey = cb.Tag.ToString();
? ? ? ? ? ? //將value和key分別給textBox的Text和Tag。
? ? ? ? ? ? textBox.Text = selectText;
? ? ? ? ? ? textBox.Tag = selectKey;
? ? ? ? ? ? //重繪textBox
? ? ? ? ? ? textBox.Invalidate();
? ? ? ? ? ? textBox.Enabled = true;
? ? ? ? ? ? //隱藏控件,并將控件所在的Form關(guān)閉
? ? ? ? ? ? this.Visible = false;
? ? ? ? ? ? this.FindForm().Close();
? ? ? ? }
??
? ? }
?
}

然后自定義用戶控件建立好了??梢栽俳⒁粋€(gè)項(xiàng)目ColorTest,然后建立2個(gè)Form。其中Form1放這個(gè)控件,F(xiàn)orm2放文本框,彈出Form1.

然后再ColorTest中添加引用,把colorHelper引用過(guò)來(lái)。

而添加到工具箱中,需要在工具箱中右擊,選擇選擇項(xiàng),然后瀏覽找到dll或者exe,就可以了。效果就是這樣。

然后就能把這個(gè)Colorselector的自定義控件拖到Form1上。然后Form1的邊框風(fēng)格FormBorderStyle改為None,并且Form的AutoSize一定要是false。還有Form1的startPosition屬性要改為Manual,自定義。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
?
namespace colorTest
{
? ? public partial class Form1 : Form
? ? {
? ? ? ? //接收textBox和dict數(shù)據(jù)
? ? ? ? public TextBox textBox;
? ? ? ? public Dictionary<string, string> dict;
?
? ? ? ? //有參構(gòu)造函數(shù),接收Form1傳遞的值
? ? ? ? public Form1(TextBox textBox, Dictionary<string, string> dict)
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? ? ? this.textBox = textBox;
? ? ? ? ? ? this.dict = dict;
? ? ? ? }
?
? ? ? ? //加載時(shí)將textBox和Dict給自定義控件,并生成button按鈕,最后顯示button框
? ? ? ? private void Form1_Load(object sender, EventArgs e)
? ? ? ? {
? ? ? ? ? ? //設(shè)置重畫控件
? ? ? ? ? ? colorSelector1.textBox = textBox;
? ? ?
? ? ? ? ? ? //返回到自定義用戶控件上
? ? ? ? ? ? colorSelector1.Dict = dict;
? ? ? ? ? ? colorSelector1.LoadButton();
? ? ? ? ? ? //設(shè)置彈出事件
? ? ? ? ? ? colorSelector1.Visible = true;
? ? ? ? }
?
? ? ? ?
? ? }
}

最后就是Form2的效果。這個(gè)就是這樣。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
?
namespace colorTest
{
? ? public partial class Form2 : Form
? ? {
? ? ? ? public Form2()
? ? ? ? {
? ? ? ? ? ? InitializeComponent();
? ? ? ? }
?
? ? ? ? private void textBox2_MouseClick(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //先去查詢數(shù)據(jù),獲取到返回值為實(shí)體數(shù)組,轉(zhuǎn)成Dictionary<string,string>
? ? ? ? ? ? Dictionary<string, string> dict = new Dictionary<string, string>();
? ? ? ? ? ? dict.Add("1", "汽油");
? ? ? ? ? ? dict.Add("2", "柴油");
? ? ? ? ? ? dict.Add("3", "煤油");
? ? ? ? ? ? dict.Add("4", "4油");
? ? ? ? ? ? Form1 form = new Form1(this.textBox2, dict);
? ? ? ? ? ? form.Size = new Size(10, 10);
? ? ? ? ? ? //MessageBox.Show(textBox2.Location.X + " " + textBox2.Height + " " + textBox2.Location.Y + " " + textBox2.Width + " ");
? ? ? ? ? ? //form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y - textBox2.Height);
? ? ? ? ? ? //MessageBox.Show(textBox2.Location.X + " " + textBox2.Height+" "+ textBox2.Location.Y+" " +textBox2.Width+ " " );
? ? ? ? ? ? //form.Location = new Point(textBox2.Location.X - textBox2.Height, textBox2.Location.Y - textBox2.Width);
? ? ? ? ? ? //MessageBox.Show(this.Location.X + " " + this.Location.Y );
? ? ? ? ? ? //每行顯示5個(gè)button按鈕
? ? ? ? ? ? if (dict.Count >= 5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //并且設(shè)置5個(gè)時(shí),form的size,直接為626,這個(gè)是多次測(cè)試過(guò)得到的結(jié)果。
? ? ? ? ? ? ? ? form.Size = new Size(626, 33 * (dict.Count / 5 + 1));
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? form.Size = new Size(125 * dict.Count, 33);
? ? ? ? ? ? }
?
? ? ? ? ? ? //form的彈出位置,必須要設(shè)置Form2的startposition為自定義,否則不管用。
? ? ? ? ? ? //在窗體的x的基礎(chǔ)上,加上textBox的x坐標(biāo),就能控制彈出框的x坐標(biāo),而窗體的y坐標(biāo)加上窗體的y坐標(biāo),還要考慮form的height
? ? ? ? ? ? form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y - 15 * (dict.Count / 5 + 1));
? ? ? ? ? ??
? ? ? ? ? ? //彈出form
? ? ? ? ? ? form.ShowDialog();
? ? ? ? ? ?
? ? ? ? }
?
? ? ? ? /// <summary>
? ? ? ? /// textBox1的鼠標(biāo)點(diǎn)擊事件
? ? ? ? /// </summary>
? ? ? ? /// <param name="sender"></param>
? ? ? ? /// <param name="e"></param>
? ? ? ? private void textBox1_MouseClick(object sender, MouseEventArgs e)
? ? ? ? {
? ? ? ? ? ? //先去查詢數(shù)據(jù),獲取到返回值為實(shí)體數(shù)組,轉(zhuǎn)成Dictionary<string,string>
? ? ? ? ? ? Dictionary<string, string> dict = new Dictionary<string, string>();
? ? ? ? ? ? dict.Add("1", "汽油");
? ? ? ? ? ? dict.Add("2", "柴油");
? ? ? ? ? ? dict.Add("3", "煤油");
? ? ? ? ? ? dict.Add("4", "4油");
? ? ? ? ? ? dict.Add("5", "5油");
? ? ? ? ? ? dict.Add("7", "6油");
? ? ? ? ? ? dict.Add("8", "7油");
? ? ? ? ? ? dict.Add("9", "8油");
? ? ? ? ? ? dict.Add("10", "9油");
? ? ? ? ? ? dict.Add("11", "10油");
? ? ? ? ? ? dict.Add("12", "6油");
? ? ? ? ? ? dict.Add("13", "7油");
? ? ? ? ? ? dict.Add("14", "8油");
? ? ? ? ? ? dict.Add("15", "9油");
? ? ? ? ? ? dict.Add("16", "10油");
? ? ? ? ? ? Form1 form = new Form1(this.textBox1, dict);
? ? ? ? ? ??
? ? ? ? ? ? if (dict.Count >= 5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? form.Size = new Size(626, 33 * (dict.Count/5+1));
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? form.Size = new Size(125 * dict.Count, 33);
? ? ? ? ? ? }
? ? ? ? ? ? form.Location = new Point(textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y -15*(dict.Count/5+1));
? ? ? ? ??
? ? ? ? ??
? ? ? ? ? ? form.ShowDialog();
? ? ? ? }
? ? }
}

以上就是彈出框的全部代碼了?;宋也簧贂r(shí)間,并且,學(xué)會(huì)了算x,y的值。發(fā)現(xiàn)所有的顯示的location的值,都是相對(duì)值,相對(duì)于包含他們的容器。textBox是在form2中,他的location是相對(duì)于form2,而form2的location的相對(duì)于屏幕。

知道了改不來(lái)FlowLayoutpanel,每5個(gè)換一行,可以控制他的容器Form1,控制他的大小。所以里面的FlowLayoutpanel也不能設(shè)置autosize為true,不能自適應(yīng),否則他就一行顯示了。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

马关县| 肇庆市| 临江市| 伊吾县| 鸡东县| 工布江达县| 且末县| 钦州市| 项城市| 西吉县| 长顺县| 平阴县| 三门县| 涞源县| 郑州市| 永泰县| 北辰区| 石台县| 澎湖县| 宁陕县| 周宁县| 和政县| 青田县| 星子县| 章丘市| 仙居县| 会昌县| 山丹县| 大城县| 泰安市| 乡城县| 伊宁市| 会理县| 星座| 平山县| 黔东| 科技| 海城市| 怀宁县| 朔州市| 新邵县|