欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#?winForm自定義彈出頁面效果

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

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

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

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

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

這個是textBox1的效果.

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

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

然后再ColorSelector中,拖一個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ù),文本框和鍵值對的數(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中原來的所有控件
? ? ? ? ? ? flowLayoutPanel1.Controls.Clear();
?
? ? ? ? ? ? //根據(jù)傳遞的dict鍵值對生成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)
? ? ? ? {
? ? ? ? ? ? //聲明一個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();
? ? ? ? }
??
? ? }
?
}

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

然后再ColorTest中添加引用,把colorHelper引用過來。

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

然后就能把這個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;
? ? ? ? }
?
? ? ? ? //加載時將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的效果。這個就是這樣。

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個button按鈕
? ? ? ? ? ? if (dict.Count >= 5)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //并且設(shè)置5個時,form的size,直接為626,這個是多次測試過得到的結(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間,并且,學(xué)會了算x,y的值。發(fā)現(xiàn)所有的顯示的location的值,都是相對值,相對于包含他們的容器。textBox是在form2中,他的location是相對于form2,而form2的location的相對于屏幕。

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

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

相關(guān)文章

  • 利用C#編寫Linux守護(hù)進(jìn)程實(shí)例代碼

    利用C#編寫Linux守護(hù)進(jìn)程實(shí)例代碼

    如今的編程是一場程序員和上帝的競賽,程序員要開發(fā)出更大更好、傻瓜都會用到軟件,下面這篇文章主要給大家介紹了關(guān)于利用C#編寫Linux守護(hù)進(jìn)程的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2018-01-01
  • C#中DataGridView常用操作實(shí)例小結(jié)

    C#中DataGridView常用操作實(shí)例小結(jié)

    這篇文章主要介紹了C#中DataGridView常用操作,以實(shí)例形式總結(jié)了DataGridView綁定下拉列表、設(shè)置默認(rèn)值、判斷復(fù)選框是否選中等技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-09-09
  • C#編寫游戲客戶端的實(shí)現(xiàn)代碼

    C#編寫游戲客戶端的實(shí)現(xiàn)代碼

    這篇文章主要介紹了C#編寫游戲客戶端的實(shí)現(xiàn)代碼,連接客戶端原理流程圖,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-11-11
  • C# URL短地址壓縮算法及短網(wǎng)址原理解析

    C# URL短地址壓縮算法及短網(wǎng)址原理解析

    這篇文章主要介紹了C# URL短地址壓縮算法及短網(wǎng)址原理解析,本文重點(diǎn)給出了算法代碼,需要的朋友可以參考下
    2015-03-03
  • C#異步調(diào)用的好處和方法分享

    C#異步調(diào)用的好處和方法分享

    我們要明確,為什么要進(jìn)行異步回調(diào)?眾所周知,普通方法運(yùn)行,是單線程的,如果中途有大型操作(如:讀取大文件,大批量操作數(shù)據(jù)庫,網(wǎng)絡(luò)傳輸?shù)龋?,都會?dǎo)致方法阻塞,表現(xiàn)在界面上就是,程序卡或者死掉,界面元素不動了,不響應(yīng)了
    2012-04-04
  • C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法

    C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法

    下面小編就為大家分享一篇C# TSC打印二維碼和條形碼的實(shí)現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-01-01
  • C#使用時序數(shù)據(jù)庫InfluxDB的教程詳解

    C#使用時序數(shù)據(jù)庫InfluxDB的教程詳解

    InfluxDB是一個開源的時序數(shù)據(jù)庫,可以自動處理時間序列數(shù)據(jù),這篇文章主要為大家詳細(xì)介紹了C#如何使用InfluxDB,感興趣的小伙伴可以跟隨小編一起了解下
    2023-11-11
  • C# NullReferenceException解決案例講解

    C# NullReferenceException解決案例講解

    這篇文章主要介紹了C# NullReferenceException解決案例講解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • C#詞法分析器之詞法分析的使用詳解

    C#詞法分析器之詞法分析的使用詳解

    本篇文章介紹了,C#詞法分析器之詞法分析的使用詳解。需要的朋友參考下
    2013-05-05
  • C# WPF ListView控件的實(shí)例詳解

    C# WPF ListView控件的實(shí)例詳解

    這篇文章主要介紹了C# WPF ListView控件的實(shí)例詳解的相關(guān)資料,希望通過本能幫助到大家,讓大家掌握這部分內(nèi)容,需要的朋友可以參考下
    2017-10-10

最新評論