C#?winForm自定義彈出頁(yè)面效果
本文實(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í)有所幫助,也希望大家多多支持腳本之家。
- C# wpf解決Popup彈出位置異常問(wèn)題解決
- C# menuStrip控件實(shí)現(xiàn)鼠標(biāo)滑過(guò)自動(dòng)彈出功能
- C#彈出對(duì)話框確定或者取消執(zhí)行相應(yīng)操作的實(shí)例代碼
- C# 屏蔽由于崩潰彈出的windows異常彈框
- C# winform實(shí)現(xiàn)右下角彈出窗口結(jié)果的方法
- C#實(shí)現(xiàn)在前端網(wǎng)頁(yè)彈出警告對(duì)話框(alert)的方法
- C#實(shí)現(xiàn)客戶端彈出消息框封裝類實(shí)例
- C#設(shè)置MDI子窗體只能彈出一個(gè)的方法
相關(guān)文章
C# 輸出字符串到文本文件中的實(shí)現(xiàn)代碼
本文通過(guò)一個(gè)簡(jiǎn)單的代碼給大家介紹C# 輸出字符串到文本文件中,代碼簡(jiǎn)單易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-05-05
關(guān)于C#泛型列表List<T>的基本用法總結(jié)
本篇文章主要是對(duì)C#中泛型列表List<T>的基本用法進(jìn)行了詳細(xì)的總結(jié)介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助2014-01-01
通過(guò)C#實(shí)現(xiàn)在Excel單元格中寫入文本、或數(shù)值
在商業(yè)、學(xué)術(shù)和日常生活中,Excel 的使用極為普遍,本文將詳細(xì)介紹如何使用免費(fèi).NET庫(kù)將數(shù)據(jù)寫入到 Excel 中,包括文本、數(shù)值、數(shù)組、和DataTable數(shù)據(jù)的輸入,需要的朋友可以參考下2024-07-07
C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式
這篇文章主要介紹了C#字節(jié)數(shù)組(byte[])和字符串相互轉(zhuǎn)換方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片的示例
本文主要介紹了c#根據(jù)網(wǎng)址抓取網(wǎng)頁(yè)截屏生成圖片并保存的示例,代碼中使用了WebBrowser控件來(lái)完成這個(gè)功能,大家參考使用吧2014-01-01

