in a Windows Forms application in C # , Add a popover effect . and finally, that's the effect. .
pageForm2on a2a text box, TextBox1and theTextBox2.Click any text box,according to the prepared data,PopupForm1.whichForm1in theButtonThe number is generated based on the prepared data.andForm1the pop-up position,It should be above the text box ..finally,Click on any one of the buttons,The value on the button will be,display to the corresponding text box,then the popup page closes.
The data effects shown in the two text boxes are as follows , which is textBox2.
This is the effect of textBox1 .
The main approach is,Customizing a user control.Since this code is changed on the basis of the pigment plate,so the name isn't right..The purpose of this user control is to generate data based onButtonnumber,and giveButtonbindingClickEvents,and receive ParametersTextBox.in theClickin the event,GetButtonof thetext,and give it to the previousTextBoxof theTextAssign ValueButtonof thetext,thenTextBoxInitialize.This makes it possible toTextBoxon displayButtonthe value on the button.and the generatedButtonis placed inFlowLayoutPanel1in the control,so he would automatically arrange a.
start by creating a separate Windows Forms application , called Colorhelper. then add the custom user control inside Colorselector . This is what happens after the entire project is completed. .
then Colorselector, drag a FlowLayoutPanel control 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{// lt;summary>///Custom color control///</summary> public partial class Colorselector:usercontrol { Receives data for passed parameters, text boxes, and key-value pairs. 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; }}//Constructor public Colorselector () {InitializeComponent (); }//Selected text value private string SelectText; public string SelectedText {get {return selecttext;} set {SelectText = value; } } The selected key value is private string Selectkey; public string Selectedkey {get {return selectkey;} set {Selectkey = value; }}///<summary>//Generate button///</summary> public void Loadbutton () {//Case all original controls in panel FlowLayoutPanel1.Controls.Clear (); Generates a button based on the passed Dict key value pair, sets the size of the button, Text,tag the value, and binds the mouse click event. foreach (keyvaluepair<string, string> temp in dict) {button button1 = new Button (); Button1. Text = temp. Value; Button1. Tag = temp. Key; Button1. Font = new Font ("Arial", 22); Button1. AutoSize = true; Button1. Width = 120; Button1. MouseClick + = new MouseEventHandler (Button_mouseclick); FLOWLAYOUTPANEL1.CONTROLS.ADD (button1); }}///<summary>// Bind to Event on button//</summary>//<param name= "sender" ></param>//<param name= "E" ></param> private void Button_mouseclick (object sender, MouseEventArgs e) {//DECLARE one but ton to get the values on the text and tag on the button, respectively, value and key. Button cb = (button) sender; SelectText = cb. Text; Selectkey = cb. Tag.tostring (); The value and key are given to the TextBox text and tag respectively. TextBox.Text = SelectText; Textbox.tag = Selectkey; Redraw TextBox textbox.invalidate (); Textbox.enabled = true; Hides the control and closes the form that contains the control. Visible = false; This. Findform (). Close (); } }}
The custom user control is then set up. You can create a project colortest, and then create 2 form. where Form1 put this control,Form2 put the text box, pop Form1.
then Add a reference to the Colortest and refer to the colorhelper .
and add to the toolbox, you need to right-click in the Toolbox, select Select Items, and then browse to find the DLL or exe, you can. This is the effect.
< Span lang= "en-us" > and then you can put this colorselector form1 form1 border style formborderstyle none form autosize must be false form1 startposition . The span lang= "ZH-CN" property is changed to 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 {//Receive Tex Tbox and Dict data public textbox textbox; Public dictionary<string, String> dict; A parameter constructor that receives the value passed by the Form1 public Form1 (textbox textbox, Dictionary<string, String> dict) {Initi Alizecomponent (); This.textbox = TextBox; This.dict = Dict; }//Load the TextBox and dict to the custom control and generate a button, and finally the button box private void Form1_Load (object sender, EventArgs e) {//Set redraw control Colorselector1.textbox = TextBox; Return to the custom user control colorselector1.dict = Dict; Colorselector1.loadbutton (); Set Popup Event colorselector1.visible = true; } }}the last is the effect of Form2 . That's the way it 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) {//Go to query data first, get the return value as an entity array, turn to Dict Ionary<string,string> dictionary<string, string> dict = new dictionary<string, string> (); Dict. ADD ("1", "petrol"); 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); Each row displays 5 button buttons if (Dict. Count >= 5) {//and set 5, the size of the form, directly 626, this is the result of multiple tests. Form. size = new Size (626, * (dict). COUNT/5 + 1)); } else {form. size = new Size (Dict *). Count, 33); }//form Pop-up location, you must set Form2 startposition as custom, otherwise it will work. On the basis of the X of the form, plus the x-coordinate of the textbox, you can control the x-coordinate of the popup, and the y-coordinate of the form plus the y-coordinate of the form, and also consider the height form. Location = new Point (textbox2.location.x + this. Location.x, this. LOCATION.Y + textbox2.location.y-15 * (Dict. COUNT/5 + 1)); Popup form Form. ShowDialog (); }///<summary>//TextBox1 mouse click events///</summary>//<param name= "Sender" > </param>//<param name= "E" ></param> private void Textbox1_mouseclick (object sender, Mous Eeventargs e) {//Go to query data first, get the return value as an entity array, turn to dictionary<string,string> dictionary<string, String> dict = new dictionary<string, string> (); Dict. ADD ("1", "petrol"); 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, * (dict). count/5+1)); } else {form. size = new Size (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 (); } }}
The above is the full code of the popup box. It took me a lot of time, and I learned to count the values of x,y . The values of all displayed location are found, relative to the container that contains them. the textBox is in Form2 , and his location is relative to Form2 , while Form2 's location is relative to the screen.
I know it's not going to change. FlowLayoutPanel, every 5 lines , you can control his container Form1 , to control his size. So the inside of the FlowLayoutPanel can not be set autosize to true, not adaptive, Otherwise he would have shown the line.
C # WinForm Custom pop-up page