Hide and Show Second form on Button Click - c#

I want to show another form(Form2) with button click. Basically When the button is clicked in Form1, another form(Form2) should show, this should not hide the Form1 though and should change the button text to "Hide Progress" in Form1. And when this button is clicked again the Form2 should hide and the text in button should change to "Show Progress".
Below is my effort to make this work. When I clicked the Show Progress Button, it brings the Form2 and changes the text in button as well. But when I clicked the button again instead of hiding the Form2, it opens another instance of Form2.
Probably reason behind is that, the bool value is not saved.
Here is my code for button event handler.
public partial class Main : Form
{
public string output_green, output_grey, output_blue, output_black;
public bool visible;
private void button1_Click(object sender, EventArgs e)
{
output progressWindow = new output();
if (visible == false)
{
progressWindow.Show();
button1.Text = "Hide Progress";
visible = true;
}
else
{
progressWindow.Show();
button1.Text = "Show Progress";
visible = false;
}
}
}
How can I achieve that I need to.

Problem:
Each time you click button1 a new progressWindow has been initialized.
Also you are using progressWindow.Show() instead of Hide() in else part.
Solution:
Declare progressWindow out of button1_Click. And then initialize it from button1_Click. Now it will be initialized only once (using if).
output progressWindow = null;
private void button1_Click(object sender, EventArgs e)
{
if(progressWindow == null)
progressWindow = new output();
if (button1.Text == "Show Progress")
{
progressWindow.Show();
button1.Text = "Hide Progress";
}
else
{
progressWindow.Hide();
button1.Text = "Show Progress";
}
}
}

For a shorter solution where the progress window's lifetime persists with the main form :
output progressWindow = new output();
private void button1_Click(object sender, EventArgs e)
{
progressWindow.Visible = !progressWindow.Visible;
button1.Text = (progressWindow.Visible) ? "Hide Progress" : "Show Progress";
}
Here you get rid of the need for the extra boolean since the progress form itself is fully capable of telling you whether it is visible or not.

// Creates a single instance only it it is request.
private Output ProgressWindow
{
get
{
return progressWindow?? (progressWindow= new Output(){Visible = false};
}
}
private Output progressWindow;
private void button1_Click(object sender, EventArgs e)
{
ProgressWindow.Visible = !ProgressWindow.Visible;
button1.Text = (ProgressWindow.Visible) ? "Hide Progress" : "Show Progress";
}
}

Related

How do I pause or stop an audio file, which is being played

The button click method mentioned below, can be used to start playing an audio file. However, once it starts it cannot be paused, if I click the button again the song will start to play again, from the beginning. Please let me know the way to stop it using Enter Key.
private void btn_reproducir_Click(object sender, EventArgs e)
{
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = #"C:\song.mp3";
myplayer.controls.play();
}
Please try this code.
public partial class Form1 : Form
{
// Media player object
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
public Form1()
{
InitializeComponent();
myplayer.URL = #"C:\song.mp3";
}
private void reproducir_Click(object sender, EventArgs e)
{
myplayer.controls.play();
}
private void btnStop_Click(object sender, EventArgs e)
{
myplayer.controls.stop();
}
private void btnPause_Click(object sender, EventArgs e)
{
myplayer.controls.pause();
}
}
If you want to use the same button to play and pause the music, you can use a flag to determine its behavior:
bool isPlaying = false;
private void btn_reproducir_Click(object sender, EventArgs e)
{
WindowsMediaPlayer myplayer = new WindowsMediaPlayer();
myplayer.URL = #"C:\song.mp3";
if(isPlaying)
{
myplayer.controls.pause();
isPlaying = false;
}
else
{
myplayer.controls.play();
isPlaying = true;
}
}
If you want to use the enter key to click the button from anywhere in the form, simply set the form's "AcceptButton" to the button you want clicked. As long as the control that currently has focus does not use the enter key, then pressing the enter key will automatically click the AcceptButton. A scenario where this would not work would be if a multi-line text box has focus. A multi-line text box allows you to press enter to start a new line, so the AcceptButton would not be clicked in this case.
Additionally, you can always use the tab key to tab to the button and then press enter once the button is highlighted.

WinForm method does not have access to panel properties

I'm trying to make a WinForm app with a expandable menu. I'm trying to create a method that will change a panel propierties in form1 but it sees the value as false even if it is not? I got message box with "recognition as false". Instead of code I would rather a nice explanation
private void JobSelectButton_Click(object sender, EventArgs e)
{
//button to open a panel with "menu"
if (jobPanel.Visible == false)
{
// the panel is visible from this point
jobPanel.Visible = true;
}
else
{
jobPanel.Visible = false;
}
}
public void fla(bool flag)
{ //method inside form1 and it should make jobPanel.Visible = false
if (jobPanel.Visible == false)
MessageBox.Show("recognition as false");
else jobPanel.Visible = false;
}
private void notification_MouseClick(object sender, MouseEventArgs e)
{ // user control that will call the method
caseNBvalue = lbl_caseNB.Text;
profile f2 = new profile();
f2.fla(true);
}

Assigning text to buttons Via methods

I'm messing around trying to make a tictactoe game and am wondering how to use this method I created to alter the text of a button.
private void Click()
{
if (player1 == true)
{
player1 = false;
player2 = true;
this.Text = "X";
}
if (player2 == true)
{
player1 = true;
player2 = false;
this.Text = "O";
}
}
private void button1_Click(object sender, EventArgs e)
{
Click();
}
So I want to be able to change the text of the buttons, I found out that this relates to the actual form. I couldn't find any ways to assign the text to different buttons only if you are specific. Cheeeeers.
"this" is your form. You need to assign your text to button.Text.
Since your are writing a tic-tac-toe you could use the same event for all buttons :)
private void button1_Click(object sender, EventArgs e)
{
var button = (Button) sender;
button.Text = "X";
}
You could add the button1 as a parameter to your click event, like:
private void Click(Control control) {
if (!string.IsNullOrEmpty(control.Text)) {
// invalid button clicked (has already x or o)
return;
}
control.Text = player1 ? 'X' : 'O';
player1 = !player1;
player2 = !player2;
}
and then use the click event as:
private void GeneralButtonHandler(object sender, EventArgs e)
{
Click(sender as Control);
}
after which you could attach all buttons to this GeneralButtonHandler
and if you need more ideas for the game, you could find a javascript implementation here

How can I move a button with mousehover?

I've made a TextBox that retains what you type, and when you click the button associated it gives you a messagebox. When people want to click no, I want the button to change location so people cannot click it so they are forced to click yes,Well the problem is mousehover works just 1 time and does not go back to initial position after i leave my pointer out. Can you help me? Here is the code:
{
MsgBox = new CustomMsgBox();
MsgBox.label1.Text = Text;
MsgBox.button1.Text = btnOK;
MsgBox.button2.Text = btnCancel;
MsgBox.Text = Caption;
result = DialogResult.No;
MsgBox.ShowDialog();
return result;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(+50, +50);
}
private void button2_MouseLeave(object sender, EventArgs e)
{
button2.Location = new Point(+100, +100);
}
You should not use MouseLeave because when you move the button in the MouseHover, the button will move so the mouse will leave the button area. Meaning the No button will flip from the original position to the new position and back again all the time. What you could do is use the MouseMove to see if the user moved away from the area where the Button2 was originally and then move it back. Or include a non-visible control, like an empty label behind the button2. And set the MouseLeave on the label instead.
Oh and don't forget to set button2.TabStop = false, otherwise the user can use tab to get to the button.
Made a quick and dirty proof of concept for this, hope that helps ;)
public partial class Form1 : Form
{
private Rectangle buttonRectangle;
private bool checkRectangle = false;
public Form1()
{
InitializeComponent();
button2.TabStop = false;
buttonRectangle = button2.ClientRectangle;
buttonRectangle.Location = button2.Location;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Location = new Point(25, 25);
}
private void button2_MouseHover(object sender, EventArgs e)
{
button2.Location = new Point(50, 50);
checkRectangle = true;
}
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if (!checkRectangle)
{
return;
}
if (!buttonRectangle.Contains(e.X, e.Y))
{
checkRectangle = false;
button2.Location = buttonRectangle.Location;
}
}
}
The buttonRectangle is set based on where the button is found during construction of the form. It has a contains method that can be used to check if a certain point (mouse move) is contained in it.
I also set the button2.TabStop to false so it will no longer be active during tab cycles.
When your hover (can change this to mouse enter, but just used your code) event fires, I set checkRectangle to true. I use this in the mouse move event handler to see if anything should be checked (prevents from doing anything when the mouse is not "over" the button).
If the buttonRectangle and the mouse location are not intersected this means we left the area where the button was, so we can move the button back.

Display Ballon Tip on Button Click

I want to display a ballon tip when an error occures instead of showing MessageBox.
[NOTE] i did not want it to be shown on mouse Hover.
I tried both but they actually show the tip on mouse hover
toolTip1.SetToolTip();
toolTip1.Show();
You can use the ToolTip Popup event to check if there is a Tooltip present and cancel it if there isn't. You can then set the tooltip during your validation then show it. In this example I set a timer to reset the tooltip text after a 2 second timeout.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
toolTip1.IsBalloon = true;
toolTip1.Popup += new PopupEventHandler(toolTip1_Popup);
toolTip1.SetToolTip(textBox1, "");
}
void toolTip1_Popup(object sender, PopupEventArgs e)
{
if (toolTip1.GetToolTip(e.AssociatedControl) == "")
e.Cancel = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
toolTip1.RemoveAll();
}
private void textBox1_Validating(object sender, CancelEventArgs e)
{
int temp;
if (!int.TryParse(textBox1.Text, out temp))
showTip("Validation Error", (Control)sender);
}
private void showTip(string message, Control destination)
{
toolTip1.Show(message, destination);
timer1.Start();
}
}
Much to my surprise it appears that toolTip1.IsOpen = true will show a tooltip and allow it to stay open. Note that you will need to provide code to close it because it wouldn't go away on its own on my machine no matter what I did.

Categories