Advertisement
Farliam

DoubleInputBox

Jun 15th, 2023
758
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.73 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics.Eventing.Reader;
  4. using System.Drawing;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Windows.Forms;
  9.  
  10. namespace DatabankFiller {
  11.     public sealed class CredentialsBox {
  12.  
  13.         #region Property
  14.         public static Color BackColor { get; set; } = Color.White;
  15.         public static Color ForeColor { get; set; } = Color.Black;
  16.         #endregion
  17.  
  18.         #region Designer
  19.  
  20.         private static DialogResult _ShowLoginBox(string message,string title,Bitmap icon,ref string login,ref string password) {
  21.             Form crdBox = new Form();
  22.             crdBox.Text = title;
  23.             crdBox.ShowIcon = false;
  24.             crdBox.MaximizeBox = false;
  25.             crdBox.MinimizeBox = false;
  26.             crdBox.BackColor = BackColor;
  27.             crdBox.ForeColor = ForeColor;
  28.             crdBox.StartPosition = FormStartPosition.CenterScreen;
  29.             crdBox.FormBorderStyle = FormBorderStyle.FixedDialog;
  30.  
  31.             PictureBox pictureBox = new PictureBox();
  32.             pictureBox.Image = icon;
  33.             pictureBox.Location = new Point(10, 10);
  34.             pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
  35.             //pictureBox.BorderStyle = BorderStyle.FixedSingle;
  36.             if(pictureBox.Size.Width > 64 || pictureBox.Size.Height > 64) {
  37.                 pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
  38.                 pictureBox.Size = new Size(64, 64);
  39.             }
  40.             crdBox.Controls.Add(pictureBox);
  41.  
  42.             Label lbl = new Label();
  43.             lbl.Text = message;
  44.             int lblWidth = crdBox.ClientSize.Width - icon.Width - 30;
  45.             lbl.MaximumSize = new Size(lblWidth, 500);
  46.             lbl.AutoSize = true;
  47.             //lbl.BorderStyle = BorderStyle.FixedSingle;
  48.             crdBox.Controls.Add(lbl);
  49.  
  50.             int grpBoxStart = 0;
  51.  
  52.             if(lbl.Height > icon.Height) {
  53.                 //Labelhoehe ist groeßer Iconhoehe
  54.                 double labelMid = lbl.Height / 2.0;
  55.                 double iconMid = icon.Height / 2.0;
  56.                 pictureBox.Location = new Point(10, 10 + (int)(labelMid - iconMid));
  57.                 lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10);
  58.                 grpBoxStart = lbl.Location.Y + lbl.Height + 10;
  59.             }else if(lbl .Height < icon.Height) {
  60.                 //Labelhoehe ist kleiner Iconhoehe
  61.                 double labelMid = lbl.Height / 2.0;
  62.                 double iconMid = icon.Height / 2.0;
  63.                 pictureBox.Location = new Point(10, 10);
  64.                 lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10 + (int)(iconMid - labelMid ));
  65.                 grpBoxStart = pictureBox.Location.Y + pictureBox.Height + 10;
  66.             } else {
  67.                 //Gleich
  68.                 pictureBox.Location = new Point(10, 10);
  69.                 lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10);
  70.                 grpBoxStart = lbl.Location.Y + lbl.Height + 10;
  71.             }
  72.  
  73.             //GroupBox mit Textboxen und Labels
  74.  
  75.             GroupBox grpBox = new GroupBox();
  76.             grpBox.Text = "Login";
  77.             grpBox.ForeColor = ForeColor;
  78.             grpBox.Size = new Size(crdBox.ClientSize.Width - 20, 40);
  79.             grpBox.Location = new Point(10, grpBoxStart);      
  80.             crdBox.Controls.Add(grpBox);
  81.  
  82.             //Wir holen Abstand 5.
  83.             Label lgn = new Label();
  84.             lgn.Text = "Anmeldename: ";
  85.             lgn.AutoSize = true;
  86.             grpBox.Controls.Add(lgn);
  87.             lgn.Location = new Point( 5,  15);
  88.  
  89.  
  90.             TextBox lgnName = new TextBox();
  91.             lgnName.Width = grpBox.ClientSize.Width - 15 - lgn.Width;
  92.             lgnName.Location = new Point(grpBox.ClientSize.Width - lgnName.Width - 5, 15);
  93.             grpBox.Controls.Add(lgnName);
  94.             double TextBoxNameMid = lgnName.Height / 2.0;
  95.             double LabelName = lgn.Height / 2.0;
  96.             lgn.Location = new Point(5,15 + (int)(TextBoxNameMid - LabelName));
  97.  
  98.             TextBox txtBoxPW = new TextBox();
  99.             txtBoxPW.Width = grpBox.ClientSize.Width - 15 - lgn.Width;
  100.             txtBoxPW.Location = new Point(lgnName.Location.X, lgnName.Location.Y + lgnName.Height + 5);
  101.             txtBoxPW.UseSystemPasswordChar = true;
  102.             grpBox.Controls.Add(txtBoxPW);
  103.             grpBox.Height = txtBoxPW.Location.Y + txtBoxPW.Height + 10;
  104.  
  105.  
  106.             Label kwt = new Label();
  107.             kwt.Text = "Kennwort: ";
  108.             kwt.AutoSize = true;
  109.             grpBox.Controls.Add(kwt);
  110.             double TextBoxNameMid2 = txtBoxPW.Height / 2.0;
  111.             double LabelName2 = kwt.Height / 2.0;
  112.             kwt.Location = new Point(5, txtBoxPW.Location.Y + (int)(TextBoxNameMid2 - LabelName2));
  113.  
  114.             //Ende Groupbox
  115.  
  116.             Button abbr = new Button();
  117.             abbr.Text = "&Abbrechen";
  118.             abbr.Location = new Point(crdBox.ClientSize.Width - 10 - abbr.Width, grpBox.Location.Y + grpBox.Height + 5);
  119.             abbr.DialogResult = DialogResult.Cancel;
  120.             crdBox.Controls.Add(abbr);
  121.  
  122.             Button loginbutton = new Button();
  123.             loginbutton.Text = "&Login";
  124.             loginbutton.Location = new Point(abbr.Location.X - 5 - loginbutton.Width, abbr.Location.Y);
  125.             loginbutton.DialogResult = DialogResult.OK;
  126.             crdBox.Controls.Add(loginbutton);
  127.  
  128.             crdBox.ClientSize = new Size(crdBox.ClientSize.Width, loginbutton.Location.Y + 5 + loginbutton.Height);
  129.  
  130.             crdBox.AcceptButton = loginbutton;
  131.             crdBox.CancelButton = abbr;
  132.  
  133.             DialogResult result = crdBox.ShowDialog();
  134.  
  135.             if(result == DialogResult.OK) {
  136.                 login = lgnName.Text;
  137.                 password = txtBoxPW.Text;
  138.             }
  139.  
  140.             //Aufräumen
  141.             foreach (Control control in crdBox.Controls) {
  142.                 foreach (Control innerControl in control.Controls) {
  143.                     innerControl.Dispose();
  144.                 }
  145.                 control.Dispose();
  146.             }
  147.             crdBox.Dispose();
  148.  
  149.             return result;          
  150.         }
  151.  
  152.         #endregion
  153.  
  154.         #region Aufrufen
  155.         /// <summary>
  156.         /// Erstellt einen Logindialog  
  157.         /// </summary>
  158.         /// <param name="message">Die Nachricht die angezeigt werden soll.</param>
  159.         /// <param name="title">Der Title im Fensterrahmen</param>
  160.         /// <param name="ico">Eine Bitmap zum anzeigen</param>
  161.         /// <param name="login">Loginstring zum speichern</param>
  162.         /// <param name="password">Passwordstring zum speichern</param>
  163.         /// <returns></returns>
  164.         public static DialogResult ShowLoginBox(string message, string title, Bitmap icon, ref string login, ref string password) {
  165.             return  _ShowLoginBox(message, title, icon, ref login,ref password) ;
  166.         }
  167.  
  168.         /// <summary>
  169.         /// Erstellt einen Logindialog  
  170.         /// </summary>
  171.         /// <param name="message">Die Nachricht die angezeigt werden soll.</param>
  172.         /// <param name="title">Der Title im Fensterrahmen</param>
  173.         /// <param name="ico">Viele Icons bekommen sie aus SystemIcons....</param>
  174.         /// <param name="login">Loginstring zum speichern</param>
  175.         /// <param name="password">Passwordstring zum speichern</param>
  176.         /// <returns></returns>
  177.         public static DialogResult ShowLoginBox(string message, string title, Icon ico, ref string login, ref string password) {
  178.             Bitmap bico =  ico.ToBitmap();
  179.             return _ShowLoginBox(message, title, bico, ref login, ref password);
  180.         }
  181.  
  182.         #endregion
  183.  
  184.     }
  185.  
  186. }
  187.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement