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();
}
Related
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;
}
}
So i have this block of code and i have a button called AddNewButton which adds a StackPanel into a already created StackPanel called MainStackPanel which is irrelevant but the "GroupPanel" has child controls such as "GroupName", "GroupTextBox" and "GroupEdit".
Now the "GroupEdit" button has a click event that runs the void named "GroupEdit_Click" and in that void i use Button GroupEdit1 = sender as Button; Now this works and makes me able to access the buttons properties and change content but my problem is: How do i access the other controls such as "GroupPanel", "GroupName" and "GroupTextBox". I will use the AddNewButton a few times so when i access the separate controls they need to be accessed seperately
I tried to get rid of as much unnecessary code.
private void AddNewButton_Click(object sender, RoutedEventArgs e)
{
StackPanel GroupPanel = new StackPanel();
TextBlock GroupName = new TextBlock();
GroupName.Text = "Group ";
TextBox GroupTextBox = new TextBox();
GroupTextBox.Visibility = Visibility.Collapsed;
Button GroupEdit = new Button();
GroupEdit.Content = "Edit Group";
GroupEdit.Click += new RoutedEventHandler(GroupEdit_Click);
GroupPanel.Children.Add(GroupName);
GroupPanel.Children.Add(GroupTextBox);
GroupPanel.Children.Add(GroupEdit);
}
private void GroupEdit_Click(object sender,RoutedEventArgs e)
{
Button GroupEdit1 = sender as Button;
GroupEdit1.Content = "Done";
//Now how do i access these controls?
GroupName.Visibility = Visibility.Collapsed;
GroupTextBox.Visibility = Visibility.Visible;
}
}
You could maintain a private List of your dynamically added GroupEdit controls and assign them numbered tags.
private List<TextBox> dynamicGroupEdits = new List<TextBox>();
private void AddNewButton_Click(object sender, RoutedEventArgs e)
{
...
dynamicGroupEdits.Add(GroupEdit);
GroupEdit.Tag = dynamicGroupEdits.Count;
GroupPanel.Tag = GroupEdit.Tag;
GroupTextBox.Tag = GroupEdit.Tag;
...
}
private void GroupEdit_Click(object sender,RoutedEventArgs e)
{
...
tag = GroupEdit1.Tag;
// Loop through all child controls and set visibility according to tag
for each (var c in LogicalTreeHelper.GetChildren(GroupEdit1.Parent)
{
if(c is TextBox && c.Tag == tag)
c.Visible =Visibility.Visible;
else if(c is TextBlock && c.Tag == tag)
c.Visibility = Visibility.Collapsed;
}
}
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);
}
I cannot figure out how to catch the Navigating event on the WebBrowser control. Basically I'm trying to figure out how trigger the progress bar to show when a user clicks a link on a page.
Here is the code I use to show the progress bar and then hide it on page loaded. Can someone help me out with the event handler for Navigating?
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
progressBar.IsIndeterminate = true;
progressBar.Visibility = Visibility.Visible;
webBrowser.Navigate(new Uri(MY_URL, UriKind.Absolute));
webBrowser.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(PageLoadCompleted);
webBrowser.Navigating = ?
}
private void PageLoadCompleted(object sender, System.Windows.Navigation.NavigationEventArgs e)
{
progressBar.IsIndeterminate = false;
progressBar.Visibility = Visibility.Collapsed;
}
The documentation you seek is here. You can write
webBrowser.Navigating += webBrowser_Navigating;
// ...
void webBrowser_Navigating( object sender, NavigatingEventArgs e )
{
// ...
}
The answer of by VisualStuart helped me solve my problem.
My now working code is as below :
private void MyButton1_Click(object sender, RoutedEventArgs e)
{
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
string site = MyTextBox1.Text;
webBrowser1.Navigate(new Uri(site, UriKind.Absolute));
webBrowser1.Navigating += webBrowser1_Navigating;
webBrowser1.LoadCompleted += new System.Windows.Navigation.LoadCompletedEventHandler(webBrowser1_LoadCompleted);
}
private void webBrowser1_Navigating(object sender, NavigatingEventArgs e)
{
MyTextBox1.Text = e.Uri.ToString();
MyprogressBar.IsIndeterminate = true;
MyprogressBar.Visibility = Visibility.Visible;
}
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;
}
}