C# WPF Application Hiding button on click - c#

I have been asked to create a C# WPF application in VS 2013 and scenario is as under.
At the start of the application, a form must appear with two buttons on it. On the top button, the text “Button 1” must be written and on the bottom button the text “Button 2” must be written. For the first time, when any of the button 1 or button 2 is clicked then it will disappear and the other button will stay unchanged. For example, user clicks button 1 for the first time then button 1 will disappear. Button 2 will be unchanged. Now there is only one button on the window that is button 2. On clicking button 2 it will disappear and button 1 will reappear on the window. Now if the user clicks button 1 then it will disappear again and button 2 will reappear. This process may go on indefinitely until the application is terminated by clicking the close button.
I have created all the things but button1.Visible=true; cannot be entered VS is generating warnings that" sytem.windows.control.button does not contains a defination for Visible.
I need help please

In WPF we have Control.Visibility which needs to be set as Visibility.Visible or as required.
So you need to do it like this to hide:
button1.Visibility=Visibility.Collapsed

Under the Button Click Method you want this:
private void button1_Click(object sender, RoutedEventArgs e)
{
//Hides Current Button
button1.Visibility = Visibility.Collapsed;
//Checks and Shows Button 2
if (button2.Visibility == Visibility.Collapsed)
{
button2.Visibility = Visibility.Visible;
}
}
private void button2_Click(object sender, RoutedEventArgs e)
{
//Hides Current Button
button2.Visibility = Visibility.Collapsed;
//Checks and Shows Button 1
if (button1.Visibility == Visibility.Collapsed)
{
button1.Visibility = Visibility.Visible;
}
}
In order to obtain these methods you should declare a Click="" Method in XAML
<Button Name="button1" Click="button1_Click"><Button>
<Button Name="button2" Click="button2_Click"><Button>

Related

Cannot keep OnClick event after visibilty change

Just wondering if someone else can recreate this or is it just me :)
Drop a panel on the form
Put a button on the panel
Create click of button "I am Button"
Above works fine
Add onmouseleave to panel set button visible to false
Add onmouseenter to panel set button visible to true
Now if you move mouse in and out of panel the button will become visible false. Pefect.
Now move mouse back intp panel and button becomes visible. Perfect.
Clicking on button now does nothing (Lost on Click Event?)
Breakpoint in onclick never fires.
Is this a concept that cannot be done?
'Update' It wont let me post long comment.
First of all thanks for the quick responses.
I did not include code because this was in a very large project so i recreated it with a brand new app.
C# WinForm with one panel and one button on the panel.
I did not try to click on an invible button. Not only can i see the button but can see it respond to bing clicked.
#steve. Good point, The button is well inside of the panel but i see where you are going. I just performed a new test.
If i move the mouse in and out of the panel the button will show and hide perfectly.
After this, hitting enter on the button will excute onclick but the mouse will not. (I suppose this has something to do with only one button and it is default.)
What prompts the mouse to not fire the click event is beyond me.
Very Strange.
What i am trying to achieve:
An area on the screen that when the mouse enters this area a group of buttons appear so they can be used.
when the mouse leaves this area the buttons will dissapear. As my original post stated button works fine if visiblity does not change.
I suppose the your code has the following problem:
You receive the mouseenter event on the panel and you make the button
visible
You move the mouse over the button
This last action triggers the mouseleave event on the panel.
As a consequence your button becomes invisible.
But making the button invisible triggers a mouseenter over the panel and in a blink the button is again visible and causing a mouseleave on the panel.
An infinite loop between these two events starts.
So the click event is not lost, simply it will never trigger because the form engine is busy hiding and showing the button control.
I have reproduced the situation and fixed it adding an event handler for the MouseMove event. In this event I set a variable to block the infinite loop between the two events...
This is my example to adapt to your real code, try it in LinqPad
public class Form1 : Form
{
Button b1 = new Button();
Panel p1 = new Panel();
bool stillOnPanel = false;
public Form1()
{
b1.Text = "ClickMe";
b1.Click += onClick;
b1.Visible = false;
p1.MouseEnter += onEnter;
p1.MouseLeave += onLeave;
p1.MouseMove += onMove;
p1.BorderStyle = BorderStyle.FixedSingle;
p1.Size = new Size(150,100);
this.Controls.Add(p1);
p1.Controls.Add(b1);
}
void onClick(object sender, EventArgs e)
{
Console.WriteLine("Clicked");
}
void onMove(object sender, MouseEventArgs e)
{
//Check if we are still over the panel area
stillOnPanel = b1.ClientRectangle.Contains(p1.PointToClient(Cursor.Position));
}
void onEnter(object sender, EventArgs e)
{
if (!b1.Visible)
{
b1.Visible = true;
}
}
void onLeave(object sender, EventArgs e)
{
// Do not hide the button if we are over it, let it click....
if (b1.Visible && !stillOnPanel)
{
b1.Visible = false;
}
}
}

Prevent Windows firing events during long touch of a button

WinForms application.
User touches a button (touchscreen) that causes authentication and the screen (A) is changed to another screen (B). If user continues to hold the finger on the screen (just for a second) the screen B has a button that overlaps with the button on screen (A) and the button on screen B inadvertently gets a touch and invokes an action that is not supposed to happen...
How do I do to prevent this from happening?
That is weird behavior. I do not have a touch device to test this but in a regular scenario the click only triggers when the button is pressed and then released while the button is still in focus. If you press the button and do not release, the click event will not fire.
Anyhow perhaps what you can do is only show screen B when the button has been pressed and then it has lost focus. Like this:
private bool buttonIsClicked = false;
private void button1_Click(object sender, EventArgs e)
{
this.buttonIsClicked = true;
}
private void button1_Leave(object sender, EventArgs e)
{
if (this.buttonIsClicked)
{
this.buttonIsClicked = !this.buttonIsClicked;
// show screen B
}
}
Please use a variable like I have used buttonIsClicked so the code in Leave event handler is not executed every time the button loses focus even if it is not clicked.

Insert existing Buttons into Context Toolstrip - Winforms

I have a GUI and some buttons in WinForms. Now I want to create a Toolstrip Menu and move the buttons into the menu. Is it possible without copying the whole code into the new Toolstrip entries? I just want to link the buttons somehow.
Thanks
Yes and no. It is not possible to move the existing buttons into the ToolStrip but you can create new ones and rewire them:
first: look for the name of the current event handler by selecting the old button, press F4 and switch to the action menu (the lightning bolt in the properties window):
second: select the new button and pick the same event handler as for the old button:
finally you can remove the old button.
Buttons click events code can be linked directly to toolstrip buttons.
All availables events can be showed in Action->Click in design time.
EDIT:
I assume that you have a form in your project with some buttons, eg.: button1, button2.
For each Button you wrote a click action, in your code something like:
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON1
}
private void button1_Click(object sender, EventArgs e)
{
// YOUR CODE FOR BUTTON2
}
Then you can use those methods/action for toolbar's buttons.
The easiest way to do is to add buttons to tollbar and use Events (with property panel, the little bolt icon) to choose the correct method to assign to each tolbar's button Click event/action.

Set panel visiblity on Escape key press

Lets say I have a windows form and it has two panels. Main panel and popup panel.When specific button click main panel will disable and popup panel will be visible.
My question is when user press escape key i want to set visibility of popup panel to false and enable main panel.
bool bPanelFocus;
private void cancelButon_Click(object sender, EventArgs e)
{
if (popuppanel.Visible == true && bPanelFocus)
{
popuppanel.Visible = false;
mainpanel.Visible = true;
return;
}
//your code for the cancel button
}
Since you have a cancel button on the form, it will trigger the click event on that button when you press the Esc button. On your cancel button's click event, add a validation to check if the pop up panel is visible, also you might need a flag to check if the user has focus on the panel otherwise proceed with the cancel button's procedures.

Message box causes loss of focus

I have created a tool bar which has three controls. First one being a text box , an OK button and a Clear button. Essentially I am using this toolbar to search some text. When there are no results found , I pop up a message box informing the user that no results were found. But when the user clicks "OK" button of the message box, the text box looses focus and the focus passes to the next control which is the "OK" button. What should I do to avoid the text box to loose focus. I am using c#.
You can't. Clicking on the Ok button forces it to have control (and therefore the textbox loses control).
You can, however, do this on your click event:
MessageBox.Show("asdf");
textBox1.Focus();
EDIT
In response to your comment, I don't think that there's an easy way to return focus to the last control once another control has received focus, and the search and clear buttons will have to receive focus when clicked. You can do this:
private Control _last;
private void textBox1_Leave(object sender, EventArgs e)
{
_last = (Control) sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("asdf");
_last.Focus();
}
It's not very clear your question, but you can make your control take the focus like this:
textBox1.Focus();

Categories