In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use C # winForm to customize the pop-up page effect", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use C# winForm custom pop-up page effect" this article.
The details are as follows
In C # windows forms application, add pop-up box effect. In the end, this is the effect.
There are two text boxes on the page Form2, textBox1 and textBox2. Click on any text box, according to the prepared data, pop up Form1. The number of button in Form1 is generated according to the prepared data. And the pop-up position of the Form1 should be above the text box. Finally, click any button, the value on the button will be displayed in the corresponding text box, and then the pop-up page will close.
The effect of the data displayed in the two text boxes is as follows, which is textBox2.
This is the effect of textBox1.
The main approach is to customize a user control. Because this code is changed on the basis of the paint board, so the name is not correct. The function of this user control is to generate the button number from the data, bind the click event to the button, and receive the parameter textBox. In the click event, get the text of button, then assign the text of button to the Text of the previous textBox, and then initialize the textBox. This allows you to display the value on the button button on the textBox. The generated button is placed in the flowLayoutPanel1 control so that it will be arranged automatically.
First, create a new windows forms application called ColorHelper. Then add a custom user control ColorSelector. The screenshot after the completion of the whole project looks like this.
Then in ColorSelector, drag a flowLayoutpanel control that is 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 {/ Custom color control / public partial class ColorSelector: UserControl {/ / receives the passed parameters, text boxes and data of key-value pairs. Public Control textBox = new Control (); public Dictionary dict = new Dictionary (); public Dictionary Dict {get {return dict;} set {dict = value;}} / / constructor public ColorSelector () {InitializeComponent () } / / selected text value private string selectText; public string SelectedText {get {return selectText;} set {selectText = value;}} / / selected key value private string selectKey; public string SelectedKey {get {return selectKey } set {selectKey = value;}} / generate all the original controls flowLayoutPanel1.Controls.Clear () in Button / public void LoadButton () {/ / case panel / / generate Button based on the passed dict key-value pair, and set the button size, Text, tag value, and bind mouse click events. Foreach (KeyValuePair temp in dict) {Button button1 = new Button (); button1.Text = temp.Value; button1.Tag = temp.Key; button1.Font = new Font ("Verdana", 22); button1.AutoSize = true; button1.Width = 120 Button1.MouseClick + = new MouseEventHandler (button_MouseClick); flowLayoutPanel1.Controls.Add (button1) } / bind to the event / private void button_MouseClick (object sender, MouseEventArgs e) on button {/ / declare a button and get the values on Text and Tag on button, which are value and key, respectively. Button cb = (Button) sender; selectText = cb.Text; selectKey = cb.Tag.ToString (); / / give value and key to Text and Tag of textBox, respectively. TextBox.Text = selectText; textBox.Tag = selectKey; / / redraw textBox textBox.Invalidate (); textBox.Enabled = true; / / hide the control and close the Form where the control is located this.Visible = false; this.FindForm (). Close ();}
Then the custom user control is established. You can create another project ColorTest, and then create 2 Form. Form1 put the control, Form2 put the text box, pop-up Form1.
Then add a reference to the ColorTest to reference the colorHelper.
To add to the toolbox, you need to right-click in the toolbox, select the selection, and then browse to find dll or exe. That's the effect.
You can then drag the custom control of this Colorselector onto Form1. Then the border style of Form1 is changed to None, and the AutoSize of Form must be false. And the startPosition property of Form1 should be changed to Manual and customized.
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 {/ / receives textBox and dict data public TextBox textBox; public Dictionary dict; / / has a parameter constructor, and receives the value public Form1 (TextBox textBox, Dictionary dict) {InitializeComponent () passed by Form1. This.textBox = textBox; this.dict = dict;} / / when loading, give textBox and Dict to the custom control, generate a button button, and finally display the button box private void Form1_Load (object sender, EventArgs e) {/ / set redrawing control colorSelector1.textBox = textBox / / return to the custom user control colorSelector1.Dict = dict; colorSelector1.LoadButton (); / / set the pop-up event colorSelector1.Visible = true;}}
Finally, there is the effect of Form2. That's what this is.
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) {/ / query the data first, get the returned value as an array of entities, and convert it to Dictionary Dictionary dict = new Dictionary (); dict.Add ("1", "gasoline"); dict.Add ("2", "diesel"); dict.Add ("3", "kerosene") Dict.Add ("4", "4 oil"); 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); / / when 5 button buttons are displayed on each line (dict.Count > = 5) {/ / and set 5, the size of form is directly 626, which is the result of many tests. Form.Size = new Size (626,33 * (dict.Count / 5 + 1));} else {form.Size = new Size (125x dict.Count, 33);} / / the pop-up position of form must be customized, otherwise it will not work. / on the basis of the x of the form, add the x coordinate of textBox, you can control the x coordinate of the pop-up box, and the y coordinate of the form plus the y coordinate of the form, but also consider the height form.Location = new Point of form (textBox2.Location.X + this.Location.X, this.Location.Y + textBox2.Location.Y-15 * (dict.Count / 5 + 1)). / / pop-up form form.ShowDialog () } / textBox1 mouse click event / private void textBox1_MouseClick (object sender, MouseEventArgs e) {/ / query the data first, get the returned value as an entity array, and convert it to Dictionary Dictionary dict = new Dictionary () Dict.Add ("1", "gasoline"); dict.Add ("2", "diesel"); dict.Add ("3", "kerosene"); dict.Add ("4", "4 oil"); dict.Add ("5", "5 oil"); dict.Add ("7", "6 oil") Dict.Add ("8", "7 oil"); dict.Add ("9", "8 oil"); dict.Add ("10", "9 oil"); dict.Add ("11", "10 oil"); dict.Add ("12", "6 oil"); dict.Add ("13", "7 oil") Dict.Add ("14", "8 oil"); dict.Add ("15", "9 oil"); dict.Add ("16", "10 oil"); 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 ();}
That's all the code for the pop-up box. It took me a lot of time, and I learned to calculate the value of xmemy. It is found that all displayed location values are relative to the container that contains them. TextBox is in form2, his location is relative to form2, and form2's location is relative to the screen.
Know that the FlowLayoutpanel can not be changed, every 5 change a line, you can control his container Form1, control his size. So inside the FlowLayoutpanel can not set autosize to true, can not be adaptive, otherwise it will be displayed on one line.
The above is all the content of the article "how to use C # winForm to customize the pop-up page effect". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.