Winforms ListView ItemCheck on load - c#

I have a winform containing tabs, containing a usercontrol, containing a listview with checkboxes.
private void lvwRoles_ItemCheck(object sender, System.Windows.Forms.ItemCheckEventArgs e)
{
if (!m_loading && m_locked)
{
e.NewValue = e.CurrentValue;
return;
}
The listview is assigned it's items (some is checked) in a method (in the user control) that is called from the parent form. This is done on load of the parent form.
My problem is that the ItemCheck occurs when I click the corresponding tab the first time.
That results in the m_loading state variable being false since long ago.Thus no item is ever checked when the usercontrol is m_locked.
Is there a way to solve this without changing how the listview is populated?

The listview is assigned it's items (some is checked) in a method (in
the user control) that is called from the parent form. This is done on
load of the parent form.
Even though you call that method in the parent form_load that effectively fires when you select that tab. Set m_loading to false after user control loads, which will not occur until you select that tab.

Related

focusing a richtextbox which has keyPressEventListener control to a method and extends Metroframeworks form? [duplicate]

In a Windows Forms application, when do I write the code to set the focus to a control both while the application is launched and subsequently after I call a function?
For instance, if I have a DropDownList, a TextBox and four buttons and I want the Focus to be set to the DropDownList, where do I write my code?
To set the focus to a particular control on application launch, I can set the tab index to that DropDown (with a minimum value, under the assumption TabStop property is set to True).
Now, if the user completes an operation (say, any of the Click Button Events) and then I update the DropDown and after that if I want to set the focus...I can do it as
MyDropDownList.Focus()
QUESTION
NB: The question is more about where, not how?
By far the simplest solution is to set the TabIndex property correctly so that your 'MyDropDownList' control has the lowest index. The next approach is to do it in the constructor. But you have to use Select(), the Focus() method cannot work yet because the control doesn't become visible until later.
Public Sub New()
InitializeComponent()
MyDropDownList.Select()
End Sub
Works in the Load event as well. Focus() starts working in the Shown event.
When the parent window is activated (that is, when it receives the "Activated" event), set the focus to the child control where you want the focus located.
private void Form_AddAppID_Activated(object sender, EventArgs e)
{
textID.Focus();
}
Note that the tab order has nothing to do with where the focus starts. Instead, the tab order is used to decide how the focus gets transferred when the user hits the tab key.

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.

totally disabling TabOrder on the form

WinForms: I don't want any tab order. I want myself be able to programatically handle all the tab orders on the form with some logic that I need.
How can I completely disable tab order? I assume after that I should deal with KeyDown event of each contorl or some similar event ....
You need to override the form's ProcessCmdKey() method. Test keydata == Keys.Tab and (Keys.Shift | Keys.Tab) to detect respectively a forward and a backward tab. Return true to indicate that you've used the key and it shouldn't be used anymore. Which defeats Winforms default handling for the Tab key. No additional changes are needed to the controls.
The form's ActiveControl property tells you which control currently has the focus, you'll need to use it to figure out which control should be focused next. Beware that it can technically be null. Watch out for controls that are embedded in a container control, like a Panel or UserControl. Making this work is definitely unpleasant, also very hard to maintain. Only do this if there are a limited number of controls on the form.
As Adrian said by setting tab stop to false you can disable it
a Function like this can be usefull to diable all tabstop
private void DiableTabStop(Control ctrl)
{
ctrl.TabStop = false;
foreach (Control item in ctrl.Controls)
{
DiableTabStop(item);
}
}
and calling it at form load
DiableTabStop(this);
One approach is to set the TabStop property of every control in the form to false. This will prevent the tab key from giving the controls focus.
If you don't want to do this manually for every control (e.g. in the design view), you can create a method that will iterate over all of the controls in the form's Controls collection and set the property on each one, then call it from your form's constructor.
In addition to disabling the tab stops for the pageframe, as you mentioned, YOU want to control which "tab" is active. You can have a custom property on your form of "WhichTab" should be shown. Then, override the click event and check if the incoming sender/eventarg page is that of another page... no matter what, force focus back to the "WhichTab" YOU are in control of setting... When ready to activate said page, tell the tab control object to ACTIVATE the new page to get displayed to the user.

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.
}
}

MDI window list not updating child title bar texts

I have a MDI container form, and some child forms that update their title bar texts themselves, independently. After the Text property is changed on the child form, the new title bar text from the child is not updated in the window list menu when the menu is opened. This is the auto-generated window list provided by .NET via the MdiWindowListItem property.
The change only propagates when another event changes the window list physically (opening a new child, closing a child, switching to another child).
Is there a way to force an update of the window list programmatically? I already have some code in place to do menu enabling/disabling at the same time the child's title bar text is changed.
I tried the following with no success:
Update() on the main MenuStrip
Refresh() on the main MenuStrip
Invalidate() on the window MenuStrip
Invalidate() on one of the window list items at runtime
Toggling the Checked state twice on one of the window list items at runtime
There don't seem to be any other remotely viable functions to call on the menu item, its parent ToolStrip, or the parent form that contains the menu system.
The above solution did not work for me. But I followed the link, and found this, which works perfectly:
private void windowMenu_DropDownOpening(object sender, EventArgs e)
{
if (this.ActiveMdiChild != null)
{
Form activeChild = this.ActiveMdiChild;
ActivateMdiChild(null);
ActivateMdiChild(activeChild);
}
}
Thank you!
You need to add a TextChanged event to the child form, with this handler:
private void childForm_TextChanged(object sender, EventArgs e) {
this.ActivateMdiChild( null );
this.ActivateMdiChild( sender as Form );
}
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/a36b89aa-57aa-48b5-87a6-49fbddc9c92d
Instead of activate/deactivate, you can send WM_MDIREFRESHMENU message to the MDI client (not frame) window whenever a window title changed.
http://msdn.microsoft.com/en-us/library/windows/desktop/ms644919%28v=VS.85%29.aspx

Categories

Resources