Hi currently i have 2 buttons, Update and Modify. Update button is set to be hidden initially.
When i click Modify button, Modify button hides, Update button appears, Textbox becomes non read only. Then Clicking update button will hide update button and modify buttons appear and textbox will be hidden and label will appear.
How can i change the code so that:
When i first click modify button, and i can get to update the textbox values and in this state if i press "ESC" button, i will hide "update" button and textbox will be read only?
The following is the current code:
private void ProjectnumberupdateButton_Click(object sender, RoutedEventArgs e)
{
ProjectnumberresultLabel.Content = ProjectnumberTextBox.Text;
ProjectnumberupdateButton.Visibility = Visibility.Hidden;
ProjectnumberTextBox.Visibility = Visibility.Hidden;
ProjectnumbermodifyButton.Visibility = Visibility.Visible;
PreviousbuildversionresultLabel.Content = "" + MajorversionresultLabel.Content + "." + MinorversionresultLabel.Content + "." + ProjectnumberresultLabel.Content + "." + BuildnumberresultLabel.Content;
}
private void ProjectnumbermodifyButton_Click(object sender, RoutedEventArgs e)
{
ProjectnumberupdateButton.Visibility = Visibility.Visible;
ProjectnumberTextBox.Visibility = Visibility.Visible;
ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
}
EDIT:
This is what i have done so far:
private void MajorversionTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Escape)
{
MajorversionupdateButton.Visibility = Visibility.Hidden;
MajorversionTextBox.Visibility = Visibility.Hidden;
MajorversionmodifyButton.Visibility = Visibility.Visible;
}
}
private void MajorversionmodifyButton_Click(object sender, RoutedEventArgs e)
{
MajorversionupdateButton.Visibility = Visibility.Visible;
MajorversionTextBox.Visibility = Visibility.Visible;
MajorversionmodifyButton.Visibility = Visibility.Hidden;
Keyboard.Focus(MajorversionTextBox);
MajorversionTextBox_KeyDown(); // this is the line. i have trouble hooking this up
}
sorry, i changed the project number to majorversion.
You can write OnKeyPress Event for the window and trace the ESC button click.
Inside that you can write the logic to toggle the visibility of the controls.
You can hook / handle the KeyDown event, check if the key pressed was the Escape button and make your changes to you buttons and text boxes in the code from there.
You can set focus on text box when modify button clicked and then use KeyDown event on text box:
private void ProjectnumberTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape)
{
ProjectnumberTextBox.ReadOnly = true;
ProjectnumbermodifyButton.Visibility = Visibility.Hidden;
}
}
Related
I want when I move button2 into button1 (img2) below code will done.
My code:
private void ButtonDragDrop(object sender, DragEventArgs e)
{
var btn = (Button)e.Data.GetData(typeof(Button)); //button2
(sender as Button).Text = "test1"; //button1
btn.Text = "Test2"; //button2
}
How Can I make when button1 touch button2 code will done?
I would like to hide a button after it was clicked but I don't know how.
C#
private async void btn1Clicked(object sender, EventArgs e)
{
if (condition)
{
// hide button
}
}
XAML
<Button Clicked="btn1Clicked"/>
If you want to make your button invisible you can do:
if (condition)
{
button.Visibility=ViewStates.Invisible;
}
else
{
button.Visibility=ViewStates.Visible;
}
Or you can use IsVisible property.
You can do the following
private async void btn1Clicked(object sender, EventArgs e)
{
Button btn = (Button)sender;
if(condition)
{
// hide the button
btn.IsVisible = false;
// or disable the button.
// As Caius Jard said, disappearing buttons sometimes cause confusion.
btn.IsEnabled = false;
}
}
i have tried 2 solutions but neither of them work, the first on is to directly send it into the listbox but neither of them work
1.To send it directly into the listbox
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
lbItems = b.Text;
}
2.The other method i tried was to send it into a a texbox first then send the the string from the texbox into the listbox, but the code breks after one button
private void button_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
lbItems = b.Text;
if (txtPre.Text == " ")
{
}
else
{
lbItems.Items.Add(txtPre.Text);
txtPre.Clear();
}
}
lbItems.Items.Add((sender as Button).Text);
I'm having this code:
private void b9_Click(object sender, EventArgs e)
{
b9.Enabled = false;
color = 8;
}
The problem is that i'm having a lot of buttons for disabling. Is there a chance i can use something like:
this.Enabled=false;
Probably that is what you want
private void OnClick(object sender, EventArgs e)
{
if( sender is Button )
{
Button button = (Button)sender;
button.Enabled = false;
}
}
Use this routine for every button you need to disable on click.
It is known as single event handler for multiple controls. Just put following event handler for your buttons as many as you like.
public void Button_Click(object sender, EventArgs e)
{
Button button = (Button)sender;
button.IsEnable = false;
// If you want to access text in the button
... = button.Content as object;
}
private void OnClick(object sender, EventArgs e)
{
Button btn = sender as Button; // if sender is not a Button, btn will be null
if (btn != null)
{
btn.Enabled = false;
}
}
If you want to apply the same behaviour to any clickable control, you can use Control class instead of Button. Button inherits from Control and the property Enabled is defined in Control class.
private void OnClick(object sender, EventArgs e)
{
Control ctrl = sender as Control; // if sender is not a Control, ctrl will be null
if (ctrl != null)
{
ctrl .Enabled = false;
}
}
Also, if you want to go one step further, you can create a method that disables the clicked control. Something like this:
private void DisableControl(object sender)
{
Control ctrl = sender as Control;
if (ctrl != null)
{
ctrl.Enabled = false;
}
}
Then, you can call this method from the Click even handler like this:
private void OnClick(object sender, EventArgs e)
{
DisableControl(sender);
}
In my XAML, there's one button and one label. At launch, the button is visible and the label is not. When clicking the install_btn, the label should becomes visible instead of the button.
Here's a part of the code:
private void install_btn_Click(object sender, RoutedEventArgs e)
{
inst_label.Visibility = Visibility.Visible;
progress.Visibility = Visibility.Visible;
install_btn.Visibility = Visibility.Hidden;
}
And then there's some code like webClient.DownloadFile(). But the visibility toggles only after processing the webClient. How can I perform toggling before downloading files (or something else)?
You should allow the application to update it's UI:
private void install_btn_Click(object sender, RoutedEventArgs e)
{
inst_label.Visibility = Visibility.Visible;
progress.Visibility = Visibility.Visible;
install_btn.Visibility = Visibility.Hidden;
Application.Current.Dispatcher.Invoke(DispatcherPriority.Background, new Action(delegate { }));
webClient.DownloadFile();
}