I'm working on c# WinForm.
I have an custom UserControl : MyControl : UserControl, INotifyPropertyChanged. I attached a method on event on event VisibleChanged : this.VisibleChanged += new System.EventHandler(this.MyControl_VisibleChanged);
My application have some pages, each page is a control like MyControl. Top of MainWindows contains Button, used to switch tab.
My problem is that my function MyControl_VisibleChanged is called only when Visible is changing to true. I added a test in a tab to check MyControl.Visible, when I select the other tab, MyControl.Visible is false but no event is raised.
I've try to define a new property Visible for this control but value is never set, only the base value is modify.
Can you help me to find a solution ?
This is a quirk in the way Visible works, explained here. His solution was to use properties that he has complete control over, but you could instead have a method allowing the tab switches to tell their children to raise their VisibleChanged event that extra time.
The first two answers to this question may also be useful.
Related
When I build a vb.net winforms application in VS2010, I can double click on a control on my form and it takes me to the code window. Then there are dropdown lists at the top left and top right of the code window that allow me to select the control and the event that I want to handle...
If the event handler already is already in use/defined/coded, it takes me there, otherwise it defines a new handler method with a standard naming convention (_click(blah blah blah))...
But for some reason when I build a c# application, there is no such "cheat"... call me lazy, but I don't like having to type code that I don't need to, and since the functionality exists in VS to "just do it", why can't I do it in a C# app...???
Maybe there is just a setting I need to find... anyone know of such a setting..???
Thanks
In VS2010's C# mode, similar functionality is enabled by default. Double-clicking on a control in the visual form designer will create a default event handler for that form in the code window. This may not always be the event that you would like to handle, however. If that is the case, right-click the control in the form designer and select Properties from the context menu. A property grid will appear that contains a list of all of the control's properties and events. Click on the lightning-bolt icon at the top of this grid to select the Events tab. From here, double-clicking on any of the event rows will automatically create a new event handler for the control in the code window.
I need to hook up an event that shows a WPF Popup control when a TextBox has focus and a keyboard shortcut is clicked. For instance. When typing in the TextBox field, the user can press ALT+H for help, to get a popup dialog showing input help. Pressing ALT+H "outside" the TextBox should not open the popup.
Any ideas?
Looks like a job for an Attached Event.
From MSDN:
The concept of
an attached event enables you to add a handler for a particular event
to an arbitrary element rather than to an element that actually
defines or inherits the event. In this case, neither the object
potentially raising the event nor the destination handling instance
defines or otherwise "owns" the event.
You can find details here, on the MSDN
Use command binding:
ApplicationCommands.Help.InputGestures.Add(new KeyGesture(Key.H, ModifierKeys.Alt));
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, Help_Executed, Help_Enabled));
In function Help_Executed do some operations
In function Help_Enabled check if textbox selected, do e.CanExecute = true;
InputGestures assign ALT-H for help
This question is as simple as it sounds. Assume button.IsEnabled == false, when I execute button.IsEnabled = true; the button remains false!! (By using the Visual Studio 'watch' feature to immediately look at the value after setting it in the debugger)
Obviously, this is not a common occurrence as generally that code works. But there is obviously something in the system that is blocking the setting and I am looking for ideas about what it could be. At first I thought it was because the button was hooked into an ICommand, which obviously controls the IsEnabled setting itself. So I eliminated the ICommand. That worked for a different button but this button is not hooked to an ICommand via "Command={Binding Path...}". In fact this button is created in C# as simply:
Button button = new Button();
button.Content = "Save Record";
button.IsTabStop = false;
These buttons exist in a Custom Control toolbar and the code which is trying to set their value occurs in a PropertyChanged eventhandler I have written. Curiously the same code initially successfully sets the button to false and that works! What could possibly be prohibiting setting IsEnabled=true?
A few hours later:
Bah, rookie mistake (after 35 years in IT). Further on down in the code was the line:
button.Command = new myCommand(...);
So, in fact, the button was hooked up via a Command Interface. I took that out and replaced it with a button click eventhandler and the problem is solved. Since I could not find this issue in Google, after numerous searches, let me restate the problem in case others encounter it: when you hook up an ICommand to a UIElement, e.g. button, menuitem, etc. .Net takes over the IsEnabled property. You can no longer set the property programmatically.
I know you answered this, but it looks like you just ripped out the new-style code and went back to the old way. So on the off chance that you'd like to know how the new way works, read on.
Commands are a refinement of the Event system! Commands replace the IsEnabled property and the Click event, and let you define useful shortcuts to the command.
Once you hook something up to a Command, you should no longer use IsEnabled or Click for that something.
The commanding equivalent to "IsEnabled = True" is to return True from the CanExecute event handler of the Command.
Then, define an Executed handler for the Command, and this replaces the Click event.
Why is this good? Simple: You can hook up a toolbar button, a menu item, a context menu item, and perhaps some other interface parts to the Command, and you don't have to keep track of (or define) each of those elements' Properties or events. Think of a text editor application. The traditional way would be to hook up an event to Edit > Copy, the Copy toolbar button, and a context menu item; handle the TextBox's SelectionChanged event to enable/disable all of these as appropriate; and code up the Click event for each of those. The new way is: hook the command up to Edit > Copy, the Copy toolbar button, and the context menu item, define the CanExecute event to return true/false depending on the TextBox's selection property, and code the Executed handler. Looks similar? Yes, but you declare the Command connections in markup instead of code, and you don't have to write nearly as much code. It adds up to a lot of savings when you have a lot of Commands.
My question is somewhat related to this thread: How to avoid Initialization of web user control inside aspx?
The UserControl should only be visible when a button is clicked.
So my usercontrol is set like this:
<example:usercontrol ID="idControl" runat="server" Visible="false" />
<asp:Button ID="showMyControl" runat="server" OnClick="ShowMyControl" /
And the usercontrol itself checks if he's visible:
protected void Page_Load( object sender, EventArgs e ) {
// only execute if the user control is visible
if ( this.Visible ) {
...
}
}
When I click the button I'm setting the property Visible of the usercontrol to true but when the postback occurs the Visible-property in the Page_Load of the usercontrol is still set to false.
My UserControl is probably loaded first and the Visible-property is set afterwards?
Is there an easy way to resolve this?
Thanks in advance!
The Load event of your control, which is being handled by your Page_Load event handler method if I understand correctly, gets fired before the Click event of your button control. Therefore, when the Page_Load method checks this.Visible, the property has not yet been changed because the Click event handler has not yet executed.
For this reason, I think that checking the Visible property of your control would be more appropriate in the PreRender event instead of the Load event.
I'm guessing that you're doing some sort of data retrieval or something that you wish to avoid if the control is not visible. Unfortunately, this sort of issue regarding the page life-cycle and the order in which events fire is sort of a common issue to deal with in ASP.Net programming.
If all of your initialization code can easily be moved into the PreRender event, then great. Problem solved (hopefully). If not (i.e. you need something to happen before the PreRender), you may need to come up with some other mechanism to ensure that your code executes at the right time. For example, you could expose a "SetVisible" method on your control which sets the Visible property to true and then also executes whatever initialization logic is needed. The downside of this is that you can't really guarantee that some code won't just set your control's Visible property to true outside of the SetVisible method that you provide.
Another idea is to actually override the Visible property and perform your initialization logic whenever that property gets set to true.
Put the code which you call in the Control's Page_Load event into the PostBack event. Make sure the button has autopostback set to true.
What you are looking for is the Control.EnsureChildControls method. This method exists for this very situation. It will ensure that all child controls have been created. Then you can set your visible property.
Here's the steps to reproduce the problem :
Create a c# project with a form with 1 control, let's say a textbox.
Using the properties window (lightning bolt thingy), add a "click" event.
Write some code in that method.
Using the properties window, remove the content of the "click" event cell.
I would like to know why is the code from step 3 has disapear (but not the method signature).
Normal behavior from the IDE ? Option in the "tools-options" menu ?
The method you created should remain even after it has been unhooked from the control's event. The only time VisualStudio will automatically remove the method is if the method is empty. Once you add custom code, the method should not be removed.