How to take a snapshot with Ozeski SDK USB Camera? - c#

I'm quite newbie in programming, but triyng to run my first app in VS2019, C# .NET. I want to take a snapshop from my USB microscope. I've found OZEKI SDK library and I'm able to connect and view image from microscope. Ozeki has a tutorial on youtube:
https://www.youtube.com/watch?v=xjMAfMAix9c
and their website,
https://www.camera-sdk.com/p_6553-how-to-take-a-picture-snapshot-and-save-it-as-.jpg-in-c-.html
but when I tried to run app, I got message:
" images.save(curentpath)" object does not contain a definition for 'save' and no accesible...."
Code:
using System;
using System.Drawing;
using System.Windows.Forms;
using Ozeki.Media;
using Ozeki.Camera;
namespace WindowsFormsApp3
{
public partial class Form1 : Form
{
private DrawingImageProvider _imageProvider;
private MediaConnector _connector;
private VideoViewerWF _videoViewerWf;
private SnapshotHandler _snapshotHandler;
private IWebCamera _webCamera;
public Form1()
{
InitializeComponent();
_imageProvider = new DrawingImageProvider();
_connector = new MediaConnector();
_snapshotHandler = new SnapshotHandler();
_videoViewerWf = new VideoViewerWF();
SetVideoViewer();
}
private void SetVideoViewer()
{
CameraBox.Controls.Add(_videoViewerWf);
_videoViewerWf.Size = new Size(260, 180);
_videoViewerWf.BackColor = Color.Black;
_videoViewerWf.TabStop = false;
_videoViewerWf.Location = new Point(14, 19);
_videoViewerWf.Name = "_videoViewerWf";
}
private void button_Connect_Click(object sender, EventArgs e)
{
_webCamera = new WebCamera();
if (_webCamera != null)
{
_connector.Connect(_webCamera.VideoChannel, _imageProvider);
_connector.Connect(_webCamera.VideoChannel,_snapshotHandler);
_videoViewerWf.SetImageProvider(_imageProvider);
_webCamera.Start();
_videoViewerWf.Start();
}
}
private void button_SaveTo_Click(object sender, EventArgs e)
{
var result = folderBrowserDialog1.ShowDialog();
if (result == DialogResult.OK)
TextBox_SaveTo.Text = folderBrowserDialog1.SelectedPath;
}
private void Btn_Snapshot_Click(object sender, EventArgs e)
{
var path = TextBox_SaveTo.Text;
CreateSnapShot(path);
}
private void CreateSnapShot(string path)
{
var date = DateTime.Now.Year + "y-" + DateTime.Now.Month + "m-" + DateTime.Now.Day + "d-" +
DateTime.Now.Hour + "h-" + DateTime.Now.Minute + "m-" + DateTime.Now.Second + "s";
string currentpath;
if (String.IsNullOrEmpty(path))
currentpath = date + ".jpg";
else
currentpath = path + "\\" + date + ".jpg";
var snapShotImage = _snapshotHandler.TakeSnapshot().ToImage();
snapShotImage.Save(currentpath, System.Drawing.Imaging.ImageFormat.Jpeg);
}
}
}
and Designer:
namespace WindowsFormsApp3
{
partial class Form1
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitializeComponent()
{
this.folderBrowserDialog1 = new System.Windows.Forms.FolderBrowserDialog();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.button_Connect = new System.Windows.Forms.Button();
this.CameraBox = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.TextBox_SaveTo = new System.Windows.Forms.TextBox();
this.Btn_Snapshot = new System.Windows.Forms.Button();
this.button_SaveTo1 = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.button_Connect);
this.groupBox1.Location = new System.Drawing.Point(10, 10);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(120, 60);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Connect";
//
// button_Connect
//
this.button_Connect.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(238)));
this.button_Connect.ForeColor = System.Drawing.Color.Black;
this.button_Connect.Location = new System.Drawing.Point(10, 20);
this.button_Connect.Name = "button_Connect";
this.button_Connect.Size = new System.Drawing.Size(100, 25);
this.button_Connect.TabIndex = 6;
this.button_Connect.Text = "Connect";
this.button_Connect.UseVisualStyleBackColor = true;
this.button_Connect.Click += new System.EventHandler(this.button_Connect_Click);
//
// CameraBox
//
this.CameraBox.Location = new System.Drawing.Point(10, 80);
this.CameraBox.Name = "CameraBox";
this.CameraBox.Size = new System.Drawing.Size(285, 210);
this.CameraBox.TabIndex = 3;
this.CameraBox.TabStop = false;
this.CameraBox.Text = "Live camera ";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.TextBox_SaveTo);
this.groupBox2.Controls.Add(this.Btn_Snapshot);
this.groupBox2.Controls.Add(this.button_SaveTo1);
this.groupBox2.Location = new System.Drawing.Point(10, 300);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(285, 100);
this.groupBox2.TabIndex = 37;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Snapshot";
//
// TextBox_SaveTo
//
this.TextBox_SaveTo.Location = new System.Drawing.Point(119, 63);
this.TextBox_SaveTo.Name = "TextBox_SaveTo";
this.TextBox_SaveTo.Size = new System.Drawing.Size(160, 20);
this.TextBox_SaveTo.TabIndex = 35;
this.TextBox_SaveTo.Text = "C:\\Users\\user\\Documents\\Visual Studio 2012\\Projects\\03_Onvif_Network_Video_Record" +
"er\\03_Onvif_Network_Video_Recorder\\bin\\Debug";
//
// Btn_Snapshot
//
this.Btn_Snapshot.Location = new System.Drawing.Point(10, 20);
this.Btn_Snapshot.Name = "Btn_Snapshot";
this.Btn_Snapshot.Size = new System.Drawing.Size(100, 25);
this.Btn_Snapshot.TabIndex = 36;
this.Btn_Snapshot.Text = "Take a snapshot";
this.Btn_Snapshot.UseVisualStyleBackColor = true;
this.Btn_Snapshot.Click += new System.EventHandler(this.Btn_Snapshot_Click);
//
// button_SaveTo1
//
this.button_SaveTo1.Location = new System.Drawing.Point(10, 60);
this.button_SaveTo1.Name = "button_SaveTo1";
this.button_SaveTo1.Size = new System.Drawing.Size(100, 25);
this.button_SaveTo1.TabIndex = 34;
this.button_SaveTo1.Text = "Save to:";
this.button_SaveTo1.UseVisualStyleBackColor = true;
this.button_SaveTo1.Click += new System.EventHandler(this.button_SaveTo_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(309, 414);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.CameraBox);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
this.MaximizeBox = false;
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Network Video Recorder Snapshot";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
}
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button button_Connect;
private System.Windows.Forms.GroupBox CameraBox;
private System.Windows.Forms.TextBox TextBox_SaveTo;
private System.Windows.Forms.Button button_SaveTo1;
private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog1;
private System.Windows.Forms.Button Btn_Snapshot;
private System.Windows.Forms.GroupBox groupBox2;
}
}
Thanks in advance for any help.

OK, I did it. Change:
var snapShotImage = _snapshotHandler.TakeSnapshot().ToImage();
to
var snapShotImage = _snapShot.TakeSnapshot().ToImage() as System.Drawing.Image;

Related

DevExpress Winforms: why aren't my newly added Labelcontrols showing up?

I've inherited this project that someone started on a while back. It's made with DevExpress for Winforms. He already had a "welcome" page ready, with the "welkom" label and the "ik heb een afspraak" label (top of the 3 bottom ones). I've just started working on it today and can't seem to find out why my newly added Labelcontrols (the 2 bottom ones) aren't showing up when I launch the program/winforms.
Here's an image of how the designer looks:
Here's the Designer code page (Designer.cs file):
partial class Welcome
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.layoutCtrlMain = new DevExpress.XtraLayout.LayoutControl();
this.lblPickupRepair = new DevExpress.XtraEditors.LabelControl();
this.lblNoAppointment = new DevExpress.XtraEditors.LabelControl();
this.lblAppointment = new DevExpress.XtraEditors.LabelControl();
this.lblWelcome = new DevExpress.XtraEditors.LabelControl();
this.layoutMain = new DevExpress.XtraLayout.LayoutControlGroup();
this.emptySpaceItem1 = new DevExpress.XtraLayout.EmptySpaceItem();
this.emptySpaceItem2 = new DevExpress.XtraLayout.EmptySpaceItem();
this.layoutControlItem1 = new DevExpress.XtraLayout.LayoutControlItem();
this.emptySpaceItem3 = new DevExpress.XtraLayout.EmptySpaceItem();
this.emptySpaceItem4 = new DevExpress.XtraLayout.EmptySpaceItem();
this.emptySpaceItem5 = new DevExpress.XtraLayout.EmptySpaceItem();
this.layoutControlItem2 = new DevExpress.XtraLayout.LayoutControlItem();
this.emptySpaceItem6 = new DevExpress.XtraLayout.EmptySpaceItem();
this.layoutControlItem4 = new DevExpress.XtraLayout.LayoutControlItem();
this.layoutControlItem3 = new DevExpress.XtraLayout.LayoutControlItem();
((System.ComponentModel.ISupportInitialize)(this.layoutCtrlMain)).BeginInit();
this.layoutCtrlMain.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.layoutMain)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem3)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem5)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem6)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem4)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).BeginInit();
this.SuspendLayout();
//
// layoutCtrlMain
//
this.layoutCtrlMain.Controls.Add(this.lblPickupRepair);
this.layoutCtrlMain.Controls.Add(this.lblNoAppointment);
this.layoutCtrlMain.Controls.Add(this.lblAppointment);
this.layoutCtrlMain.Controls.Add(this.lblWelcome);
this.layoutCtrlMain.Dock = System.Windows.Forms.DockStyle.Fill;
this.layoutCtrlMain.Location = new System.Drawing.Point(0, 0);
this.layoutCtrlMain.Name = "layoutCtrlMain";
this.layoutCtrlMain.OptionsCustomizationForm.DesignTimeCustomizationFormPositionAndSize = new System.Drawing.Rectangle(2218, 466, 859, 400);
this.layoutCtrlMain.Root = this.layoutMain;
this.layoutCtrlMain.Size = new System.Drawing.Size(1131, 427);
this.layoutCtrlMain.TabIndex = 0;
//
// lblPickupRepair
//
this.lblPickupRepair.Appearance.Font = new System.Drawing.Font("Tahoma", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblPickupRepair.Appearance.ForeColor = System.Drawing.Color.White;
this.lblPickupRepair.Appearance.Image = global::ESC.Intern.Custom.DigRecWF.Reception.Properties.Resources.Webp_net_resizeimage;
this.lblPickupRepair.Appearance.Options.UseFont = true;
this.lblPickupRepair.Appearance.Options.UseForeColor = true;
this.lblPickupRepair.Appearance.Options.UseImage = true;
this.lblPickupRepair.ImageAlignToText = DevExpress.XtraEditors.ImageAlignToText.LeftCenter;
this.lblPickupRepair.IndentBetweenImageAndText = 15;
this.lblPickupRepair.Location = new System.Drawing.Point(355, 309);
this.lblPickupRepair.Name = "lblPickupRepair";
this.lblPickupRepair.Size = new System.Drawing.Size(326, 49);
this.lblPickupRepair.StyleController = this.layoutCtrlMain;
this.lblPickupRepair.TabIndex = 8;
this.lblPickupRepair.Text = "Afhaling / herstelling";
//
// lblNoAppointment
//
this.lblNoAppointment.Appearance.Font = new System.Drawing.Font("Tahoma", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblNoAppointment.Appearance.ForeColor = System.Drawing.Color.White;
this.lblNoAppointment.Appearance.Image = global::ESC.Intern.Custom.DigRecWF.Reception.Properties.Resources.Webp_net_resizeimage;
this.lblNoAppointment.Appearance.Options.UseFont = true;
this.lblNoAppointment.Appearance.Options.UseForeColor = true;
this.lblNoAppointment.Appearance.Options.UseImage = true;
this.lblNoAppointment.ImageAlignToText = DevExpress.XtraEditors.ImageAlignToText.LeftCenter;
this.lblNoAppointment.IndentBetweenImageAndText = 15;
this.lblNoAppointment.Location = new System.Drawing.Point(355, 256);
this.lblNoAppointment.Name = "lblNoAppointment";
this.lblNoAppointment.Size = new System.Drawing.Size(331, 49);
this.lblNoAppointment.StyleController = this.layoutCtrlMain;
this.lblNoAppointment.TabIndex = 7;
this.lblNoAppointment.Text = "Ik heb geen afspraak";
this.lblNoAppointment.Click += new System.EventHandler(this.lblNoAppointment_Click);
//
// lblAppointment
//
this.lblAppointment.Appearance.Font = new System.Drawing.Font("Tahoma", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblAppointment.Appearance.ForeColor = System.Drawing.Color.White;
this.lblAppointment.Appearance.Image = global::ESC.Intern.Custom.DigRecWF.Reception.Properties.Resources.Webp_net_resizeimage;
this.lblAppointment.Appearance.Options.UseFont = true;
this.lblAppointment.Appearance.Options.UseForeColor = true;
this.lblAppointment.Appearance.Options.UseImage = true;
this.lblAppointment.ImageAlignToText = DevExpress.XtraEditors.ImageAlignToText.LeftCenter;
this.lblAppointment.IndentBetweenImageAndText = 15;
this.lblAppointment.Location = new System.Drawing.Point(355, 203);
this.lblAppointment.Name = "lblAppointment";
this.lblAppointment.Size = new System.Drawing.Size(315, 49);
this.lblAppointment.StyleController = this.layoutCtrlMain;
this.lblAppointment.TabIndex = 5;
this.lblAppointment.Text = "Ik heb een afspraak";
this.lblAppointment.Click += new System.EventHandler(this.lblAppointment_Click);
//
// lblWelcome
//
this.lblWelcome.Appearance.Font = new System.Drawing.Font("Arial Narrow", 48F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblWelcome.Appearance.ForeColor = System.Drawing.SystemColors.ControlLightLight;
this.lblWelcome.Appearance.Options.UseFont = true;
this.lblWelcome.Appearance.Options.UseForeColor = true;
this.lblWelcome.Location = new System.Drawing.Point(290, 70);
this.lblWelcome.Name = "lblWelcome";
this.lblWelcome.Size = new System.Drawing.Size(396, 75);
this.lblWelcome.StyleController = this.layoutCtrlMain;
this.lblWelcome.TabIndex = 4;
this.lblWelcome.Text = "Welkom bij ESC!";
//
// layoutMain
//
this.layoutMain.EnableIndentsWithoutBorders = DevExpress.Utils.DefaultBoolean.True;
this.layoutMain.GroupBordersVisible = false;
this.layoutMain.Items.AddRange(new DevExpress.XtraLayout.BaseLayoutItem[] {
this.emptySpaceItem1,
this.emptySpaceItem2,
this.layoutControlItem1,
this.emptySpaceItem3,
this.emptySpaceItem4,
this.emptySpaceItem5,
this.layoutControlItem2,
this.emptySpaceItem6,
this.layoutControlItem4,
this.layoutControlItem3});
this.layoutMain.Name = "Root";
this.layoutMain.Size = new System.Drawing.Size(1131, 427);
this.layoutMain.TextVisible = false;
//
// emptySpaceItem1
//
this.emptySpaceItem1.AllowHotTrack = false;
this.emptySpaceItem1.Location = new System.Drawing.Point(678, 0);
this.emptySpaceItem1.MinSize = new System.Drawing.Size(104, 24);
this.emptySpaceItem1.Name = "emptySpaceItem1";
this.emptySpaceItem1.Size = new System.Drawing.Size(433, 407);
this.emptySpaceItem1.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
this.emptySpaceItem1.TextSize = new System.Drawing.Size(0, 0);
//
// emptySpaceItem2
//
this.emptySpaceItem2.AllowHotTrack = false;
this.emptySpaceItem2.Location = new System.Drawing.Point(0, 0);
this.emptySpaceItem2.Name = "emptySpaceItem2";
this.emptySpaceItem2.Size = new System.Drawing.Size(278, 407);
this.emptySpaceItem2.TextSize = new System.Drawing.Size(0, 0);
//
// layoutControlItem1
//
this.layoutControlItem1.Control = this.lblWelcome;
this.layoutControlItem1.Location = new System.Drawing.Point(278, 58);
this.layoutControlItem1.Name = "layoutControlItem1";
this.layoutControlItem1.Size = new System.Drawing.Size(400, 79);
this.layoutControlItem1.TextSize = new System.Drawing.Size(0, 0);
this.layoutControlItem1.TextVisible = false;
//
// emptySpaceItem3
//
this.emptySpaceItem3.AllowHotTrack = false;
this.emptySpaceItem3.Location = new System.Drawing.Point(278, 350);
this.emptySpaceItem3.MinSize = new System.Drawing.Size(104, 24);
this.emptySpaceItem3.Name = "emptySpaceItem3";
this.emptySpaceItem3.Size = new System.Drawing.Size(400, 57);
this.emptySpaceItem3.SizeConstraintsType = DevExpress.XtraLayout.SizeConstraintsType.Custom;
this.emptySpaceItem3.TextSize = new System.Drawing.Size(0, 0);
//
// emptySpaceItem4
//
this.emptySpaceItem4.AllowHotTrack = false;
this.emptySpaceItem4.Location = new System.Drawing.Point(278, 0);
this.emptySpaceItem4.Name = "emptySpaceItem4";
this.emptySpaceItem4.Size = new System.Drawing.Size(400, 58);
this.emptySpaceItem4.TextSize = new System.Drawing.Size(0, 0);
//
// emptySpaceItem5
//
this.emptySpaceItem5.AllowHotTrack = false;
this.emptySpaceItem5.Location = new System.Drawing.Point(278, 137);
this.emptySpaceItem5.Name = "emptySpaceItem5";
this.emptySpaceItem5.Size = new System.Drawing.Size(400, 54);
this.emptySpaceItem5.TextSize = new System.Drawing.Size(0, 0);
//
// layoutControlItem2
//
this.layoutControlItem2.Control = this.lblAppointment;
this.layoutControlItem2.Location = new System.Drawing.Point(343, 191);
this.layoutControlItem2.Name = "layoutControlItem2";
this.layoutControlItem2.Size = new System.Drawing.Size(335, 53);
this.layoutControlItem2.TextSize = new System.Drawing.Size(0, 0);
this.layoutControlItem2.TextVisible = false;
//
// emptySpaceItem6
//
this.emptySpaceItem6.AllowHotTrack = false;
this.emptySpaceItem6.Location = new System.Drawing.Point(278, 191);
this.emptySpaceItem6.Name = "emptySpaceItem6";
this.emptySpaceItem6.Size = new System.Drawing.Size(65, 159);
this.emptySpaceItem6.TextSize = new System.Drawing.Size(0, 0);
//
// layoutControlItem4
//
this.layoutControlItem4.Control = this.lblNoAppointment;
this.layoutControlItem4.Location = new System.Drawing.Point(343, 244);
this.layoutControlItem4.Name = "layoutControlItem4";
this.layoutControlItem4.Size = new System.Drawing.Size(335, 53);
this.layoutControlItem4.TextSize = new System.Drawing.Size(0, 0);
this.layoutControlItem4.TextVisible = false;
//
// layoutControlItem3
//
this.layoutControlItem3.Control = this.lblPickupRepair;
this.layoutControlItem3.Location = new System.Drawing.Point(343, 297);
this.layoutControlItem3.Name = "layoutControlItem3";
this.layoutControlItem3.Size = new System.Drawing.Size(335, 53);
this.layoutControlItem3.TextSize = new System.Drawing.Size(0, 0);
this.layoutControlItem3.TextVisible = false;
//
// Welcome
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(1131, 427);
this.Controls.Add(this.layoutCtrlMain);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Welcome";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Welcome_Load);
this.Shown += new System.EventHandler(this.Welcome_Shown);
((System.ComponentModel.ISupportInitialize)(this.layoutCtrlMain)).EndInit();
this.layoutCtrlMain.ResumeLayout(false);
this.layoutCtrlMain.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.layoutMain)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem1)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem3)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem5)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.emptySpaceItem6)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem4)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.layoutControlItem3)).EndInit();
this.ResumeLayout(false);
}
#endregion
private DevExpress.XtraLayout.LayoutControl layoutCtrlMain;
private DevExpress.XtraLayout.LayoutControlGroup layoutMain;
private DevExpress.XtraEditors.LabelControl lblWelcome;
private DevExpress.XtraLayout.LayoutControlItem layoutControlItem1;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem4;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem5;
private DevExpress.XtraEditors.LabelControl lblAppointment;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem6;
private DevExpress.XtraEditors.LabelControl lblPickupRepair;
private DevExpress.XtraEditors.LabelControl lblNoAppointment;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem3;
private DevExpress.XtraLayout.LayoutControlItem layoutControlItem2;
private DevExpress.XtraLayout.LayoutControlItem layoutControlItem4;
private DevExpress.XtraLayout.LayoutControlItem layoutControlItem3;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem1;
private DevExpress.XtraLayout.EmptySpaceItem emptySpaceItem2;
}
}
And here's the code page (.cs file):
public partial class Welcome : BaseForm
{
public Welcome(int timeoutIntervalInSeconds, Controller controller) : base(timeoutIntervalInSeconds, controller)
{
InitializeComponent();
if (!_controller.IsDesignMode)
{
}
_controller.SetDesignMode(layoutCtrlMain);
}
private void Welcome_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
{
if (!_controller.IsDesignMode) return;
_controller.SaveLayout(layoutCtrlMain, Enums.LayoutType.Welcome);
}
private void lblAppointment_Click(object sender, EventArgs e)
{
_controller.OpenAppointments();
Close();
}
private void lblNoAppointment_Click(object sender, EventArgs e)
{
_controller.OpenAppointments();
Close();
}
private void Welcome_Load(object sender, EventArgs e)
{
_controller.LoadLayout(layoutCtrlMain, Enums.LayoutType.Welcome);
layoutCtrlMain.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
layoutCtrlMain.LookAndFeel.UseDefaultLookAndFeel = false;
layoutCtrlMain.OptionsView.ShareLookAndFeelWithChildren = false;
layoutCtrlMain.OptionsView.EnableTransparentBackColor = true;
layoutCtrlMain.Root.AppearanceGroup.BackColor = System.Drawing.Color.Transparent;
layoutCtrlMain.Root.AppearanceGroup.Options.UseBackColor = true;
var sharepath = _controller.GetSharePath(Enums.LayoutType.Welcome);
var backFile = Path.Combine(_controller.GetSharePath(LayoutType.Welcome), "background.png");
if (File.Exists(backFile)) layoutCtrlMain.BackgroundImage = Image.FromFile(backFile);
//throws them out of the main layoutgroup...
/*layoutControlItem4.RestoreFromCustomization();
layoutControlItem3.RestoreFromCustomization();*/
}
private void Welcome_Shown(object sender, EventArgs e)
{
LoadMenuDisplayed();
}
private void LoadMenuDisplayed()
{
//var sharepath = _controller.GetSharePath(LayoutType.Appointment);
//((System.ComponentModel.ISupportInitialize)(this.layoutCtrlMain)).BeginInit();
//flpMain.SuspendLayout();
//flpMain.Controls.Clear();
//layoutCtrlMain.SuspendLayout();
//var itemFont = new Font("Arial", 18F, FontStyle.Bold);
//var fontService = _controller.GetContainer().Resolve(typeof(ILayoutService), "") as ILayoutService;
//if (fontService != null)
//{
// var fontResponse = fontService.GetFontLayout(LayoutType.Appointment_Font_EmployeeNameButton);
// if (fontResponse.IsSuccess) itemFont = fontResponse.GetValue().Font;
//}
//var firstBtn = new MyHoverButton();
//int counter = 0;
//foreach (var item in response.Values)
//{
// if (counter >= 20) break; //we only show 20 employees max
// var btn = new MyHoverButton();
// btn.TextLocation = TextLocation.Bottom;
// btn.ItemText = item.FullName;
// btn.ItemValue = item.ID;
// btn.ItemFont = itemFont;
// if (item.ID > 0)
// {
// var medwImg = Path.Combine(_controller.GetSharePath(LayoutType.Appointment), "Medewerkers", "contact_" + item.ID + ".jpg");
// if (File.Exists(medwImg))
// {
// btn.NormalImage = Image.FromFile(medwImg);
// //btn.HoverImage = Image.FromFile(medwImg);
// }
// else
// {
// }
// }
// btn.BorderStyle = BorderStyle.None;
// //btn.Width = flpMain.Size.Width / 5;
// //btn.Height = flpMain.Size.Height / 4;
// btn.ItemClicked += Btn_Click;
// flpMain.Controls.Add(btn);
// counter++;
//}
//layoutCtrlMain.ResumeLayout();
//flpMain.ResumeLayout();
//((System.ComponentModel.ISupportInitialize)(this.layoutCtrlMain)).EndInit();
}
}
}
I'm not familiar with DevExpress so maybe I'm forgetting something somewhere?
I'm not familiar with DevExpress so maybe I'm forgetting something
somewhere?
I believe that you did not forget anything, because new labels already visible in the designer. There must be a very simple key to this riddle.
Comment out this line in the Welcome_Load method:
_controller.LoadLayout(layoutCtrlMain, Enums.LayoutType.Welcome);
If this helps, then newly added labels are not shown because the previously saved layout do not contain them.
To fix this problem and keep the _controller.LoadLayout code line uncommented, find the file where your _controller saves the layout and delete it.

Why does Visual Studio 2017 keep changing my public static statements to "public" after I change any aspect of any object on a form in C#

I've posted my code. Every single time I change anything on my this form in my Windows Forms App in C#, it changes my "public static" changes at the end to just "public" and then it adds "this." to every single reference to them. My code seems to run fine after I edit it again, but this is really annoying. I just wanted to make sure I'm not missing something or taking a poor approach to this.
The reason I am using public static statements is because I am modifying some Textboxes and a DataGridView object on my main form from other forms - i.e. looking up customer info from a database in one form and then entering the data on the main form to write up an order, and taking shopping cart information from a WebBrowser control on one form and putting it into a DataGridView object on another form. If this isn't how to accomplish populating forms from another form please tell me how and be as specific as possible as I am quite new to C#. Sorry if I've posted too much code - this is almost everything from my form1.cs file and I didn't know what (if anything) that I have here is causing the problem. The relevant "public static" statements are at the end.
(AFTER I WROTE THIS - I looked up another thread where someone said that forms are supposed to be a "specific instantiation" and that is why the developer environment keeps taking out the "static"). That being the case... HELP!! I do not understand how to change the values on one form from another form and I absolutely need to do so. - this code works, but I guess it's bad practice? - Completely lost and need very specific instructions to make this work)
Thanks in advance!!!
namespace WindowsFormsApp1
{
public partial class frmMain
{
private System.ComponentModel.IContainer components = null;
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.cmdSendEmail = new System.Windows.Forms.Button();
this.cmdExit = new System.Windows.Forms.Button();
this.cmdLookup = new System.Windows.Forms.Button();
this.txtTransactionNumber = new System.Windows.Forms.TextBox();
this.cmdEdit = new System.Windows.Forms.Button();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.cmdBrowse = new System.Windows.Forms.Button();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.cmdCustomer = new System.Windows.Forms.Button();
this.txtNotes = new System.Windows.Forms.TextBox();
txtZip = new System.Windows.Forms.TextBox();
txtCountry = new System.Windows.Forms.TextBox();
txtState = new System.Windows.Forms.TextBox();
txtCity = new System.Windows.Forms.TextBox();
txtAddress = new System.Windows.Forms.TextBox();
txtName = new System.Windows.Forms.TextBox();
txtPayPalEmail = new System.Windows.Forms.TextBox();
txtEmail = new System.Windows.Forms.TextBox();
txtPhone = new System.Windows.Forms.TextBox();
txtLast = new System.Windows.Forms.TextBox();
txtFirst = new System.Windows.Forms.TextBox();
grdOrderItems = new System.Windows.Forms.DataGridView();
this.Qty = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Description = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.UnitPrice = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.ExtPrice = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.SCode = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Options1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.Options2 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.groupBox3 = new System.Windows.Forms.GroupBox();
this.lblSubTotal = new System.Windows.Forms.Label();
this.lblTotal = new System.Windows.Forms.Label();
this.txtTax = new System.Windows.Forms.TextBox();
this.txtShipping = new System.Windows.Forms.TextBox();
this.txtShippingMethod = new System.Windows.Forms.TextBox();
this.cmdTaxLookup = new System.Windows.Forms.Button();
this.lstShipping = new System.Windows.Forms.ListBox();
this.cmdBrowser = new System.Windows.Forms.Button();
this.groupBox4 = new System.Windows.Forms.GroupBox();
this.cmdSendRequest = new System.Windows.Forms.Button();
this.cmdClear = new System.Windows.Forms.Button();
this.cmdSave = new System.Windows.Forms.Button();
this.cmdPrint = new System.Windows.Forms.Button();
this.cmdSavePrintEmail = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(grdOrderItems)).BeginInit();
this.groupBox3.SuspendLayout();
this.groupBox4.SuspendLayout();
this.SuspendLayout();
//
// cmdLookup
//
this.cmdLookup.Location = new System.Drawing.Point(88, 15);
this.cmdLookup.Name = "cmdLookup";
this.cmdLookup.Size = new System.Drawing.Size(197, 33);
this.cmdLookup.TabIndex = 3;
this.cmdLookup.Text = "Look up order to attach request to";
this.cmdLookup.UseVisualStyleBackColor = true;
//
// txtTransactionNumber
//
this.txtTransactionNumber.Location = new System.Drawing.Point(91, 57);
this.txtTransactionNumber.Name = "txtTransactionNumber";
this.txtTransactionNumber.Size = new System.Drawing.Size(158, 20);
this.txtTransactionNumber.TabIndex = 5;
//
// cmdEdit
//
this.cmdEdit.Location = new System.Drawing.Point(260, 57);
this.cmdEdit.Name = "cmdEdit";
this.cmdEdit.Size = new System.Drawing.Size(87, 30);
this.cmdEdit.TabIndex = 6;
this.cmdEdit.Text = "Edit";
this.cmdEdit.UseVisualStyleBackColor = true;
//
//
// groupBox1
//
this.groupBox1.Location = new System.Drawing.Point(438, 125);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(345, 56);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
//
// cmdBrowse
//
this.cmdBrowse.Location = new System.Drawing.Point(14, 139);
this.cmdBrowse.Name = "cmdBrowse";
this.cmdBrowse.Size = new System.Drawing.Size(220, 22);
this.cmdBrowse.TabIndex = 11;
this.cmdBrowse.Text = "Browse Unpaid Payment Requests";
this.cmdBrowse.UseVisualStyleBackColor = true;
//
// groupBox2
//
this.groupBox2.BackColor = System.Drawing.Color.WhiteSmoke;
this.groupBox2.Controls.Add(this.cmdCustomer);
this.groupBox2.Controls.Add(this.txtNotes);
this.groupBox2.Controls.Add(txtZip);
this.groupBox2.Controls.Add(txtCountry);
this.groupBox2.Controls.Add(txtState);
this.groupBox2.Controls.Add(txtCity);
this.groupBox2.Controls.Add(txtAddress);
this.groupBox2.Controls.Add(txtName);
this.groupBox2.Controls.Add(txtPayPalEmail);
this.groupBox2.Controls.Add(txtEmail);
this.groupBox2.Controls.Add(txtPhone);
this.groupBox2.Controls.Add(txtLast);
this.groupBox2.Controls.Add(txtFirst);
this.groupBox2.Location = new System.Drawing.Point(14, 168);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(749, 264);
this.groupBox2.TabIndex = 12;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Customer Information";
this.groupBox2.Enter += new System.EventHandler(this.groupBox2_Enter);
//
// label21
//
this.label21.AutoSize = true;
this.label21.Location = new System.Drawing.Point(186, 215);
this.label21.Name = "label21";
this.label21.Size = new System.Drawing.Size(25, 13);
this.label21.TabIndex = 24;
this.label21.Text = "Zip:";
//
// cmdCustomer
//
this.cmdCustomer.Location = new System.Drawing.Point(657, 136);
this.cmdCustomer.Name = "cmdCustomer";
this.cmdCustomer.Size = new System.Drawing.Size(86, 69);
this.cmdCustomer.TabIndex = 23;
this.cmdCustomer.Text = "Load Customer Info from Prior Order";
this.cmdCustomer.UseVisualStyleBackColor = true;
this.cmdCustomer.Click += new System.EventHandler(this.cmdCustomer_Click);
//
// txtNotes
//
this.txtNotes.AcceptsReturn = true;
this.txtNotes.Location = new System.Drawing.Point(404, 136);
this.txtNotes.Multiline = true;
this.txtNotes.Name = "txtNotes";
this.txtNotes.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this.txtNotes.Size = new System.Drawing.Size(247, 122);
this.txtNotes.TabIndex = 19;
//
// txtZip
//
txtZip.Location = new System.Drawing.Point(211, 212);
txtZip.Name = "txtZip";
txtZip.Size = new System.Drawing.Size(99, 20);
txtZip.TabIndex = 25;
txtZip.TextChanged += new System.EventHandler(this.txtZip_TextChanged);
//
// txtCountry
//
txtCountry.Location = new System.Drawing.Point(72, 237);
txtCountry.Name = "txtCountry";
txtCountry.Size = new System.Drawing.Size(238, 20);
txtCountry.TabIndex = 22;
txtCountry.TextChanged += new System.EventHandler(this.txtCountry_TextChanged);
//
// txtState
//
txtState.Location = new System.Drawing.Point(72, 210);
txtState.Name = "txtState";
txtState.Size = new System.Drawing.Size(108, 20);
txtState.TabIndex = 21;
txtState.TextChanged += new System.EventHandler(this.txtState_TextChanged);
//
// txtCity
//
txtCity.Location = new System.Drawing.Point(72, 185);
txtCity.Name = "txtCity";
txtCity.Size = new System.Drawing.Size(238, 20);
txtCity.TabIndex = 20;
txtCity.TextChanged += new System.EventHandler(this.txtCity_TextChanged);
//
// txtAddress
//
txtAddress.AcceptsReturn = true;
txtAddress.Location = new System.Drawing.Point(72, 92);
txtAddress.Multiline = true;
txtAddress.Name = "txtAddress";
txtAddress.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
txtAddress.Size = new System.Drawing.Size(236, 87);
txtAddress.TabIndex = 18;
txtAddress.TextChanged += new System.EventHandler(this.txtAddress_TextChanged);
//
// txtName
//
txtName.Location = new System.Drawing.Point(72, 68);
txtName.Name = "txtName";
txtName.Size = new System.Drawing.Size(238, 20);
txtName.TabIndex = 17;
txtName.TextChanged += new System.EventHandler(this.txtName_TextChanged);
//
// txtPayPalEmail
//
txtPayPalEmail.Location = new System.Drawing.Point(405, 98);
txtPayPalEmail.Name = "txtPayPalEmail";
txtPayPalEmail.Size = new System.Drawing.Size(203, 20);
txtPayPalEmail.TabIndex = 16;
//
// txtEmail
//
txtEmail.Location = new System.Drawing.Point(404, 65);
txtEmail.Name = "txtEmail";
txtEmail.Size = new System.Drawing.Size(204, 20);
txtEmail.TabIndex = 15;
//
// txtPhone
//
txtPhone.Location = new System.Drawing.Point(405, 39);
txtPhone.Name = "txtPhone";
txtPhone.Size = new System.Drawing.Size(203, 20);
txtPhone.TabIndex = 14;
txtPhone.TextChanged += new System.EventHandler(this.txtPhone_TextChanged);
//
// txtLast
//
txtLast.Location = new System.Drawing.Point(189, 39);
txtLast.Name = "txtLast";
txtLast.Size = new System.Drawing.Size(121, 20);
txtLast.TabIndex = 13;
txtLast.TextChanged += new System.EventHandler(this.txtLast_TextChanged);
//
// txtFirst
//
txtFirst.Location = new System.Drawing.Point(72, 39);
txtFirst.Name = "txtFirst";
txtFirst.Size = new System.Drawing.Size(84, 20);
txtFirst.TabIndex = 12;
txtFirst.TextChanged += new System.EventHandler(this.txtFirst_TextChanged);
// grdOrderItems
//
grdOrderItems.AutoSizeColumnsMode = System.Windows.Forms.DataGridViewAutoSizeColumnsMode.AllCells;
grdOrderItems.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
grdOrderItems.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.Qty,
this.Description,
this.UnitPrice,
this.ExtPrice,
this.SCode,
this.Options1,
this.Options2});
grdOrderItems.Location = new System.Drawing.Point(10, 19);
grdOrderItems.Name = "grdOrderItems";
grdOrderItems.Size = new System.Drawing.Size(732, 81);
grdOrderItems.TabIndex = 0;
grdOrderItems.CellValueChanged += new System.Windows.Forms.DataGridViewCellEventHandler(grdOrderItems_CellValueChanged);
grdOrderItems.RowsAdded += new System.Windows.Forms.DataGridViewRowsAddedEventHandler(this.grdOrderItems_RowsAdded);
//
// Qty
//
this.Qty.HeaderText = "Qty";
this.Qty.Name = "Qty";
this.Qty.Width = 48;
//
// Description
//
this.Description.HeaderText = "Description";
this.Description.Name = "Description";
this.Description.Width = 85;
//
// UnitPrice
//
this.UnitPrice.HeaderText = "Unit Price";
this.UnitPrice.Name = "UnitPrice";
this.UnitPrice.Width = 78;
//
// ExtPrice
//
this.ExtPrice.HeaderText = "Ext. Price3";
this.ExtPrice.Name = "ExtPrice";
this.ExtPrice.ReadOnly = true;
this.ExtPrice.Width = 83;
//
// SCode
//
this.SCode.HeaderText = "S-Code";
this.SCode.Name = "SCode";
this.SCode.Width = 67;
//
// Options1
//
this.Options1.HeaderText = "Options1";
this.Options1.Name = "Options1";
this.Options1.Width = 74;
//
// Options2
//
this.Options2.HeaderText = "Options2";
this.Options2.Name = "Options2";
this.Options2.Width = 74;
//
// groupBox3
//
this.groupBox3.BackColor = System.Drawing.Color.WhiteSmoke;
this.groupBox3.Controls.Add(this.checkBox1);
this.groupBox3.Controls.Add(this.cmdApplyTaxRate);
this.groupBox3.Controls.Add(this.txtTaxRate);
this.groupBox3.Controls.Add(this.lblSubTotal);
this.groupBox3.Controls.Add(this.lblTotal);
this.groupBox3.Controls.Add(this.txtTax);
this.groupBox3.Controls.Add(this.txtShipping);
this.groupBox3.Controls.Add(this.txtShippingMethod);
this.groupBox3.Controls.Add(this.cmdTaxLookup);
this.groupBox3.Controls.Add(this.lstShipping);
this.groupBox3.Controls.Add(this.cmdBrowser);
this.groupBox3.Controls.Add(this.groupBox4);
this.groupBox3.Controls.Add(grdOrderItems);
this.groupBox3.Location = new System.Drawing.Point(14, 443);
this.groupBox3.Name = "groupBox3";
this.groupBox3.Size = new System.Drawing.Size(749, 242);
this.groupBox3.TabIndex = 13;
this.groupBox3.TabStop = false;
this.groupBox3.Text = "Order Information";
//
// checkBox1
//
this.checkBox1.AutoSize = true;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Location = new System.Drawing.Point(253, 224);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(257, 17);
this.checkBox1.TabIndex = 21;
this.checkBox1.Text = "Automatically Apply Tax Rate with order changes";
this.checkBox1.UseVisualStyleBackColor = true;
//
// cmdApplyTaxRate
//
this.cmdApplyTaxRate.Location = new System.Drawing.Point(313, 194);
this.cmdApplyTaxRate.Name = "cmdApplyTaxRate";
this.cmdApplyTaxRate.Size = new System.Drawing.Size(126, 24);
this.cmdApplyTaxRate.TabIndex = 20;
this.cmdApplyTaxRate.Text = "Apply Tax Rate";
this.cmdApplyTaxRate.UseVisualStyleBackColor = true;
//
// txtTaxRate
//
this.txtTaxRate.Location = new System.Drawing.Point(186, 198);
this.txtTaxRate.Name = "txtTaxRate";
this.txtTaxRate.Size = new System.Drawing.Size(101, 20);
this.txtTaxRate.TabIndex = 19;
//
// lblSubTotal
//
this.lblSubTotal.AutoSize = true;
this.lblSubTotal.Location = new System.Drawing.Point(571, 128);
this.lblSubTotal.Name = "lblSubTotal";
this.lblSubTotal.Size = new System.Drawing.Size(13, 13);
this.lblSubTotal.TabIndex = 18;
this.lblSubTotal.Text = "$";
//
// lblTotal
//
this.lblTotal.AutoSize = true;
this.lblTotal.Location = new System.Drawing.Point(571, 210);
this.lblTotal.Name = "lblTotal";
this.lblTotal.Size = new System.Drawing.Size(13, 13);
this.lblTotal.TabIndex = 17;
this.lblTotal.Text = "$";
//
// txtTax
//
this.txtTax.Location = new System.Drawing.Point(561, 177);
this.txtTax.Name = "txtTax";
this.txtTax.Size = new System.Drawing.Size(182, 20);
this.txtTax.TabIndex = 16;
//
// txtShipping
//
this.txtShipping.Location = new System.Drawing.Point(561, 149);
this.txtShipping.Name = "txtShipping";
this.txtShipping.Size = new System.Drawing.Size(182, 20);
this.txtShipping.TabIndex = 15;
//
// txtShippingMethod
//
this.txtShippingMethod.Location = new System.Drawing.Point(559, 105);
this.txtShippingMethod.Name = "txtShippingMethod";
this.txtShippingMethod.Size = new System.Drawing.Size(182, 20);
this.txtShippingMethod.TabIndex = 14;
//
// cmdTaxLookup
//
this.cmdTaxLookup.Location = new System.Drawing.Point(138, 218);
this.cmdTaxLookup.Name = "cmdTaxLookup";
this.cmdTaxLookup.Size = new System.Drawing.Size(99, 21);
this.cmdTaxLookup.TabIndex = 13;
this.cmdTaxLookup.Text = "Lookup Tax Rate";
this.cmdTaxLookup.UseVisualStyleBackColor = true;
//
// lstShipping
//
this.lstShipping.FormattingEnabled = true;
this.lstShipping.Location = new System.Drawing.Point(304, 106);
this.lstShipping.Name = "lstShipping";
this.lstShipping.Size = new System.Drawing.Size(150, 69);
this.lstShipping.TabIndex = 5;
//
// cmdBrowser
//
this.cmdBrowser.Location = new System.Drawing.Point(16, 106);
this.cmdBrowser.Name = "cmdBrowser";
this.cmdBrowser.Size = new System.Drawing.Size(164, 21);
this.cmdBrowser.TabIndex = 2;
this.cmdBrowser.Text = "Launch Browser Form";
this.cmdBrowser.UseVisualStyleBackColor = true;
this.cmdBrowser.Click += new System.EventHandler(this.cmdBrowser_Click);
//
// groupBox4
//
this.groupBox4.Controls.Add(this.cmdSendRequest);
this.groupBox4.Location = new System.Drawing.Point(18, 133);
this.groupBox4.Name = "groupBox4";
this.groupBox4.Size = new System.Drawing.Size(162, 57);
this.groupBox4.TabIndex = 1;
this.groupBox4.TabStop = false;
this.groupBox4.Text = "PayPal";
//
// cmdSendRequest
//
this.cmdSendRequest.Location = new System.Drawing.Point(7, 19);
this.cmdSendRequest.Name = "cmdSendRequest";
this.cmdSendRequest.Size = new System.Drawing.Size(131, 32);
this.cmdSendRequest.TabIndex = 2;
this.cmdSendRequest.Text = "Send Payment Request";
this.cmdSendRequest.UseVisualStyleBackColor = true;
this.cmdSendRequest.Click += new System.EventHandler(this.cmdSendRequest_Click_1);
//
// cmdClear
//
this.cmdClear.Location = new System.Drawing.Point(16, 696);
this.cmdClear.Name = "cmdClear";
this.cmdClear.Size = new System.Drawing.Size(67, 25);
this.cmdClear.TabIndex = 14;
this.cmdClear.Text = "Clear";
this.cmdClear.UseVisualStyleBackColor = true;
//
// cmdSave
//
this.cmdSave.Location = new System.Drawing.Point(91, 697);
this.cmdSave.Name = "cmdSave";
this.cmdSave.Size = new System.Drawing.Size(67, 25);
this.cmdSave.TabIndex = 15;
this.cmdSave.Text = "Save";
this.cmdSave.UseVisualStyleBackColor = true;
//
// cmdPrint
//
this.cmdPrint.Location = new System.Drawing.Point(164, 697);
this.cmdPrint.Name = "cmdPrint";
this.cmdPrint.Size = new System.Drawing.Size(67, 25);
this.cmdPrint.TabIndex = 16;
this.cmdPrint.Text = "Print";
this.cmdPrint.UseVisualStyleBackColor = true;
//
// cmdSavePrintEmail
//
this.cmdSavePrintEmail.Location = new System.Drawing.Point(393, 694);
this.cmdSavePrintEmail.Name = "cmdSavePrintEmail";
this.cmdSavePrintEmail.Size = new System.Drawing.Size(131, 31);
this.cmdSavePrintEmail.TabIndex = 17;
this.cmdSavePrintEmail.Text = "Save, Print, and Email";
this.cmdSavePrintEmail.UseVisualStyleBackColor = true;
//
// frmMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Aqua;
this.ClientSize = new System.Drawing.Size(790, 725);
this.Controls.Add(this.cmdSavePrintEmail);
this.Controls.Add(this.cmdPrint);
this.Controls.Add(this.cmdSave);
this.Controls.Add(this.cmdClear);
this.Controls.Add(this.groupBox3);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.cmdBrowse);
this.Controls.Add(this.groupBox1);
this.Controls.Add(this.optRequestNew);
this.Controls.Add(this.optRequestShipping);
this.Controls.Add(this.optRequestAdd);
this.Controls.Add(this.cmdEdit);
this.Controls.Add(this.txtTransactionNumber);
this.Controls.Add(this.cmdLookup);
this.Controls.Add(this.cmdExit);
this.Controls.Add(this.cmdSendEmail);
this.Name = "frmMain";
this.Text = "PayPal Payment Request Program version C1";
this.Load += new System.EventHandler(this.Form1_Load);
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
((System.ComponentModel.ISupportInitialize)(grdOrderItems)).EndInit();
this.groupBox3.ResumeLayout(false);
this.groupBox3.PerformLayout();
this.groupBox4.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button cmdSendEmail;
private System.Windows.Forms.Button cmdExit;
private System.Windows.Forms.Button cmdLookup;
private System.Windows.Forms.TextBox txtTransactionNumber;
private System.Windows.Forms.Button cmdEdit;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Button cmdBrowse;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Button cmdCustomer;
private System.Windows.Forms.TextBox txtNotes;
private System.Windows.Forms.GroupBox groupBox3;
private System.Windows.Forms.GroupBox groupBox4;
private System.Windows.Forms.Button cmdSendRequest;
private System.Windows.Forms.Button cmdBrowser;
private System.Windows.Forms.CheckBox checkBox1;
private System.Windows.Forms.Button cmdApplyTaxRate;
private System.Windows.Forms.TextBox txtTaxRate;
private System.Windows.Forms.TextBox txtTax;
private System.Windows.Forms.TextBox txtShipping;
private System.Windows.Forms.TextBox txtShippingMethod;
private System.Windows.Forms.Button cmdTaxLookup;
private System.Windows.Forms.ListBox lstShipping;
private System.Windows.Forms.Button cmdClear;
private System.Windows.Forms.Button cmdSave;
private System.Windows.Forms.Button cmdPrint;
private System.Windows.Forms.Button cmdSavePrintEmail;
private System.Windows.Forms.DataGridViewTextBoxColumn Qty;
private System.Windows.Forms.DataGridViewTextBoxColumn Description;
private System.Windows.Forms.DataGridViewTextBoxColumn UnitPrice;
private System.Windows.Forms.DataGridViewTextBoxColumn ExtPrice;
private System.Windows.Forms.DataGridViewTextBoxColumn SCode;
private System.Windows.Forms.DataGridViewTextBoxColumn Options1;
private System.Windows.Forms.DataGridViewTextBoxColumn Options2;
public static System.Windows.Forms.TextBox txtEmail;
public static System.Windows.Forms.TextBox txtPhone;
public static System.Windows.Forms.TextBox txtLast;
public static System.Windows.Forms.TextBox txtCountry;
public static System.Windows.Forms.TextBox txtState;
public static System.Windows.Forms.TextBox txtAddress;
public static System.Windows.Forms.TextBox txtPayPalEmail;
public static System.Windows.Forms.DataGridView grdOrderItems;
public static System.Windows.Forms.TextBox txtZip;
public static System.Windows.Forms.TextBox txtCity;
public static System.Windows.Forms.TextBox txtName;
public static System.Windows.Forms.TextBox txtFirst;
}
}
If you look into the .designer.cs file carefully you will notice this little comment
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
Basically that part of the code are auto generated by the designer and should never be modified manually.
And yes this is considered bad practice. Firstly the control should be self contained inside the form instead of being a static one. Static just means there will only be one instance across all the forms of the same class.
Direct modification of UI from one form to another is considered anti-pattern as well. You should be making a Data model and have the UI reads the updated data when they are changed. I know this might be a bit challenging for beginners as well for winform.
So if you want to take the shortcut you can expose a public property on the other form and by modifying the property your other form will update the appropriate control.
Edit:
In the other form's code behind (.cs file)
you can do like
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string SelectedUser { get; private set; }
}
and then in your main form you would listen to the close event
Form1 other = new Form1();
other.FormClosed += Other_FormClosed;
other.Show();
...
private void Other_FormClosed(object sender, FormClosedEventArgs e)
{
Form1 other = (Form1)sender;
string selectedUser = other.SelectedUser;
//do something
}

C# login system

Currently I'm experimenting how I can make a login system, with different userinput. (Every user has his own information).I've two database tables, one is called "user" (here is id primary key, and username and password are in it) and the second table is the "userInformation" (here is id the foreign key and there is information like name,adres etc.) I've made it like this:
public partial class standardUserInterface : Form
{
int userIdNumber;
string[] userinfo;
public standardUserInterface(int userIdNumber)
{
InitializeComponent();
userinfo = new string[3];
this.userIdNumber = userIdNumber;
string selectUserData = "select voornaam,achternaam,woonplaats from dbo.usersinfo inner join dbo.loginuser on usersinfo.id=#loginUserId";
SqlConnection conn = sqlConn.openSqlConnection();
conn.Open();
SqlCommand comm = new SqlCommand(selectUserData, conn);
comm.Parameters.AddWithValue("#loginUserId", userIdNumber);
SqlDataReader dr = comm.ExecuteReader();
while (dr.Read())
{
for (int i = 0; i < 3; i++)
{
userinfo[i] = dr.GetString(i);
}
}
lblVoornaam.Text = userinfo[0];
lblAchternaam.Text = userinfo[1];
lblAdres.Text = userinfo[2];
}
}
What do you think about this? Is there a better way, or is this done right?
EDIT
This is the login before it's receiving the userdata
private string password;
private string username;
private int userIdNumber;
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
username = txtUsername.Text;
password = txtPassword.Text;
userLogin user = new userLogin(username, password);
//wachtwoord & gebruikersnaam controleren
if (username != "" & password != "")
{
if (user.checkPassword())
{
this.Hide();
userIdNumber = user.checkID();
standardUserInterface openUserInterface = new standardUserInterface(userIdNumber);
openUserInterface.ShowDialog();
}
else
{
MessageBox.Show("wachtwoord is onjuist");
}
}
else
{
MessageBox.Show("vul alle velden in");
}
}
Don't we need a authentication system for log in, wherein we can validate the used id and password? only if the validation is successful we will go for fetching the user details
one suggestion, it will be better if we can create a stored procedure for getting the user data instead of the inline query.
I've done it now (the stored procedure)`
SqlCommand comm = new SqlCommand("searchForUserInformation", conn);
comm.CommandType = CommandType.StoredProcedure;
comm.Parameters.AddWithValue("#loginUserId", userIdNumber);`
Registration
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace Csharp_Login_And_Register
{
public partial class RegisterForm : Form
{
public RegisterForm()
{
InitializeComponent();
}
private void RegisterForm_Load(object sender, EventArgs e)
{
// remove the focus from the textboxes
this.ActiveControl = label1;
}
private void textBoxFirstname_Enter(object sender, EventArgs e)
{
String fname = textBoxFirstname.Text;
if(fname.ToLower().Trim().Equals("first name"))
{
textBoxFirstname.Text = "";
textBoxFirstname.ForeColor = Color.Black;
}
}
private void textBoxFirstname_Leave(object sender, EventArgs e)
{
String fname = textBoxFirstname.Text;
if (fname.ToLower().Trim().Equals("first name") || fname.Trim().Equals(""))
{
textBoxFirstname.Text = "first name";
textBoxFirstname.ForeColor = Color.Gray;
}
}
private void textBoxLastname_Enter(object sender, EventArgs e)
{
String lname = textBoxLastname.Text;
if (lname.ToLower().Trim().Equals("last name"))
{
textBoxLastname.Text = "";
textBoxLastname.ForeColor = Color.Black;
}
}
private void textBoxLastname_Leave(object sender, EventArgs e)
{
String lname = textBoxLastname.Text;
if (lname.ToLower().Trim().Equals("last name") || lname.Trim().Equals(""))
{
textBoxLastname.Text = "last name";
textBoxLastname.ForeColor = Color.Gray;
}
}
private void textBoxEmail_Enter(object sender, EventArgs e)
{
String email = textBoxEmail.Text;
if (email.ToLower().Trim().Equals("email address"))
{
textBoxEmail.Text = "";
textBoxEmail.ForeColor = Color.Black;
}
}
private void textBoxEmail_Leave(object sender, EventArgs e)
{
String email = textBoxEmail.Text;
if (email.ToLower().Trim().Equals("email address") || email.Trim().Equals(""))
{
textBoxEmail.Text = "email address";
textBoxEmail.ForeColor = Color.Gray;
}
}
private void textBoxUsername_Enter(object sender, EventArgs e)
{
String username = textBoxUsername.Text;
if (username.ToLower().Trim().Equals("username"))
{
textBoxUsername.Text = "";
textBoxUsername.ForeColor = Color.Black;
}
}
private void textBoxUsername_Leave(object sender, EventArgs e)
{
String username = textBoxUsername.Text;
if (username.ToLower().Trim().Equals("username") || username.Trim().Equals(""))
{
textBoxUsername.Text = "username";
textBoxUsername.ForeColor = Color.Gray;
}
}
private void textBoxPassword_Enter(object sender, EventArgs e)
{
String password = textBoxPassword.Text;
if (password.ToLower().Trim().Equals("password"))
{
textBoxPassword.Text = "";
textBoxPassword.UseSystemPasswordChar = true;
textBoxPassword.ForeColor = Color.Black;
}
}
private void textBoxPassword_Leave(object sender, EventArgs e)
{
String password = textBoxPassword.Text;
if (password.ToLower().Trim().Equals("password") || password.Trim().Equals(""))
{
textBoxPassword.Text = "password";
textBoxPassword.UseSystemPasswordChar = false;
textBoxPassword.ForeColor = Color.Gray;
}
}
private void textBoxPasswordConfirm_Enter(object sender, EventArgs e)
{
String cpassword = textBoxPasswordConfirm.Text;
if (cpassword.ToLower().Trim().Equals("confirm password"))
{
textBoxPasswordConfirm.Text = "";
textBoxPasswordConfirm.UseSystemPasswordChar = true;
textBoxPasswordConfirm.ForeColor = Color.Black;
}
}
private void textBoxPasswordConfirm_Leave(object sender, EventArgs e)
{
String cpassword = textBoxPasswordConfirm.Text;
if (cpassword.ToLower().Trim().Equals("confirm password") ||
cpassword.ToLower().Trim().Equals("password") ||
cpassword.Trim().Equals(""))
{
textBoxPasswordConfirm.Text = "confirm password";
textBoxPasswordConfirm.UseSystemPasswordChar = false;
textBoxPasswordConfirm.ForeColor = Color.Gray;
}
}
private void labelClose_Click(object sender, EventArgs e)
{
//this.Close();
Application.Exit();
}
private void labelClose_MouseEnter(object sender, EventArgs e)
{
labelClose.ForeColor = Color.Black;
}
private void labelClose_MouseLeave(object sender, EventArgs e)
{
labelClose.ForeColor = Color.White;
}
private void buttonCreateAccount_Click(object sender, EventArgs e)
{
// add a new user
DB db = new DB();
MySqlCommand command = new MySqlCommand("INSERT INTO `users`(`firstname`, `lastname`, `emailaddress`, `username`, `password`) VALUES (#fn, #ln, #email, #usn, #pass)", db.getConnection());
command.Parameters.Add("#fn", MySqlDbType.VarChar).Value = textBoxFirstname.Text;
command.Parameters.Add("#ln", MySqlDbType.VarChar).Value = textBoxLastname.Text;
command.Parameters.Add("#email", MySqlDbType.VarChar).Value = textBoxEmail.Text;
command.Parameters.Add("#usn", MySqlDbType.VarChar).Value = textBoxUsername.Text;
command.Parameters.Add("#pass", MySqlDbType.VarChar).Value = textBoxPassword.Text;
// open the connection
db.openConnection();
// check if the textboxes contains the default values
if (!checkTextBoxesValues())
{
// check if the password equal the confirm password
if(textBoxPassword.Text.Equals(textBoxPasswordConfirm.Text))
{
// check if this username already exists
if (checkUsername())
{
MessageBox.Show("This Username Already Exists, Select A Different One","Duplicate Username",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
else
{
// execute the query
if (command.ExecuteNonQuery() == 1)
{
MessageBox.Show("Your Account Has Been Created","Account Created",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
else
{
MessageBox.Show("ERROR");
}
}
}
else
{
MessageBox.Show("Wrong Confirmation Password","Password Error",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
}
else
{
MessageBox.Show("Enter Your Informations First","Empty Data",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
}
// close the connection
db.closeConnection();
}
// check if the username already exists
public Boolean checkUsername()
{
DB db = new DB();
String username = textBoxUsername.Text;
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand("SELECT * FROM `users` WHERE `username` = #usn", db.getConnection());
command.Parameters.Add("#usn", MySqlDbType.VarChar).Value = username;
adapter.SelectCommand = command;
adapter.Fill(table);
// check if this username already exists in the database
if (table.Rows.Count > 0)
{
return true;
}
else
{
return false;
}
}
// check if the textboxes contains the default values
public Boolean checkTextBoxesValues()
{
String fname = textBoxFirstname.Text;
String lname = textBoxLastname.Text;
String email = textBoxEmail.Text;
String uname = textBoxUsername.Text;
String pass = textBoxPassword.Text;
if(fname.Equals("first name") || lname.Equals("last name") ||
email.Equals("email address") || uname.Equals("username")
|| pass.Equals("password"))
{
return true;
}
else
{
return false;
}
}
private void labelGoToLogin_MouseEnter(object sender, EventArgs e)
{
labelGoToLogin.ForeColor = Color.Yellow;
}
private void labelGoToLogin_MouseLeave(object sender, EventArgs e)
{
labelGoToLogin.ForeColor = Color.White;
}
private void labelGoToLogin_Click(object sender, EventArgs e)
{
this.Hide();
LoginForm loginform = new LoginForm();
loginform.Show();
}
}
}
namespace Csharp_Login_And_Register
{
partial class RegisterForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.labelGoToLogin = new System.Windows.Forms.Label();
this.textBoxPasswordConfirm = new System.Windows.Forms.TextBox();
this.textBoxUsername = new System.Windows.Forms.TextBox();
this.textBoxEmail = new System.Windows.Forms.TextBox();
this.textBoxLastname = new System.Windows.Forms.TextBox();
this.buttonCreateAccount = new System.Windows.Forms.Button();
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.textBoxFirstname = new System.Windows.Forms.TextBox();
this.panel2 = new System.Windows.Forms.Panel();
this.labelClose = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(24)))), ((int)(((byte)(34)))));
this.panel1.Controls.Add(this.labelGoToLogin);
this.panel1.Controls.Add(this.textBoxPasswordConfirm);
this.panel1.Controls.Add(this.textBoxUsername);
this.panel1.Controls.Add(this.textBoxEmail);
this.panel1.Controls.Add(this.textBoxLastname);
this.panel1.Controls.Add(this.buttonCreateAccount);
this.panel1.Controls.Add(this.textBoxPassword);
this.panel1.Controls.Add(this.textBoxFirstname);
this.panel1.Controls.Add(this.panel2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(635, 624);
this.panel1.TabIndex = 1;
//
// labelGoToLogin
//
this.labelGoToLogin.AutoSize = true;
this.labelGoToLogin.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelGoToLogin.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelGoToLogin.ForeColor = System.Drawing.Color.White;
this.labelGoToLogin.Location = new System.Drawing.Point(238, 569);
this.labelGoToLogin.Name = "labelGoToLogin";
this.labelGoToLogin.Size = new System.Drawing.Size(164, 13);
this.labelGoToLogin.TabIndex = 201;
this.labelGoToLogin.Text = "Already Have an Account? Login";
this.labelGoToLogin.Click += new System.EventHandler(this.labelGoToLogin_Click);
this.labelGoToLogin.MouseEnter += new System.EventHandler(this.labelGoToLogin_MouseEnter);
this.labelGoToLogin.MouseLeave += new System.EventHandler(this.labelGoToLogin_MouseLeave);
//
// textBoxPasswordConfirm
//
this.textBoxPasswordConfirm.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxPasswordConfirm.ForeColor = System.Drawing.Color.Gray;
this.textBoxPasswordConfirm.Location = new System.Drawing.Point(52, 388);
this.textBoxPasswordConfirm.Name = "textBoxPasswordConfirm";
this.textBoxPasswordConfirm.Size = new System.Drawing.Size(537, 40);
this.textBoxPasswordConfirm.TabIndex = 9;
this.textBoxPasswordConfirm.Text = "confirm password";
this.textBoxPasswordConfirm.Enter += new System.EventHandler(this.textBoxPasswordConfirm_Enter);
this.textBoxPasswordConfirm.Leave += new System.EventHandler(this.textBoxPasswordConfirm_Leave);
//
// textBoxUsername
//
this.textBoxUsername.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxUsername.ForeColor = System.Drawing.Color.Gray;
this.textBoxUsername.Location = new System.Drawing.Point(52, 264);
this.textBoxUsername.Multiline = true;
this.textBoxUsername.Name = "textBoxUsername";
this.textBoxUsername.Size = new System.Drawing.Size(537, 40);
this.textBoxUsername.TabIndex = 8;
this.textBoxUsername.Text = "username";
this.textBoxUsername.Enter += new System.EventHandler(this.textBoxUsername_Enter);
this.textBoxUsername.Leave += new System.EventHandler(this.textBoxUsername_Leave);
//
// textBoxEmail
//
this.textBoxEmail.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxEmail.ForeColor = System.Drawing.Color.Gray;
this.textBoxEmail.Location = new System.Drawing.Point(52, 204);
this.textBoxEmail.Multiline = true;
this.textBoxEmail.Name = "textBoxEmail";
this.textBoxEmail.Size = new System.Drawing.Size(537, 40);
this.textBoxEmail.TabIndex = 7;
this.textBoxEmail.Text = "email address";
this.textBoxEmail.Enter += new System.EventHandler(this.textBoxEmail_Enter);
this.textBoxEmail.Leave += new System.EventHandler(this.textBoxEmail_Leave);
//
// textBoxLastname
//
this.textBoxLastname.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxLastname.ForeColor = System.Drawing.Color.Gray;
this.textBoxLastname.Location = new System.Drawing.Point(329, 145);
this.textBoxLastname.Multiline = true;
this.textBoxLastname.Name = "textBoxLastname";
this.textBoxLastname.Size = new System.Drawing.Size(260, 40);
this.textBoxLastname.TabIndex = 6;
this.textBoxLastname.Text = "last name";
this.textBoxLastname.Enter += new System.EventHandler(this.textBoxLastname_Enter);
this.textBoxLastname.Leave += new System.EventHandler(this.textBoxLastname_Leave);
//
// buttonCreateAccount
//
this.buttonCreateAccount.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(217)))), ((int)(((byte)(66)))), ((int)(((byte)(85)))));
this.buttonCreateAccount.Cursor = System.Windows.Forms.Cursors.Hand;
this.buttonCreateAccount.FlatAppearance.BorderSize = 0;
this.buttonCreateAccount.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonCreateAccount.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.buttonCreateAccount.ForeColor = System.Drawing.Color.White;
this.buttonCreateAccount.Location = new System.Drawing.Point(52, 473);
this.buttonCreateAccount.Name = "buttonCreateAccount";
this.buttonCreateAccount.Size = new System.Drawing.Size(537, 60);
this.buttonCreateAccount.TabIndex = 5;
this.buttonCreateAccount.Text = "CREATE ACCOUNT";
this.buttonCreateAccount.UseVisualStyleBackColor = false;
this.buttonCreateAccount.Click += new System.EventHandler(this.buttonCreateAccount_Click);
//
// textBoxPassword
//
this.textBoxPassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxPassword.ForeColor = System.Drawing.Color.Gray;
this.textBoxPassword.Location = new System.Drawing.Point(52, 325);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.Size = new System.Drawing.Size(537, 40);
this.textBoxPassword.TabIndex = 4;
this.textBoxPassword.Text = "password";
this.textBoxPassword.Enter += new System.EventHandler(this.textBoxPassword_Enter);
this.textBoxPassword.Leave += new System.EventHandler(this.textBoxPassword_Leave);
//
// textBoxFirstname
//
this.textBoxFirstname.Font = new System.Drawing.Font("Microsoft Sans Serif", 21.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxFirstname.ForeColor = System.Drawing.Color.Gray;
this.textBoxFirstname.Location = new System.Drawing.Point(52, 145);
this.textBoxFirstname.Multiline = true;
this.textBoxFirstname.Name = "textBoxFirstname";
this.textBoxFirstname.Size = new System.Drawing.Size(260, 40);
this.textBoxFirstname.TabIndex = 200;
this.textBoxFirstname.Text = "first name";
this.textBoxFirstname.Enter += new System.EventHandler(this.textBoxFirstname_Enter);
this.textBoxFirstname.Leave += new System.EventHandler(this.textBoxFirstname_Leave);
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(183)))), ((int)(((byte)(19)))));
this.panel2.Controls.Add(this.labelClose);
this.panel2.Controls.Add(this.label1);
this.panel2.Location = new System.Drawing.Point(-6, 0);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(641, 93);
this.panel2.TabIndex = 0;
//
// labelClose
//
this.labelClose.AutoSize = true;
this.labelClose.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelClose.Font = new System.Drawing.Font("Arial", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelClose.ForeColor = System.Drawing.Color.White;
this.labelClose.Location = new System.Drawing.Point(615, 3);
this.labelClose.Name = "labelClose";
this.labelClose.Size = new System.Drawing.Size(23, 22);
this.labelClose.TabIndex = 1;
this.labelClose.Text = "X";
this.labelClose.Click += new System.EventHandler(this.labelClose_Click);
this.labelClose.MouseEnter += new System.EventHandler(this.labelClose_MouseEnter);
this.labelClose.MouseLeave += new System.EventHandler(this.labelClose_MouseLeave);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Arial", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(641, 93);
this.label1.TabIndex = 0;
this.label1.Text = "CREATE YOUR ACCOUNT";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// RegisterForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(635, 624);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "RegisterForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "RegisterForm";
this.Load += new System.EventHandler(this.RegisterForm_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button buttonCreateAccount;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.TextBox textBoxFirstname;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label labelClose;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxPasswordConfirm;
private System.Windows.Forms.TextBox textBoxUsername;
private System.Windows.Forms.TextBox textBoxEmail;
private System.Windows.Forms.TextBox textBoxLastname;
private System.Windows.Forms.Label labelGoToLogin;
}
}
using MySql.Data.MySqlClient;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_Login_And_Register
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
this.textBoxPassword.AutoSize = false;
this.textBoxPassword.Size = new Size(this.textBoxPassword.Size.Width, 50);
}
private void labelClose_MouseEnter(object sender, EventArgs e)
{
labelClose.ForeColor = Color.Black;
}
private void labelClose_MouseLeave(object sender, EventArgs e)
{
labelClose.ForeColor = Color.White;
}
private void labelClose_Click(object sender, EventArgs e)
{
//this.Close();
Application.Exit();
}
private void buttonLogin_Click(object sender, EventArgs e)
{
DB db = new DB();
String username = textBoxUsername.Text;
String password = textBoxPassword.Text;
DataTable table = new DataTable();
MySqlDataAdapter adapter = new MySqlDataAdapter();
MySqlCommand command = new MySqlCommand("SELECT * FROM `users` WHERE `username` = #usn and `password` = #pass", db.getConnection());
command.Parameters.Add("#usn", MySqlDbType.VarChar).Value = username;
command.Parameters.Add("#pass", MySqlDbType.VarChar).Value = password;
adapter.SelectCommand = command;
adapter.Fill(table);
// check if the user exists or not
if (table.Rows.Count > 0)
{
this.Hide();
MainForm mainform = new MainForm();
mainform.Show();
}
else
{
if(username.Trim().Equals(""))
{
MessageBox.Show("Enter Your Username To Login","Empty Username",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
else if (password.Trim().Equals(""))
{
MessageBox.Show("Enter Your Password To Login", "Empty Password", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
MessageBox.Show("Wrong Username Or Password", "Wrong Data", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
private void labelGoToSignUp_Click(object sender, EventArgs e)
{
this.Hide();
RegisterForm registerform = new RegisterForm();
registerform.Show();
}
private void labelGoToSignUp_MouseEnter(object sender, EventArgs e)
{
labelGoToSignUp.ForeColor = Color.Yellow;
}
private void labelGoToSignUp_MouseLeave(object sender, EventArgs e)
{
labelGoToSignUp.ForeColor = Color.White;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MySql.Data.MySqlClient;
namespace Csharp_Login_And_Register
{
/*
we need to download the mysql connector
* add the connector to our project
* ( watch the video to see how )
* create a connection now with mysql
* open xampp and start mysql & apache
* go to phpmyadmin and create the users database
*/
class DB
{
private MySqlConnection connection = new MySqlConnection("server=localhost;port=3306;username=root;password=;database=csharp_users_db");
// create a function to open the connection
public void openConnection()
{
if(connection.State == System.Data.ConnectionState.Closed)
{
connection.Open();
}
}
// create a function to close the connection
public void closeConnection()
{
if (connection.State == System.Data.ConnectionState.Open)
{
connection.Close();
}
}
// create a function to return the connection
public MySqlConnection getConnection()
{
return connection;
}
}
}
namespace Csharp_Login_And_Register
{
partial class LoginForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.labelGoToSignUp = new System.Windows.Forms.Label();
this.buttonLogin = new System.Windows.Forms.Button();
this.textBoxPassword = new System.Windows.Forms.TextBox();
this.pictureBox2 = new System.Windows.Forms.PictureBox();
this.textBoxUsername = new System.Windows.Forms.TextBox();
this.pictureBox1 = new System.Windows.Forms.PictureBox();
this.panel2 = new System.Windows.Forms.Panel();
this.labelClose = new System.Windows.Forms.Label();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).BeginInit();
this.panel2.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(17)))), ((int)(((byte)(24)))), ((int)(((byte)(34)))));
this.panel1.Controls.Add(this.labelGoToSignUp);
this.panel1.Controls.Add(this.buttonLogin);
this.panel1.Controls.Add(this.textBoxPassword);
this.panel1.Controls.Add(this.pictureBox2);
this.panel1.Controls.Add(this.textBoxUsername);
this.panel1.Controls.Add(this.pictureBox1);
this.panel1.Controls.Add(this.panel2);
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(632, 523);
this.panel1.TabIndex = 0;
//
// labelGoToSignUp
//
this.labelGoToSignUp.AutoSize = true;
this.labelGoToSignUp.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelGoToSignUp.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Underline, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelGoToSignUp.ForeColor = System.Drawing.Color.White;
this.labelGoToSignUp.Location = new System.Drawing.Point(195, 479);
this.labelGoToSignUp.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labelGoToSignUp.Name = "labelGoToSignUp";
this.labelGoToSignUp.Size = new System.Drawing.Size(211, 17);
this.labelGoToSignUp.TabIndex = 6;
this.labelGoToSignUp.Text = "Don\'t Have an Account? SignUp";
this.labelGoToSignUp.Click += new System.EventHandler(this.labelGoToSignUp_Click);
this.labelGoToSignUp.MouseEnter += new System.EventHandler(this.labelGoToSignUp_MouseEnter);
this.labelGoToSignUp.MouseLeave += new System.EventHandler(this.labelGoToSignUp_MouseLeave);
//
// buttonLogin
//
this.buttonLogin.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(242)))), ((int)(((byte)(103)))), ((int)(((byte)(46)))));
this.buttonLogin.Cursor = System.Windows.Forms.Cursors.Hand;
this.buttonLogin.FlatAppearance.BorderSize = 0;
this.buttonLogin.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.buttonLogin.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.buttonLogin.ForeColor = System.Drawing.Color.White;
this.buttonLogin.Location = new System.Drawing.Point(45, 380);
this.buttonLogin.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.buttonLogin.Name = "buttonLogin";
this.buttonLogin.Size = new System.Drawing.Size(541, 74);
this.buttonLogin.TabIndex = 5;
this.buttonLogin.Text = "LOGIN";
this.buttonLogin.UseVisualStyleBackColor = false;
this.buttonLogin.Click += new System.EventHandler(this.buttonLogin_Click);
//
// textBoxPassword
//
this.textBoxPassword.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxPassword.Location = new System.Drawing.Point(120, 276);
this.textBoxPassword.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.textBoxPassword.Name = "textBoxPassword";
this.textBoxPassword.Size = new System.Drawing.Size(465, 53);
this.textBoxPassword.TabIndex = 4;
this.textBoxPassword.UseSystemPasswordChar = true;
//
// pictureBox2
//
this.pictureBox2.Image = global::Csharp_Login_And_Register.Properties.Resources._lock;
this.pictureBox2.Location = new System.Drawing.Point(45, 276);
this.pictureBox2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.pictureBox2.Name = "pictureBox2";
this.pictureBox2.Size = new System.Drawing.Size(67, 62);
this.pictureBox2.TabIndex = 3;
this.pictureBox2.TabStop = false;
//
// textBoxUsername
//
this.textBoxUsername.Font = new System.Drawing.Font("Microsoft Sans Serif", 24F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.textBoxUsername.Location = new System.Drawing.Point(120, 178);
this.textBoxUsername.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.textBoxUsername.Multiline = true;
this.textBoxUsername.Name = "textBoxUsername";
this.textBoxUsername.Size = new System.Drawing.Size(465, 61);
this.textBoxUsername.TabIndex = 2;
//
// pictureBox1
//
this.pictureBox1.Image = global::Csharp_Login_And_Register.Properties.Resources.user;
this.pictureBox1.Location = new System.Drawing.Point(45, 178);
this.pictureBox1.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(67, 62);
this.pictureBox1.TabIndex = 1;
this.pictureBox1.TabStop = false;
//
// panel2
//
this.panel2.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(183)))), ((int)(((byte)(19)))));
this.panel2.Controls.Add(this.labelClose);
this.panel2.Controls.Add(this.label1);
this.panel2.Location = new System.Drawing.Point(-8, 0);
this.panel2.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(640, 114);
this.panel2.TabIndex = 0;
//
// labelClose
//
this.labelClose.AutoSize = true;
this.labelClose.Cursor = System.Windows.Forms.Cursors.Hand;
this.labelClose.Font = new System.Drawing.Font("Arial", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.labelClose.ForeColor = System.Drawing.Color.White;
this.labelClose.Location = new System.Drawing.Point(604, 4);
this.labelClose.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.labelClose.Name = "labelClose";
this.labelClose.Size = new System.Drawing.Size(27, 27);
this.labelClose.TabIndex = 1;
this.labelClose.Text = "X";
this.labelClose.Click += new System.EventHandler(this.labelClose_Click);
this.labelClose.MouseEnter += new System.EventHandler(this.labelClose_MouseEnter);
this.labelClose.MouseLeave += new System.EventHandler(this.labelClose_MouseLeave);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Font = new System.Drawing.Font("Arial", 27.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Margin = new System.Windows.Forms.Padding(4, 0, 4, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(640, 114);
this.label1.TabIndex = 0;
this.label1.Text = "USER LOGIN";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// LoginForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(632, 523);
this.Controls.Add(this.panel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "LoginForm";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "LoginForm";
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
((System.ComponentModel.ISupportInitialize)(this.pictureBox2)).EndInit();
((System.ComponentModel.ISupportInitialize)(this.pictureBox1)).EndInit();
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label labelClose;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBoxPassword;
private System.Windows.Forms.PictureBox pictureBox2;
private System.Windows.Forms.TextBox textBoxUsername;
private System.Windows.Forms.PictureBox pictureBox1;
private System.Windows.Forms.Button buttonLogin;
private System.Windows.Forms.Label labelGoToSignUp;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Csharp_Login_And_Register
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
Application.Exit();
}
}
}

Panel visibility not changing in C# visual studio 2010

I'm trying to do something that i would think to be rather simple. I have 2 toolstrip items.
test1: ToolStripMenuItem which should show panel -> test_panel_1
test2: ToolStripMenuItem which should show panel -> panel1
test_panel_1 contains button -> panel_1_button1 which should hide test_panel_1 and show test_panel_2
panel1 contains button -> button1 which should hide test_panel2 and then show panel2
However, when I run the code and click on test1ToolStripMenuItem it shows test_panel_1 like it's supposed to, then when i click on panel_1_button_1 it just clears test_panel_1 and doesn't show test_panel_2. And regardless of what I click first, test2ToolStripMenuItem doesn't show panel1 at all.
Here's my code...
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 panel_test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_1_button1_Click(object sender, EventArgs e)
{
test_panel2.Visible = true;
test_panel_1.Visible = false;
}
private void test1ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel_1.Visible = true;
panel1.Visible = false;
}
private void button1_click(object sender, EventArgs e)
{
test_panel_1.Visible = false;
test_panel2.Visible = true;
}
private void test2ToolStripMenuItem_Click(object sender, EventArgs e)
{
test_panel2.Visible = false;
panel1.Visible = true;
}
private void button1_Click_1(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.panel2.Visible = true;
}
}
}
and, not sure if this helps, but...
namespace panel_test
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.menuStrip1 = new System.Windows.Forms.MenuStrip();
this.testToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test1ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test2ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test3ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.test_panel_1 = new System.Windows.Forms.Panel();
this.panel1 = new System.Windows.Forms.Panel();
this.button1 = new System.Windows.Forms.Button();
this.panel2 = new System.Windows.Forms.Panel();
this.label3 = new System.Windows.Forms.Label();
this.test_panel2 = new System.Windows.Forms.Panel();
this.label2 = new System.Windows.Forms.Label();
this.panel_1_button1 = new System.Windows.Forms.Button();
this.label1 = new System.Windows.Forms.Label();
this.menuStrip1.SuspendLayout();
this.test_panel_1.SuspendLayout();
this.panel1.SuspendLayout();
this.panel2.SuspendLayout();
this.test_panel2.SuspendLayout();
this.SuspendLayout();
//
// menuStrip1
//
this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.testToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(467, 24);
this.menuStrip1.TabIndex = 0;
this.menuStrip1.Text = "menuStrip1";
//
// testToolStripMenuItem
//
this.testToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.test1ToolStripMenuItem,
this.test2ToolStripMenuItem,
this.test3ToolStripMenuItem});
this.testToolStripMenuItem.Name = "testToolStripMenuItem";
this.testToolStripMenuItem.Size = new System.Drawing.Size(38, 20);
this.testToolStripMenuItem.Text = "test";
//
// test1ToolStripMenuItem
//
this.test1ToolStripMenuItem.Name = "test1ToolStripMenuItem";
this.test1ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test1ToolStripMenuItem.Text = "test1";
this.test1ToolStripMenuItem.Click += new System.EventHandler(this.test1ToolStripMenuItem_Click);
//
// test2ToolStripMenuItem
//
this.test2ToolStripMenuItem.Name = "test2ToolStripMenuItem";
this.test2ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test2ToolStripMenuItem.Text = "test2";
this.test2ToolStripMenuItem.Click += new System.EventHandler(this.test2ToolStripMenuItem_Click);
//
// test3ToolStripMenuItem
//
this.test3ToolStripMenuItem.Name = "test3ToolStripMenuItem";
this.test3ToolStripMenuItem.Size = new System.Drawing.Size(99, 22);
this.test3ToolStripMenuItem.Text = "test3";
//
// test_panel_1
//
this.test_panel_1.Controls.Add(this.test_panel2);
this.test_panel_1.Controls.Add(this.panel_1_button1);
this.test_panel_1.Controls.Add(this.label1);
this.test_panel_1.Location = new System.Drawing.Point(44, 28);
this.test_panel_1.Name = "test_panel_1";
this.test_panel_1.Size = new System.Drawing.Size(442, 317);
this.test_panel_1.TabIndex = 1;
this.test_panel_1.Visible = false;
//
// panel1
//
this.panel1.Controls.Add(this.button1);
this.panel1.Location = new System.Drawing.Point(218, 187);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(328, 318);
this.panel1.TabIndex = 1;
this.panel1.Visible = false;
//
// button1
//
this.button1.Location = new System.Drawing.Point(86, 155);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "panel 3 to 4";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click_1);
//
// panel2
//
this.panel2.Controls.Add(this.panel1);
this.panel2.Controls.Add(this.label3);
this.panel2.Location = new System.Drawing.Point(95, 194);
this.panel2.Name = "panel2";
this.panel2.Size = new System.Drawing.Size(398, 260);
this.panel2.TabIndex = 1;
this.panel2.Visible = false;
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(86, 118);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(71, 13);
this.label3.TabIndex = 0;
this.label3.Text = "this is panel 4";
//
// test_panel2
//
this.test_panel2.Controls.Add(this.panel2);
this.test_panel2.Controls.Add(this.label2);
this.test_panel2.Location = new System.Drawing.Point(154, 101);
this.test_panel2.Name = "test_panel2";
this.test_panel2.Size = new System.Drawing.Size(358, 237);
this.test_panel2.TabIndex = 3;
this.test_panel2.Visible = false;
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(70, 118);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(71, 13);
this.label2.TabIndex = 0;
this.label2.Text = "this is panel 2";
//
// panel_1_button1
//
this.panel_1_button1.Location = new System.Drawing.Point(86, 155);
this.panel_1_button1.Name = "panel_1_button1";
this.panel_1_button1.Size = new System.Drawing.Size(75, 23);
this.panel_1_button1.TabIndex = 2;
this.panel_1_button1.Text = "button1";
this.panel_1_button1.UseVisualStyleBackColor = true;
this.panel_1_button1.Click += new System.EventHandler(this.panel_1_button1_Click);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(28, 21);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(42, 13);
this.label1.TabIndex = 0;
this.label1.Text = "panel 1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(467, 357);
this.Controls.Add(this.test_panel_1);
this.Controls.Add(this.menuStrip1);
this.MainMenuStrip = this.menuStrip1;
this.Name = "Form1";
this.Text = "Form1";
this.menuStrip1.ResumeLayout(false);
this.menuStrip1.PerformLayout();
this.test_panel_1.ResumeLayout(false);
this.test_panel_1.PerformLayout();
this.panel1.ResumeLayout(false);
this.panel2.ResumeLayout(false);
this.panel2.PerformLayout();
this.test_panel2.ResumeLayout(false);
this.test_panel2.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem testToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test1ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test2ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem test3ToolStripMenuItem;
private System.Windows.Forms.Panel test_panel_1;
private System.Windows.Forms.Panel test_panel2;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Button panel_1_button1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Panel panel2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Button button1;
}
}
I've toyed with Usercontrols a little, and i like the way i can edit and view them much better than panels; however, i don't know how to control hiding and showing them.
Thanks for the help. I know this must be coding 101, but it's something i haven't quite fully figured out yet.
It looks like your test_panel_2 is a child of test_panel_1. panel1 is a child of panel2. This is likely not what you intended. What's happening is that, because test_panel_2 is inside test_panel_1, hiding test_panel_1 also hides test_panel_2. There's a hierarchy there.
There's one or two spots where you set the visibility to false again - I'm not sure if those are correct.

Creating an Inputbox in C# using forms

Hello I'm currently creating an application which has the need to add server IP addresses to it, as there is no InputBox function in C# I'm trying to complete this using forms, but am very new to the language so not 100% as to what I should do.
At the moment I have my main form and a form which will act as my inputbox which wants to hide on load. Then when the user clicks on the add IP Address on the main form I wish to open up the secondary form and return the IP address entered into a text box on the secondary form.
So how would I go about doing this? Or is there any better ways to achieve similar results?
In your main form, add an event handler for the event Click of button Add Ip Address. In the event handler, do something similar as the code below:
private string m_ipAddress;
private void OnAddIPAddressClicked(object sender, EventArgs e)
{
using(SetIPAddressForm form = new SetIPAddressForm())
{
if (form.ShowDialog() == DialogResult.OK)
{
//Create a property in SetIPAddressForm to return the input of user.
m_ipAddress = form.IPAddress;
}
}
}
Edit: Add another example to fit with manemawanna comment.
private void btnAddServer_Click(object sender, EventArgs e)
{
string ipAdd;
using(Input form = new Input())
{
if (form.ShowDialog() == DialogResult.OK)
{
//Create a property in SetIPAddressForm to return the input of user.
ipAdd = form.IPAddress;
}
}
}
In your Input form, add a property:
public class Input : Form
{
public string IPAddress
{
get { return txtInput.Text; }
}
private void btnInput_Click(object sender, EventArgs e)
{
//Do some validation for the text in txtInput to be sure the ip is well-formated.
if(ip_well_formated)
{
this.DialogResult = DialogResult.OK;
this.Close();
}
}
}
You could just use the VB InputBox...
Add reference to Microsoft.VisualBasic
string result = Microsoft.VisualBasic.Interaction.InputBox("Title","text", "", 10, 20);
I've needed this feature, too. Here's my code; it auto-centers and sizes to fit the prompt. The public method creates a dialog and returns the user's input, or null if they cancel.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Utilities
{
public class InputBox
{
#region Interface
public static string ShowDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null)
{
InputBoxDialog form = new InputBoxDialog(prompt, title, defaultValue, xPos, yPos);
DialogResult result = form.ShowDialog();
if (result == DialogResult.Cancel)
return null;
else
return form.Value;
}
#endregion
#region Auxiliary class
private class InputBoxDialog: Form
{
public string Value { get { return _txtInput.Text; } }
private Label _lblPrompt;
private TextBox _txtInput;
private Button _btnOk;
private Button _btnCancel;
#region Constructor
public InputBoxDialog(string prompt, string title, string defaultValue = null, int? xPos = null, int? yPos = null)
{
if (xPos == null && yPos == null)
{
StartPosition = FormStartPosition.CenterParent;
}
else
{
StartPosition = FormStartPosition.Manual;
if (xPos == null) xPos = (Screen.PrimaryScreen.WorkingArea.Width - Width ) >> 1;
if (yPos == null) yPos = (Screen.PrimaryScreen.WorkingArea.Height - Height) >> 1;
Location = new Point(xPos.Value, yPos.Value);
}
InitializeComponent();
if (title == null) title = Application.ProductName;
Text = title;
_lblPrompt.Text = prompt;
Graphics graphics = CreateGraphics();
_lblPrompt.Size = graphics.MeasureString(prompt, _lblPrompt.Font).ToSize();
int promptWidth = _lblPrompt.Size.Width;
int promptHeight = _lblPrompt.Size.Height;
_txtInput.Location = new Point(8, 30 + promptHeight);
int inputWidth = promptWidth < 206 ? 206 : promptWidth;
_txtInput.Size = new Size(inputWidth, 21);
_txtInput.Text = defaultValue;
_txtInput.SelectAll();
_txtInput.Focus();
Height = 125 + promptHeight;
Width = inputWidth + 23;
_btnOk.Location = new Point(8, 60 + promptHeight);
_btnOk.Size = new Size(100, 26);
_btnCancel.Location = new Point(114, 60 + promptHeight);
_btnCancel.Size = new Size(100, 26);
return;
}
#endregion
#region Methods
protected void InitializeComponent()
{
_lblPrompt = new Label();
_lblPrompt.Location = new Point(12, 9);
_lblPrompt.TabIndex = 0;
_lblPrompt.BackColor = Color.Transparent;
_txtInput = new TextBox();
_txtInput.Size = new Size(156, 20);
_txtInput.TabIndex = 1;
_btnOk = new Button();
_btnOk.TabIndex = 2;
_btnOk.Size = new Size(75, 26);
_btnOk.Text = "&OK";
_btnOk.DialogResult = DialogResult.OK;
_btnCancel = new Button();
_btnCancel.TabIndex = 3;
_btnCancel.Size = new Size(75, 26);
_btnCancel.Text = "&Cancel";
_btnCancel.DialogResult = DialogResult.Cancel;
AcceptButton = _btnOk;
CancelButton = _btnCancel;
Controls.Add(_lblPrompt);
Controls.Add(_txtInput);
Controls.Add(_btnOk);
Controls.Add(_btnCancel);
FormBorderStyle = FormBorderStyle.FixedDialog;
MaximizeBox = false;
MinimizeBox = false;
return;
}
#endregion
}
#endregion
}
}
Add a button in main form.
Create a form with textbox for ip address. (lets say it IPAddressForm)
Add click event handler for that button.
In the event handler, create an instance of IPAddressForm and call showdialog method of IPAddressForm.
Store the ip address in some class variable.
If the showdialog result is ok, read the class variable from main form (simplest way is to declare the field as public)
Looks like Francis has the correct idea which is what I would have suggested. However, just to add to this I would probably suggest using a MaskedTextBox instead of a basic TextBox and add the IP Address format as the Mask.
You can create your special messagebox. I created my messagebox for getting database information like below. And when the messagebox open, application stop during you click any button in related messagebox.
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;
using System.Data.Sql;
namespace Palmaris_Installation
{
public class efexBox
{
public static string ShowDialog()
{
PopUpDatabase form = new PopUpDatabase();
DialogResult result = form.ShowDialog();
if (result == DialogResult.Cancel)
return null;
else
{
if (form.ValueAuthentication == "SQL Server Authentication")
return form.Valueservername + "?" + form.ValueAuthentication + "?" + form.ValueUsername + "?" + form.ValuePassword;
else
return form.Valueservername + "?" + form.ValueAuthentication + "?" + "" + "?" + "";
}
}
public partial class PopUpDatabase : Form
{
public PopUpDatabase()
{
InitializeComponent();
SqlDataSourceEnumerator instance = SqlDataSourceEnumerator.Instance;
DataTable table = instance.GetDataSources();
foreach (DataRow row in table.Rows)
{
cmbServerName.Items.Add(row[0] + "\\" + row[1]);
}
cmbAuthentication.Items.Add("Windows Authentication");
cmbAuthentication.Items.Add("SQL Server Authentication");
return;
}
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.cmbServerName = new System.Windows.Forms.ComboBox();
this.cmbAuthentication = new System.Windows.Forms.ComboBox();
this.txtUserName = new System.Windows.Forms.TextBox();
this.txtPassword = new System.Windows.Forms.TextBox();
this.btnCancel = new System.Windows.Forms.Button();
this.btnConnect = new System.Windows.Forms.Button();
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedToolWindow;
this.MaximizeBox = false;
this.groupBox1.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.btnConnect);
this.groupBox1.Controls.Add(this.btnCancel);
this.groupBox1.Controls.Add(this.txtPassword);
this.groupBox1.Controls.Add(this.txtUserName);
this.groupBox1.Controls.Add(this.cmbAuthentication);
this.groupBox1.Controls.Add(this.cmbServerName);
this.groupBox1.Controls.Add(this.label4);
this.groupBox1.Controls.Add(this.label3);
this.groupBox1.Controls.Add(this.label2);
this.groupBox1.Controls.Add(this.label1);
this.groupBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.groupBox1.Location = new System.Drawing.Point(0, 0);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(348, 198);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Database Configration";
this.groupBox1.BackColor = Color.Gray;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(50, 46);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(69, 13);
this.label1.TabIndex = 0;
this.label1.Text = "Server Name";
//
// label2
//
this.label2.AutoSize = true;
this.label2.Location = new System.Drawing.Point(50, 73);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(75, 13);
this.label2.TabIndex = 0;
this.label2.Text = "Authentication";
//
// label3
//
this.label3.AutoSize = true;
this.label3.Location = new System.Drawing.Point(50, 101);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(60, 13);
this.label3.TabIndex = 0;
this.label3.Text = "User Name";
//
// label4
//
this.label4.AutoSize = true;
this.label4.Location = new System.Drawing.Point(50, 127);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(53, 13);
this.label4.TabIndex = 0;
this.label4.Text = "Password";
//
// cmbServerName
//
this.cmbServerName.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbServerName.FormattingEnabled = true;
this.cmbServerName.Location = new System.Drawing.Point(140, 43);
this.cmbServerName.Name = "cmbServerName";
this.cmbServerName.Size = new System.Drawing.Size(185, 21);
this.cmbServerName.TabIndex = 1;
//
// cmbAuthentication
//
this.cmbAuthentication.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.cmbAuthentication.FormattingEnabled = true;
this.cmbAuthentication.Location = new System.Drawing.Point(140, 70);
this.cmbAuthentication.Name = "cmbAuthentication";
this.cmbAuthentication.Size = new System.Drawing.Size(185, 21);
this.cmbAuthentication.TabIndex = 1;
this.cmbAuthentication.SelectedIndexChanged += new System.EventHandler(this.cmbAuthentication_SelectedIndexChanged);
//
// txtUserName
//
this.txtUserName.Location = new System.Drawing.Point(140, 98);
this.txtUserName.Name = "txtUserName";
this.txtUserName.Size = new System.Drawing.Size(185, 20);
this.txtUserName.TabIndex = 2;
//
// txtPassword
//
this.txtPassword.Location = new System.Drawing.Point(140, 124);
this.txtPassword.Name = "txtPassword";
this.txtPassword.Size = new System.Drawing.Size(185, 20);
this.txtPassword.TabIndex = 2;
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(250, 163);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(75, 23);
this.btnCancel.TabIndex = 3;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
this.btnCancel.DialogResult = DialogResult.Cancel;
//
// btnConnect
//
this.btnConnect.Location = new System.Drawing.Point(140, 163);
this.btnConnect.Name = "btnConnect";
this.btnConnect.Size = new System.Drawing.Size(75, 23);
this.btnConnect.TabIndex = 3;
this.btnConnect.Text = "Connect";
this.btnConnect.UseVisualStyleBackColor = true;
this.btnConnect.DialogResult = DialogResult.OK;
//
// PopUpDatabase
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(348, 198);
this.Controls.Add(this.groupBox1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Name = "PopUpDatabase";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "::: Database Configration :::";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.ResumeLayout(false);
}
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtPassword;
private System.Windows.Forms.TextBox txtUserName;
private System.Windows.Forms.ComboBox cmbAuthentication;
private System.Windows.Forms.ComboBox cmbServerName;
private System.Windows.Forms.Button btnConnect;
private System.Windows.Forms.Button btnCancel;
public string ValueUsername { get { return txtUserName.Text; } }
public string ValuePassword { get { return txtPassword.Text; } }
public string Valueservername { get { return cmbServerName.SelectedItem.ToString(); } }
public string ValueAuthentication { get { return cmbAuthentication.SelectedItem.ToString(); } }
private void cmbAuthentication_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbAuthentication.SelectedIndex == 1)
{
txtUserName.Enabled = true;
txtPassword.Enabled = true;
}
else
{
txtUserName.Enabled = false;
txtPassword.Enabled = false;
}
}
}
}
}
and in your main application call like :
string[] strPopUp = efexBox.ShowDialog().Split('?');

Categories