Why does setting focus in the OnNavigatedTo() event not set focus? - c#

I've got this code in a pages OnNavigatedTo() event:
if (string.IsNullOrWhiteSpace(textBoxGroupName.Text))
{
textBoxGroupName.Focus(FocusState.Programmatic);
}
...but textBoxGroupName does not have focus when the page displays. Why not?

OnNavigatedTo happens to early in the page lifetime for setting the focus to work. You should call your code in the Loaded event:
private void MainPage_OnLoaded(object sender, RoutedEventArgs e)
{
if (string.IsNullOrWhiteSpace(textBoxGroupName.Text))
{
textBoxGroupName.Focus(FocusState.Programmatic);
}
}
Of course you need to setup the handler in your .xaml file (I've omitted the other attributes in the Page element:
<Page
Loaded="MainPage_OnLoaded">

Only controls that are contained within the GroupBox control can be selected or receive focus. Seems like you are not using GroupBox correctly.
From MSDN
The complete GroupBox itself cannot be selected or receive focus. For more information about how this control responds to the Focus and Select methods, see the following Control members: CanFocus, CanSelect, Focused, ContainsFocus, Focus, Select.
Hint:
You maybe want to use the Controls property to access the child controls:
if (string.IsNullOrWhiteSpace(textBoxGroupName.Text))
{
var child_TextBox = textBoxGroupName.Controls["myTextBox"]
if(child_TextBox.CanFocus)
child_TextBox.Focus(FocusState.Programmatic);
}

Related

Showing/Hiding a control from within another control

I have got a Windows forms application with a main application form and two controls I added to my main form.
The first control has got a login button. When that button is pressed, I want to hide control1 and show control2, which is currently hidden.
Hiding control1 works by using this.Hide() in the onClick event of the login button, but I have so far been unable to find a way to reference control2 and call .Show()
How can I show control2 from within the onClick event of the login button?
EDIT:
Hope the below information helps.
Controls:
LoginMenu.cs - Has all of the control's code in it. - Control name in MainForm.cs is loginMenu1
TicketSearch.cs - Has all of the control's code in it. - control name in MainForm.cs is ticketSearch1
After adding all controls, I rebuilt the project and dragged them into the main form from the tools menu.
The code for the login button is in LoginMenu.cs
public partial class LoginMenu : UserControl
{
public LoginMenu()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent();
*CODE HERE*
}
void ButtonLoginClick(object sender, EventArgs e)
{
*CODE HERE*
try
{
HttpWebResponse response =
(HttpWebResponse)loginTest.GetResponse();
}
catch (System.Exception ex)
{
if (ex.Message.Contains("401") == true)
{
*CODE HERE*
}
else if (ex.Message.Contains("403") == true)
{
*CODE HERE*
//Hide login control. This works
this.Hide();
//This is where I want to show the control ticketSearch1
}
}
}
}
Thanks to those who tried to help. I learnt something new today.
For some reason, I don't quite understand I could not access the controls in MainForm.cs from LoginMenu.cs even though they are in the same namespace and the control 'Modifiers' property is set to 'Public'.
I resolved this by adding the following method call to my login button onClick action to reference the control I wanted Form.ActiveForm.Controls.Find("ticketSearch1", true)[0].Show();
But it should work.
Look at the properties of Control2 and pick the name (or set a name) under the line "(Name)".
then you should:
private void button_Click(object sender, EventArgs e)
{
firstcontrol.Hide();
secondcontrol.Show();
}
it should work.
Maybe you failed with the placing of the control. So look what happens if you start your programm with the first control hidden and the second one shown. And try to hide the second control and show the first.
There must be the problem.
Did you place your Control via "Toolbox"?
Or do you call your Control via code?
EDIT:
Referring to your inserted Code, it seems, that you call this code in your Usercontrol.
So you need to set the second Usercontrol to "Public". When you click on it and go to the Properties, you choose "Mofifiers" and set it to public.
Now you should look for the (Name) Attribute in his properties.
Then just add:
NameOfYourSecondControl.Show();
This will fix your problem.

Enter event not firing in Panel inside a UserControl

I added a Panel inside a UserControl i design time.
Then, I added this control to a form.
I want to show a focus dashed border when the control has the focus.
Unfortunately, the Enter event from the panel never fires. I only get a fire when I click on the user control itself.
To extend this question. How can I forward events from controls inside a user control to the base user control? A comment from Hans Passant in this question says that by default events are forwarded to their direct parent. I didn't change any of the control's properties. What am I doing wrong? Is there an obvious property I need to change on each control i order to force it to forward unhandled events?
I am using DevExpress controls but this behavior is same in windows WinForms controls.
edit: I understand that panel might not be able to get focus. If this is true, how do I forward each mouse event to the parent control?
Based on your comment, from inside your UserControl, handle the panel's MouseDown event and set the focus to the parent control:
public UserControl1() {
InitializeComponent();
panel1.MouseDown += new MouseEventHandler(panel1_MouseDown);
}
void panel1_MouseDown(object sender, MouseEventArgs e) {
if (!this.Focused)
this.Focus();
}
Use panel1.Select() on MouseClick event of the panel and you will be able to trigger panel1.Enter and panel1.Leave

When I add an custom UserControl to a Panel dynamically, the usercontrol loses all event handling

I've got aspx page which dynamically loads UserControl into a Panel object based on the input on a set of radio buttons. The UserControl successfully adds and displays properly to the Panel on postback and calls the Page_Load() event just fine in the UC but when I interact with the form in any way that would trigger an event, the event is not captured on the postback.
I've tried to add the event handling association in the Page_Load() which I know gets called as well as adding the association in the ASP.NET tag without any difference in result.
This is how I am adding the control (object names have been simplified):
private UserControl _control;
protected void RadioButtonGroup_CheckedChanged(object sender, EventArgs e)
{
RadioButton radioButton = (RadioButton)sender;
if (radioButton == RadioButton1Ctl)
{
_control = (UserControl1)LoadControl("~/Controls/UserControl1.ascx");
PanelCtl.Controls.Add(_control);
}
else if (radioButton == RadioButton2Ctl)
{
_control = (UserControl2)LoadControl("~/Controls/UserControl2.ascx");
PanelCtl.Controls.Add(_control);
}
}
As I said, the control gets successfully added by when I click any buttons or have any postback events which should be bound on the UC, the control gets removed from the page and events aren't fired.
Any help would be greatly appreciated.
This is happening because the controls are being added dynamically. I would suggest using the DynamicControlsPlaceHolder instead of a Panel. It will persist your controls for you when the page is posted back.
DynamicControlsPlaceHolder:
http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
The other alternative is to recreate the controls at every postback, before the ViewState is reloaded. I would suggest using OnInit.
The DynamicControlsPlaceHolder takes all of the hard work away, so that might be the best option for you.

Adding controls to tab page runtime

I am using C# and winForms, I have a few tabcontrols, which contains a few tab pages,
I would like to add all tab pages my user control, it would be best, If I could add it after user click on tab page. It works for me only, when I add this controls in constructor - what makes delay at application start - about 3 seconds, what is very much.
I would like to add this controls at run time, like
tabPage1.onClickEvent()
{
tabPage1.Controls.Add(myUserControl);
}
but it didnt works, I also tryied to use Invalidate, or Refresh method but it not works.
So is there any possibility to add this userControls in other method than constructor?
Maybe its problem, that I have TabControl inside TabControl and I have to add this controls throught parent TabControl?
Thanks!
Try double clicking the tag page in the designer, or you can add the event handler manually like this:
tabPage1.Click += new EventHandler(tagPage1_Click);
I don't know whether tabPage1 is dynamic, but if it's not, you should add the above event handler to your designer.cs file.
Here is the event handler in the code:
protected void tabPage1_Click(object sender, EventArgs e)
{
tagPage1.Controls.Add(new TextBox());
}

How can I detect when a control is no longer visible?

In my current app I have a Tree control on a page of a TabControl which is inside a panel of a SplitContainer control. The tree control can thus be hidden by either hiding the SplitContainer panel, or switching to another TabPage in the TabControl.
In the Form's menus there are commands which act on the currently selected Node in the tree. I do not want these options to be enabled when the user can not see what is selected.
Is there a simple way of determining when the TreeView goes out of view with out subscribing to the events of both the TabControl and the SplitContainer separately?
You can create a boolean member variable. In the tabchanged event, test to see if the treeview tab is selected and set the variable appropriately. Also, subscribe to the event that is fired when the splitter view size is changed. Test the width or height of the splitter to see if your treeview is hidden. If it is, set the variable here to. Then all you need to do is test your new member variable.
Test the TreeView's Visible property. There is also a VisibleChanged event.
if(!myControl.Visible)
{
// Control is not visible.
}
or
if(myControl.Visible == false)
{
// Control is not visible.
}
Or, probably the better option would be to add a handler to the VisibleChanged event, in the code (or using the Events tab in Designer view):
void myControl_VisibleChanged(object sender, EventArgs e)
{
TreeView tView = sender as TreeView ;
if (tView.Visible)
{
// Do something.
}
else
{
// Do something.
}
}

Categories

Resources