Show password in textbox while holding a button - c#

I have a textbox in my WinForm and when I type the password in, its hidden because:
private void textBoxPWMain2_TextChanged(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}
is it possible to add here a button, and while the button is pressed, the password show normal and when I stop pressing the button, the password will hide again?

Maybe this? (Don't forget to subscribe to these events)
private void button2_MouseDown(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button2_MouseUp(object sender, EventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}

I have a solution now, I wanted something like a eye button, when you press it down the password shows, when you stop pressing, the password hides again.
Solution
First I added a pictureBox with Eye Icon and added this pictureBox to my password textbox and set Passwort textbox to .UseSystemPasswordChar
public Form1
{
textBoxPW.Controls.Add(pictureBoxEye);
pictureBoxEye.Location = new Point(95,0);
pictureBoxEye.BackColor = Color.Transparent;
textBoxPW.UseSystemPasswordChar = true;
//Subscribe to Event
pictureBoxPW.MouseDown += new MouseEventHandler(pictureBoxPW_MouseDown);
pictureBoxPW.MouseUp += new MouseEventHandler(pictureBoxPW_MouseUp);
}
Added the Mouse_Down/Up Event
private void pictureBoxEye_MouseDown(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = false;
}
private void pictureBoxEye_MouseUp(object sender, MouseEventArgs e)
{
textBoxPW.UseSystemPasswordChar = true;
}
This works fine for me! Thank you guys !!

Adding a bit change details to ispiro's answer
public void button1_MouseDown(object sender, EventArgs e)
{
textBox1.PasswordChar = '\0';
textBox1.UseSystemPasswordChar = false;
}
public void button1_MouseUp(object sender, EventArgs e)
{
textBox1.PasswordChar = '*';
textBox1.UseSystemPasswordChar = true;
}
Before:-
After :-

Is there a reason you set the UseSystemPasswordChar in the TextChanged event?
If you can set the property in the Initialize() method or in the constructor you can implement the following events for your button:
private void button1_MouseDown(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = false;
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
textBoxPWMain2.UseSystemPasswordChar = true;
}

Related

WebBrowser.DocumentCompleted and Navigate - C#

I'm trying to modify a simple application which use the WebBrowser item:
private void button1_Click(object sender, EventArgs e)
{
var menu = webBrowser1.Document.GetElementById("SomeItem").InvokeMember("click");
webBrowser1.Visible = true;
button1.Visible = false;
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.Visible = false;
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("domain");
}
This works fine, the page load and then after clicking the button the user is redirected to the desired location.
Now I'm trying to make this without the help of the button.
Simply put the button code inside the load function didn't help, so I've introduced in my code the DocumentCompleted event.
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.AllowNavigation = true;
webBrowser1.Navigate("SomeDomain");
webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(wb_DocumentComplete);
}
private void wb_DocumentComplete(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser wb = sender as WebBrowser;
wb.Document.GetElementById("SomeItem").InvokeMember("click");
}
By debugging it, I saw that the wb object is correctly instantiated with the "domain" but I got System.NullReferenceException when I try to retrieve SomeItem.
The document is not fully loaded because I'm still in the load function ?

C# Help Using Multiple Forms

I am trying to create a application with multiple forms,
I have placed the forms on top of each other in the section needed.
However, the code as provided does not work.
private void home_butt_Click(object sender, EventArgs e)
{
Home_Panel.Visible = true;
Home_Panel.BringToFront();
}
private void Intro_butt_Click(object sender, EventArgs e)
{
Introduction.Visible = true;
Introduction.BringToFront();
Home_Panel.Visible = false;
Crime.Visible = false;
}
private void Crime_butt_Click(object sender, EventArgs e)
{
Crime.Visible = true;
Crime.BringToFront();
Home_Panel.Visible = false;
Introduction.Visible = false;
}
It will the home_Panel on start up but when you click on a button it will only show the Crimes Panel.
Any help will be appreciated.
Why not use the show and hide methods for forms instead of bringing them to the front and setting the visibility? E.G.
private void home_butt_Click(object sender, EventArgs e)
{
Home_Panel.Show();
Crime.Hide();
Introduction.Hide();
}
And vice versa for the other button events?

GlobalHotKey not firing any events

I googled around to find a way to have a GlobalHotKey on my C# Winform.
I saw this tutorial and followed it.
The issue I'm having now is that nothing is occurring once I press the designated key.(In this case, the ` key)
Here is the class I'm using
Here is the relevant code on my Form1.cs
Hotkey hk = new Hotkey();
private void Form1_Load(object sender, EventArgs e)
{
hk.KeyCode = Keys.Oemtilde;
hk.Windows = true;
hk.Pressed += hk_Pressed;
hk.Register(this);
}
private void hk_Pressed(object sender, EventArgs e)
{
MessageBox.Show("pressed");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (hk.Registered)
{ hk.Unregister(); }
}
Because you set hk.Windows = true; which required you to press Windows+ ``, remove that line and press `, it will work

How can i separate the user triggered and program triggered on textbox1_TextChange Event in C#

I'm using C# .net 4.0 VS 2010.
I'm trying to make a textbox that emulates facebook behavior, particular there is "+Enter Message" on a textbox and is colored gray. I also swapped the tabindex so the textbox does not selected by default (destroying the illusion).
Supposedly when the user clicked on the textbox the textbox.text disappears then the Forecolor back to black.
What happens is, it detects my program changes i put on Form_Load and runs the Event even before it's displayed.
How can i separate the user triggered and program triggered on textbox1_TextChange Event.
Here is my code:
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//facebook illusion
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
this.textBox1.Text = "+Enter Message";
}
//when the user clicks on the textbox
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() == "+Enter Message")
{
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
this.textBox1.Text = "";
}
}
Just for Reference this is the final working codes-------------
private void Form1_Load(object sender, EventArgs e)
{
//facebook illusion
this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
this.textBox1.Text = "+Enter Message";
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
this.textBox1.Click += new System.EventHandler(this.textBox1_Click);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() == "+Enter Message")
{
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
this.textBox1.Text = "";
}
}
private void textBox1_Click(object sender, EventArgs e)
{
if (this.textBox1.Text.Trim() == "+Enter Message")
{
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
this.textBox1.Text = "";
}
}
You can suppress the events by removing the handle first
this.textBox1.TextChanged -= new System.EventHandler(this.textBox1_TextChanged);
then adding it again or just adding the event after the text has ben changed in form_load
this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
You can subscribe the TextChanged event after the initialization, for example:
private void Form1_Load(object sender, EventArgs e)
{
//facebook illusion
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#CCCCCC");
this.textBox1.Text = "+Enter Message";
this.textBox1.TextChanged += textBox1_TextChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
this.textBox1.ForeColor = System.Drawing.ColorTranslator.FromHtml("#000000");
this.textBox1.Text = "";
}
And remove it from the designer. Alternatively you can set up the ForeColor and the Text "+Enter message" directly in the designer, in this way the initialization is done before the TextChanged event subscribing.

I need to close the previous active window when navigating a windows form with menustrip

I'm putting together a simple UI that interacts with a SQL database. My problem is a UI problem, ever time a menustrip item is selected, it opens a new active window. How do I set this up to close the previous active window? I've tried using Form.Close();, but that just closes everything.
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_campus go = new if_add_campus();
go.Show();
}
private void addDepartmentToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_dept go = new if_add_dept();
go.Show();
}
private void addEmployeToolStripMenuItem_Click(object sender, EventArgs e)
{
if_add_employee go = new if_add_employee();
go.Show();
}
Just keep track of the last form you created in a variable:
private Form lastForm;
private void showForm(Form frm) {
frm.FormClosed += (sender, ea) => {
if (object.ReferenceEquals(lastForm, sender)) lastForm = null;
};
frm.Show();
if (lastForm != null) lastForm.Close();
lastForm = frm;
}
And use showForm() to display your forms:
private void addCampusToolStripMenuItem_Click(object sender, EventArgs e)
{
showForm(new if_add_campus());
}
Not tested, should be close.

Categories