Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.Eventing.Reader;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace DatabankFiller {
- public sealed class CredentialsBox {
- #region Property
- public static Color BackColor { get; set; } = Color.White;
- public static Color ForeColor { get; set; } = Color.Black;
- #endregion
- #region Designer
- private static DialogResult _ShowLoginBox(string message,string title,Bitmap icon,ref string login,ref string password) {
- Form crdBox = new Form();
- crdBox.Text = title;
- crdBox.ShowIcon = false;
- crdBox.MaximizeBox = false;
- crdBox.MinimizeBox = false;
- crdBox.BackColor = BackColor;
- crdBox.ForeColor = ForeColor;
- crdBox.StartPosition = FormStartPosition.CenterScreen;
- crdBox.FormBorderStyle = FormBorderStyle.FixedDialog;
- PictureBox pictureBox = new PictureBox();
- pictureBox.Image = icon;
- pictureBox.Location = new Point(10, 10);
- pictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
- //pictureBox.BorderStyle = BorderStyle.FixedSingle;
- if(pictureBox.Size.Width > 64 || pictureBox.Size.Height > 64) {
- pictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
- pictureBox.Size = new Size(64, 64);
- }
- crdBox.Controls.Add(pictureBox);
- Label lbl = new Label();
- lbl.Text = message;
- int lblWidth = crdBox.ClientSize.Width - icon.Width - 30;
- lbl.MaximumSize = new Size(lblWidth, 500);
- lbl.AutoSize = true;
- //lbl.BorderStyle = BorderStyle.FixedSingle;
- crdBox.Controls.Add(lbl);
- int grpBoxStart = 0;
- if(lbl.Height > icon.Height) {
- //Labelhoehe ist groeßer Iconhoehe
- double labelMid = lbl.Height / 2.0;
- double iconMid = icon.Height / 2.0;
- pictureBox.Location = new Point(10, 10 + (int)(labelMid - iconMid));
- lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10);
- grpBoxStart = lbl.Location.Y + lbl.Height + 10;
- }else if(lbl .Height < icon.Height) {
- //Labelhoehe ist kleiner Iconhoehe
- double labelMid = lbl.Height / 2.0;
- double iconMid = icon.Height / 2.0;
- pictureBox.Location = new Point(10, 10);
- lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10 + (int)(iconMid - labelMid ));
- grpBoxStart = pictureBox.Location.Y + pictureBox.Height + 10;
- } else {
- //Gleich
- pictureBox.Location = new Point(10, 10);
- lbl.Location = new Point(pictureBox.Location.X + pictureBox.Width + 10, 10);
- grpBoxStart = lbl.Location.Y + lbl.Height + 10;
- }
- //GroupBox mit Textboxen und Labels
- GroupBox grpBox = new GroupBox();
- grpBox.Text = "Login";
- grpBox.ForeColor = ForeColor;
- grpBox.Size = new Size(crdBox.ClientSize.Width - 20, 40);
- grpBox.Location = new Point(10, grpBoxStart);
- crdBox.Controls.Add(grpBox);
- //Wir holen Abstand 5.
- Label lgn = new Label();
- lgn.Text = "Anmeldename: ";
- lgn.AutoSize = true;
- grpBox.Controls.Add(lgn);
- lgn.Location = new Point( 5, 15);
- TextBox lgnName = new TextBox();
- lgnName.Width = grpBox.ClientSize.Width - 15 - lgn.Width;
- lgnName.Location = new Point(grpBox.ClientSize.Width - lgnName.Width - 5, 15);
- grpBox.Controls.Add(lgnName);
- double TextBoxNameMid = lgnName.Height / 2.0;
- double LabelName = lgn.Height / 2.0;
- lgn.Location = new Point(5,15 + (int)(TextBoxNameMid - LabelName));
- TextBox txtBoxPW = new TextBox();
- txtBoxPW.Width = grpBox.ClientSize.Width - 15 - lgn.Width;
- txtBoxPW.Location = new Point(lgnName.Location.X, lgnName.Location.Y + lgnName.Height + 5);
- txtBoxPW.UseSystemPasswordChar = true;
- grpBox.Controls.Add(txtBoxPW);
- grpBox.Height = txtBoxPW.Location.Y + txtBoxPW.Height + 10;
- Label kwt = new Label();
- kwt.Text = "Kennwort: ";
- kwt.AutoSize = true;
- grpBox.Controls.Add(kwt);
- double TextBoxNameMid2 = txtBoxPW.Height / 2.0;
- double LabelName2 = kwt.Height / 2.0;
- kwt.Location = new Point(5, txtBoxPW.Location.Y + (int)(TextBoxNameMid2 - LabelName2));
- //Ende Groupbox
- Button abbr = new Button();
- abbr.Text = "&Abbrechen";
- abbr.Location = new Point(crdBox.ClientSize.Width - 10 - abbr.Width, grpBox.Location.Y + grpBox.Height + 5);
- abbr.DialogResult = DialogResult.Cancel;
- crdBox.Controls.Add(abbr);
- Button loginbutton = new Button();
- loginbutton.Text = "&Login";
- loginbutton.Location = new Point(abbr.Location.X - 5 - loginbutton.Width, abbr.Location.Y);
- loginbutton.DialogResult = DialogResult.OK;
- crdBox.Controls.Add(loginbutton);
- crdBox.ClientSize = new Size(crdBox.ClientSize.Width, loginbutton.Location.Y + 5 + loginbutton.Height);
- crdBox.AcceptButton = loginbutton;
- crdBox.CancelButton = abbr;
- DialogResult result = crdBox.ShowDialog();
- if(result == DialogResult.OK) {
- login = lgnName.Text;
- password = txtBoxPW.Text;
- }
- //Aufräumen
- foreach (Control control in crdBox.Controls) {
- foreach (Control innerControl in control.Controls) {
- innerControl.Dispose();
- }
- control.Dispose();
- }
- crdBox.Dispose();
- return result;
- }
- #endregion
- #region Aufrufen
- /// <summary>
- /// Erstellt einen Logindialog
- /// </summary>
- /// <param name="message">Die Nachricht die angezeigt werden soll.</param>
- /// <param name="title">Der Title im Fensterrahmen</param>
- /// <param name="ico">Eine Bitmap zum anzeigen</param>
- /// <param name="login">Loginstring zum speichern</param>
- /// <param name="password">Passwordstring zum speichern</param>
- /// <returns></returns>
- public static DialogResult ShowLoginBox(string message, string title, Bitmap icon, ref string login, ref string password) {
- return _ShowLoginBox(message, title, icon, ref login,ref password) ;
- }
- /// <summary>
- /// Erstellt einen Logindialog
- /// </summary>
- /// <param name="message">Die Nachricht die angezeigt werden soll.</param>
- /// <param name="title">Der Title im Fensterrahmen</param>
- /// <param name="ico">Viele Icons bekommen sie aus SystemIcons....</param>
- /// <param name="login">Loginstring zum speichern</param>
- /// <param name="password">Passwordstring zum speichern</param>
- /// <returns></returns>
- public static DialogResult ShowLoginBox(string message, string title, Icon ico, ref string login, ref string password) {
- Bitmap bico = ico.ToBitmap();
- return _ShowLoginBox(message, title, bico, ref login, ref password);
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement