Control keys not working while window is disable and again Enable - c#

I have a XAML MainWindow and Child User control loaded on that window. On button click event in user control I disable mainWindow as Well as User Control till a work on button click is not finished. When I again updating the property from User control to enable the window again, now user control keys are not working, because while debugging its showing IsEnabled Property of window/user control is still disable. I am also forcefully updating user control property this.IsEnabled = true; but still its not updating IsEnabled property of class true.
How could I update the mainwindow and user control property to enable or how could my Control keys can work again.

If you set mainWindow.IsEnabled to false, you cannot set userControl.IsEnabled to true. You have to set the mainWindow.IsEnabled to true first. You cannot have an enabled control inside a disabled control.
I wrote a simple WPF app with a user control with a button. The main window has the user control and the code for the user control is:
public partial class MyUserControl : UserControl
{
DispatcherTimer m_timer = new DispatcherTimer();
public MyUserControl()
{
InitializeComponent();
m_timer.Tick += Timer_Tick;
m_timer.Interval = TimeSpan.FromSeconds(4);
}
private void Timer_Tick(object sender, EventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = true;
m_timer.Stop();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
Window window = Window.GetWindow(this);
window.IsEnabled = false;
m_timer.Start();
}
}

Related

C# Make item show

I am trying to hide a progress bar until a button is clicked. Once the button is clicked I would like the progress bar to show. How would I do that? I am new to C#.
1- In the form view, on your progress bar properties, make sure that the "Visible" property is set to false.
2- Create a click event for the button (you can double click it in the form view, it will generate the event).
3- In the click event function, set your progress bar visibility to true. (YourProgressBarID.Visible = true) and you might want to hide that button, you could use YourButtonID.Visible = false
During the initialization of your form, set the visibility of your ProgressBar to false or set it in from the Designer Properties.
public Form1()
{
InitializeComponent();
progressBar1.Visible = false;
}
Now, on your button click event just make the visibility of your ProgressBar to true.
private void button1_Click(object sender, EventArgs e)
{
progressBar1.Visible = true;
}

Click event not firing for custom UserControl on a form

I have created a UserControl called Toggle, this is my code for it
[DefaultEvent("Click")]
public partial class Toggle : UserControl {
public bool ToggleStatus { get { return toggleStatus; } }
private bool toggleStatus { get; set; }
public Toggle() {
InitializeComponent();
toggleStatus = true;
}
private void toggleClick(object sender, EventArgs e) {
if (toggleStatus) { // currently set as "true" or "on"
this.lblSwitch.Dock = DockStyle.Right;
this.pnlBackground.BackColor = System.Drawing.Color.Red;
toggleStatus = false;
} else { // currently set as "false" or "off"
this.lblSwitch.Dock = DockStyle.Left;
this.pnlBackground.BackColor = System.Drawing.Color.Green;
toggleStatus = true;
}
}
}
The toggleClick method is tied to the click event of controls within the UserControl; this fires off just fine.
However, when I put my Toggle control on a form and attempt to tie an event to the click of it, it won't fire off.
private void toggleSoundClick(object sender, EventArgs e) {
soundToggle = !soundToggle;
}
I've made sure that the proper method is tied to the click event in my Designer.cs file of both my UserControl and my form
UserControl:
this.lblSwitch.Click += new System.EventHandler(this.toggleClick);
this.pnlBackground.Click += new System.EventHandler(this.toggleClick);
(I have it tied to two controls on my Toggle since I want it to fire no matter where you click on the control)
Form:
this.tglSound.Click += new System.EventHandler(this.toggleSoundClick);
The expected behavior for the UserControl is to fire off toggleClick (which it does) then the form should fire off toggleSoundClick (which it doesn't). I have seen this behavior work fine for other UserControls I have designed and used in this same project.
To clarify:
I have a UserControl called ServerDisplay. I have a method tied to the click event of the background panel of ServerDisplay (in the code for ServerDisplay) that shows a random MessageBox:
private void ServerDisplay_Click(object sender, EventArgs e) {
MessageBox.Show("test");
}
Then, I have a ServerDisplay control contained within my form. I have a method tied to the click event of it as well (in the code for my form)
private void serverDisplayClick(object sender, EventArgs e) {
if (loaded) {
ServerDisplay display = (ServerDisplay)sender;
this.lblLastServer.Text = "Last server joined was " + display.Server.Name + " at " + DateTime.Now.ToString("h:mm tt");
centerControl(this.lblLastServer);
}
}
When I click on the ServerDisplay control in my form, it shows the MessageBox (code from within ServerDisplay), then updates the label I specified in the code (code from form). This is the intended behavior, but it is not working for my other UserControl.
I finally figured it out! The way I had the control set up, I had the control itself, a panel filling up the entire background (I used this for the color), and then another panel inside the first panel to act as the "switch".
When I got rid of the first panel and just used the background of the control for the color and a small panel for the switch, it works when I click the background, but not when I click the "switch" panel. I guess this opens up more questions that I'll have to ask separately from this one, but at least I got my answer.

How can I Display UserControl into a form

I have created a UserControl and I want when I click on some button to display this UserControl into my form.
Is there any way to do that ?
You can dynamically add the user control to the forms Controls collection.
For example, in the button click event handler:
MyUserControl uc = new MyUserControl();
uc.Dock = DockStyle.Fill;
this.Controls.Add(uc);
Put it on the form. Start it out invisible, when the button is clicked set it to visible.
Drag your user control to your form and change it's Visible property to false
UserControl1.Visible = false;
Then on your button set the Visibility of your usercontrol to true.
private void button1_Click(object sender, EventArgs e)
{
UserControl1.Visible = true;
}
Is it a typical ascx type of control? If yes then you can set the "visible" property of the control through the button click event.
Let say you have :
<uc1:ft ID="userctrl" runat="server" Visible="false" />
then on your button event:
protected void Button1_Click(object sender, EventArgs e)
{
userctrl.Visible = true;
}

Changing theme calls UserControl_Loaded event

I am not getting why behavior of WPF user control and Windows forms user control is different . I added window loaded event which just shows message box as :
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("Main Window Loaded","WPF");
}
Also I created one user control and added loaded event as :
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("User Control Loaded.","WPF");
}
I have put this user control in main window.
When i launch this , I get both message box, User controls as well as windows.
Now , When i change my theme from Aero to any High contrast then user control's message box is shown again.
Why this is happening ? Why this is different from Windows form ? What should I do to avoid showing it multiple times ?
Wajeed
You could have a boolean field which stores the state of whether the dialog was shown yet or not. If you change the theme the UI-elements will reload so naturally the event will fire again.
if (!_diagWasShown)
{
_diagWasShown = true;
//Show dialogue
}
you can create bool variable, which will indicate if MessageBox was shown.
bool isUserMessageBoxShown = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
if (!isUserMessageBoxShown)
{
MessageBox.Show("User Control Loaded.","WPF");
isUserMessageBoxShown = true;
}
}

WPF event when a window is no longer on top

I have a WPF window (window1) whose owner is window2. If a user clicks on window2, or the desktop, or anything else to make window1 not on top of the z-order, I want to set window1's visibility to hidden. I.e., the window either needs to be on top, or hidden. Is this possible?
Yes.
public Window1()
{
InitializeComponent();
this.Deactivated += new EventHandler(Window1_Deactivated);
}
void Window1_Deactivated(object sender, EventArgs e)
{
Visibility = Visibility.Collapsed;
}
Note that this will also remove it from the TaskBar.

Categories

Resources