C# Help Using Multiple Forms - c#

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?

Related

Show password in textbox while holding a button

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;
}

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 ?

fast-report close designer without save dialog

I'm working on an application that uses fast-report.
If I modified the report and close the designer window I get asked if I want to save "Untitled.frx". How can I prevent this dialog from appearing?
Thanks in advance
There is AskSave property:
var report = new Report();
Config.DesignerSettings.DesignerLoaded += (object sender, EventArgs e) =>
{
report.Designer.AskSave = false;
};
report.Design();
This is how I solved the problem. The DesignerSettings_DesignerClosed method is to prevent problems if i reopen the Designer later.
Thanks #Grzegorz Brzęczyszczykiewicz for the tip with AskSave().
...
{
Config.DesignerSettings.DesignerLoaded += DesignerSettings_DesignerLoaded;
Config.DesignerSettings.DesignerClosed += DesignerSettings_DesignerClosed;
Report.ReportData.Design(false);
}
private void DesignerSettings_DesignerClosed(object sender, EventArgs e) {
Config.DesignerSettings.DesignerLoaded -= DesignerSettings_DesignerLoaded;
Config.DesignerSettings.DesignerClosed -= DesignerSettings_DesignerClosed;
}
private void DesignerSettings_DesignerLoaded(object sender, EventArgs e) {
Report.ReportData.Designer.AskSave = false;
}

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

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