Reading password from TextBox - c#

I have created a Password field in C# as
public System.Windows.Forms.TextBox passwordBox;
other setting made for this field are
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.CornflowerBlue;
this.ClientSize = new System.Drawing.Size(381, 199);
this.Controls.Add(this.button1);
this.Controls.Add(this.panel1);
this.Controls.Add(this.label1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "Progpassword";
this.Text = "Programmer Password";
this.TransparencyKey = System.Drawing.Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
this.Load += new System.EventHandler(this.Progpassword_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.ResumeLayout(false);
this.PerformLayout();
I am not able to read the text entered in the TextBox
I am reading the value using following line
string text = PasswordBox.Text;
Is this the right way to read the password field.

Yes text = passwordBox.Text; is only that way how you can get it!

Related

C# Form - transparent applications WebView2 unclickable

When I create transparent applications with WebView2 c# form,
It is not possible to click on visible elements.
When I click, it clicks on the window behind.
I don't have the problem with WebBrowser
Do you have a solution ?
Thank you
//
// webView21
//
this.webView21.CreationProperties = null;
this.webView21.DefaultBackgroundColor = System.Drawing.Color.Transparent;
this.webView21.Location = new System.Drawing.Point(462, 12);
this.webView21.Name = "webView21";
this.webView21.Size = new System.Drawing.Size(337, 429);
this.webView21.Source = new System.Uri("https://***", System.UriKind.Absolute);
this.webView21.TabIndex = 2;
this.webView21.ZoomFactor = 1D;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.webBrowser1);
this.Controls.Add(this.webView21);
this.Name = "Form1";
this.Text = "Form1";
this.TransparencyKey = System.Drawing.Color.Transparent;
((System.ComponentModel.ISupportInitialize)(this.webView21)).EndInit();
this.ResumeLayout(false);

Finding an existing panel in form using Controls.Find C# Windows Form

I am making a form system where i need to find a panel to change it's background using string imploration. I have done something similar for a label using the code below where numberEntered is a seperate interger in the program.
Label label = Controls.Find($"Num{numberEntered}", true).OfType<Label>().FirstOrDefault();
label.Text = "Text"
How can I do something similar for a panel where where I can find a panel using a seperate variable in the name? Such as $"Panel{number}"
I have already tried this:
Panel panel = Controls.Find($"Ans{answerNum}Panel", true).OfType<Panel>().FirstOrDefault();
panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;
However it throws a NullReferenceException. Any help is much appreciated!
Try it this way please:
String ctlName = $"Ans{answerNum}Panel";
Panel panel = this.Controls.Find(ctlName, true).FirstOrDefault() as Panel;
if (panel != null) {
panel.BackgroundImage = Programming_Project.Properties.Resources.MutliChoiceCorrectSelected;
}
else {
MessageBox.Show("Unable to find " + ctlName);
}
You can try to open Form1.Designer.cs where Form1 is your form name.
Then find the panel code and modify it.
That's how I tried and it worked.
It should look something like this:`
private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.SuspendLayout();
//
// panel1
//
**this.panel1.BackColor = System.Drawing.SystemColors.ActiveCaption;**
this.panel1.Location = new System.Drawing.Point(169, 41);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(427, 190);
this.panel1.TabIndex = 0;
this.panel1.Paint += new
System.Windows.Forms.PaintEventHandler(this.panel1_Paint);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}

Cannot implicitly convert type 'string' to 'System.Windows.Forms.TextBox'

I don't know what to do because the error is in the Form1.Designer.cs and because I have no experience in debugging that part of the program.
//Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(352, 246);
this.Controls.Add(this.groupBox2);
this.Controls.Add(this.groupBox1);
this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Name = "Form1";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Generate Username";
this.groupBox1.ResumeLayout(false);
this.groupBox1.PerformLayout();
this.groupBox2.ResumeLayout(false);
this.groupBox2.PerformLayout();
this.ResumeLayout(false);
The error is on this.Name = "Form1";
I suspect you have created a control named Name, which conflicts with the Name property of the window. Just rename the control to something else and it should work again.
The error must come from somewhere else, there's nothing here with a TextBox. The error is probably caused by assigning a string to a TextBox itself instead of assigning a string to the Text property of the TextBox.
Example:
TextBox tb = new TextBox();
tb = "Default text";
This should be:
TextBox tb = new TextBox();
tb.Text = "Default text";
Otherwise you have created a control with a name like Name or Text, in which case you'll have to rename it to NameTextBox or something.

User Control Custom Property lose value when build

I have a UserControl named "UserControl1" with a label inside it and a custom property:
[Browsable(true)]
public new string Text
{
get { return label1.Text; }
set { label1.Text = value; }
}
public UserControl1()
{
InitializeComponent();
}
This UserControl is used in a form named "Form1".
In the designer appears the property but when I write some text and build the application the text is cleared. I can see, the property isn't written in the Form1.Designer.cs.
If I change the property name to some other word all is ok.. Note the "new" keyword to override the base variable..
I have found a similar question here but there is no solution.
Greetings!
EDIT: There is no hardcoded value:
UserControl1.Designer.cs:
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(64, 63);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(35, 13);
this.label1.TabIndex = 0;
//
// UserControl1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Controls.Add(this.label1);
this.Name = "UserControl1";
this.Size = new System.Drawing.Size(181, 136);
this.ResumeLayout(false);
this.PerformLayout();
Form1.Designer.cx:
//
// userControl11
//
this.userControl11.Location = new System.Drawing.Point(35, 43);
this.userControl11.Name = "userControl11";
this.userControl11.Size = new System.Drawing.Size(181, 136);
this.userControl11.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.userControl11);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
To reproduce the problem just create a new windows form application, create an user control with a label inside it and with the property named "Text".
Try using override instead of new for the Text property and include the DesignerSerializationVisibility attribute:
[Browsable(true)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
public override string Text {
get { return label1.Text; }
set { label1.Text = value; }
}
Look inside InitializeComponent(). It is most likely hard-coding a default value when you place the control on the design surface.

How can I hide my application's form in the Windows Taskbar? [duplicate]

This question already has answers here:
Hiding winforms app from taskbar
(2 answers)
Closed 6 years ago.
How can I hide the name of my application in the Windows Taskbar, even when it is visible?
Currently, I have the following code to initialize and set the properties of my form:
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(0, 0);
this.Controls.Add(this.eventlogs);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Minimized;
this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.Form1_FormClosing);
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
To prevent your form from appearing in the taskbar, set its ShowInTaskbar property to False.
this.ShowInTaskbar = false;

Categories