maintaining focus in user controls Windows application - c#

I am having a windows application using c#. I have created a form frmMain which loads when user logs in. There are 4 options for the user like Customer Creation, Supplier Creation, Employee Creation, User Creation
This works fine.
The issue arises with focus. When the user loads Customer user control and filling in some data and let's suppose user is somewhere on 4th control (Textbox/Combobox or any other Windows Forms input control) and suddenly he clicks on CreateUser, then CreateUser control loads, but the focus remains on the 4th control in Customer user control.
What I want is to set focus on the current user control where user left it from else if it is newly loaded set focus on default control.
Please check the code which I am using,
// this method gets called if the form was opened earlier
private void ShowOpenForm(ControlItem _item)
{
try
{
//Get item from menu
ControlItem _menuI = null;
foreach (ToolStripmenuI menuI in tsmenuWindow.DropDownItems)
{
_menuI = (ControlItem)menuI.Tag;
if (_menuI.Control.Name.ToLower() == _item.Control.Name.ToLower())
{
break;
}
_menuI = null;
}
if (_menuI != null)
{
WmenuI_ClearAllSelection();
for (int index = 0; index < PnlUserCtrl.Controls.Count; index++)
{
Control ctl = PnlUserCtrl.Controls[index];
if (ctl.Name.ToLower() == _menuI.Control.Name.ToLower())
{
ctl.Visible = true;
ctl.BringToFront();
break;
}
}
WmenuI_SetCurrentItemChecked(_menuI);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//User can navigate to opened items from menu as well
private void WindowMenuItem_Click(object sender, EventArgs e)
{
try
{
ControlItem _item = (ControlItem)((ToolStripMenuItem)sender).Tag;
WmenuI_ClearAllSelection();
for (int index = 0; index < PnlUserCtrl.Controls.Count; index++)
{
Control ctl = PnlUserCtrl.Controls[index];
if (ctl.Name.ToLower() == _item.Control.Name.ToLower())
{
ctl.Visible = true;
ctl.BringToFront();
if (ctl is BaseControl)
{
((BaseControl)ctl).SetFocus(); // This sets the focus to default textbox
}
break;
}
}
WmenuI_SetCurrentItemChecked(_item);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
But this set focus on the default textbox. Not on the control from where user moves to other user control.

It's a good practice to supply some code in your questions for the sake of getting better answers.
You can manually manipulate focus as you want using SetFocus() method. For your question I believe you can have a object of type Control then you can set it to default control you want to focus on. Then you handle all GotFocus methods of your controls and set the value of that object.
Whenever you want focus restored to last item you can call [your object].SetFocus().
This is just an idea. See if it helps.

Related

C# Winform: How to restrict a user to go into particular TabPage in TabControl

I am Creating a sample Inventory windows form application in which if the dictionary of quantity is empty then user should not be allowed to go into sales tab.
I am using metro design and material skin mix up to design my application I have posted a code sample below which works in case of simple winform control but not working in case of metro and material design.
Code Sample
//check if selected tab is sales tab
if (tcmain.SelectedTab == tpSales)
{
//check if our cart is empty or not
if (Globals.qty.Count == 0)
{
//show error msg
var diaEmptCart = MessageBox.Show("There Are 0 Products in Cart", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//set selected tab as purchase
tcmain.SelectedTab = tpPurchase;
}
else
{
//or show the products available in cart for sales
//populate combo box with them
cmbPro.DataSource = new BindingSource(Globals.qty, null);
//set key as display member
cmbPro.DisplayMember = "Key";
}
}
//check if selectedd tab is tab purchase
if (tcmain.SelectedTab == tpPurchase)
{
if (Globals.qty.Count == 0)
{
//if yes, setting cart empty
pbCart.Image = Image.FromFile(#"C:\Users\ThE PrOgRaMmEr\Documents\Visual Studio 2013\Projects\simpleInventory.cs.MUI\simpleInventory.cs\Resources\crt_empty.png");
}
else
{
//if not, setting cart full
pbCart.Image = Image.FromFile(#"C:\Users\ThE PrOgRaMmEr\Documents\Visual Studio 2013\Projects\simpleInventory.cs.MUI\simpleInventory.cs\Resources\crt_full.png");
}
}
You need to handle tab selecting event of the control. Try this:
private void tcmain_Selecting(object sender, TabControlCancelEventArgs e)
{
//Change whatever you want
if (tcmain.TabPages[e.TabPageIndex] == tpSales && Globals.qty.Count == 0)
e.Cancel = true;
}
But the question is why you're even displaying the tab. I'd recommend not to create tabs which aren't required.

Tab Item remains selected WPF

I am using the Tab_SelectionChanged event of TabControl in WPF. It contains 3 tab items. I have to restrict the user to navigate to other tabs i.e Settings and Schedule while work is in-progress on the home tab. While using the event i am facing an issue i.e. If i clicked on settings tab it shows me a popup "You cannot navigate while work is in progress" and when i clicked on schedule tab after clicking on settings tab it shows me the same popup twice. The reason behind this is the Settings tab remains selected.Here is my code for this:
private void tabMHPC_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TabControl tab = (TabControl)sender;
if (tab.SelectedIndex != -1)
{
if (tab.SelectedIndex != 4 && tab.SelectedIndex != 1 && tab.SelectedIndex != 0)
{
if (scanStatus == "fixing")
{
MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy in fixing issues.Please let the fixation complete.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
homeTab.IsSelected = true;
}
else
{
MessageBox.Show(ApplicationInfo.ApplicationName + " is still busy scanning issues.Please stop it before you leave the Home tab.", ApplicationInfo.ApplicationName, MessageBoxButton.OK, MessageBoxImage.Information);
homeTab.IsSelected = true;
}
}
else if (tab.SelectedIndex == 0)
{
}
}
}
I want that previous tab item isSelected property gets false when i move on other tabitem.
Instead of handling the SelectionChanged event, you should data bind a property of a suitable type to the TabControl.SelectedItem property:
<TabControl SelectedItem="{Binding YourSelectedItemProperty}" ... />
When you do this, you will then be able to stop the TabItem being changed:
public YourDataType YourSelectedItemProperty
{
get { return yourSelectedItemProperty; }
set
{
if (isOkToChangeTabItem)
{
yourSelectedItemProperty = value;
NotifyPropertyChanged("YourSelectedItemProperty");
}
}
}
The final part of the solution is to set the isOkToChangeTabItem variable to true or false depending on whether it is OK for the user to change the selected TabItem or not.

Clearing all data on tab page when clicking No Button

in my application im submitting a data to the db on a tabControls page(page:tabPage2) and i want when hitting the submit button first saving data to db(im achieving this) the a question will ask anything will be done? if the user hit the no button all fields on tabpage2 will reset. so i wrote a script like below but it is not clearing fields.
if (dr == DialogResult.Yes)
{
for (int i = 0; i < this.tabControl1.Controls.Count; i++)
{
if (this.tabControl1.SelectedTab == tabPage2)
{
if (tabPage2.Controls[i] is TextBox)
{
tabPage2.Controls[i].Text = "";
}
if (tabPage2.Controls[i] is ComboBox)
{
tabPage2.Controls[i].Text = "";
}
if (tabPage2.Controls[i] is PictureBox)
{
tabPage2.Controls[i].Text = "";
}
if (tabPage2.Controls[i] is RadioButton)
{
tabPage2.Controls[i].Text = "";
}
}
}
}
If you control the class for the page layout within the specific tab page you want to clear, it's probably best to create a public or internal method in that class (such as Clear()) which can access each of its member controls and clear them directly. That's the easiest approach, and it should usually apply.
If you instead need it to handle a page with an unknown structure, you might need an approach like:
private void ClearControls(Control parentControl)
{
foreach (Control ctrl in parentControl.Controls)
{
TextBox ctrlText;
ComboBox ctrlCombo;
PictureBox ctrlPicture;
RadioButton ctrlRadio;
// Pay careful attention to the parentheses...
if ((ctrlText = ctrl as TextBox) != null)
{
ctrlText.Text = string.Empty;
}
else if ((ctrlCombo = ctrl as ComboBox) != null)
{
ctrlCombo.SelectedIndex = -1;
}
else if ((ctrlPicture = ctrl as PictureBox) != null)
{
// Logic to clear a PictureBox called ctrlPicture
}
else if ((ctrlRadio = ctrl as RadioBox) != null)
{
// Logic to clear a RadioButton called ctrlRadio
}
else if (ctrl.Controls.Count > 0)
{
ClearControls(ctrl); // Recursively clear contained controls.
}
}
}
With a call to start it off from the original handler:
if (dr == DialogResult.Yes)
ClearControls(this.tabControl1);
You are itering over the collection of TabControl child controls, not actual TabPage's.
Change your code to this instead:
if (dr == DialogResult.Yes && this.tabControl1.SelectedTab == tabPage2)
{
foreach (var ctrl in tabPage2.Controls)
{
if (ctrl is TextBox || ctrl is ComboBox || ctrl is PictureBox || ctrl is RadioButton)
{
ctrl.Text = "";
}
}
}
I should say though than setting the Text property to "" for controls other than TextBox feels rather wrong to me. As you'll find out, this won't work for combos, images and radio buttons.
Also if you have controls nested into panels or the like they won't be cleared. Containers have their own nested collection of controls, which in turn can also be containers, and so on and on.
IMHO it would be far better for you to explicitely reset form controls one by one rather than trying to find them dynamically on your form. This way you'll be free to move your controls around at design time without ever worrying about breaking the resetting logic.
Additional suggestion: you can also attach your controls at design time to an instance of your own IExtenderProvider component which will take care of resetting controls appropriately based on their type.

Actual Element That Lost Focus

I am working on an application that has a GridView item on an ASP.net page which is dynamically generated and does a partial post-back as items are updated within the grid-view. This partial post-back is causing the tab indices to be lost or at the very least ignored as the tab order appears to restart. The grid view itself already has the pre-render that is being caught to calculate the new values from the modified items in the grid-view. Is there a way to get what element had the focus of the page prior to the pre-render call? The sender object is the grid-view itself.
You can try using this function, which will return the control that caused the postback. With this, you should be able to reselect it, or find the next tab index.
private Control GetControlThatCausedPostBack(Page page)
{
//initialize a control and set it to null
Control ctrl = null;
//get the event target name and find the control
string ctrlName = Page.Request.Params.Get("__EVENTTARGET");
if (!String.IsNullOrEmpty(ctrlName))
ctrl = page.FindControl(ctrlName);
//return the control to the calling method
return ctrl;
}
Here's an instance where I had dynamically generated inputs that updated totals via AJAX on change. I used this code to determine the next tab index, based on the tab index of the control that caused the postback. Obviously, this code is tailored to my usage, but with some adjustments I think it could work for you as well.
int currentTabIndex = 1;
WebControl postBackCtrl = (WebControl)GetControlThatCausedPostBack(Page);
foreach (PlaceHolder plcHolderCtrl in pnlWorkOrderActuals.Controls.OfType<PlaceHolder>())
{
foreach (GuardActualHours entryCtrl in plcHolderCtrl.Controls.OfType<GuardActualHours>())
{
foreach (Control childCtrl in entryCtrl.Controls.OfType<Panel>())
{
if (childCtrl.Visible)
{
foreach (RadDateInput dateInput in childCtrl.Controls.OfType<RadDateInput>())
{
dateInput.TabIndex = (short)currentTabIndex;
if (postBackCtrl != null)
{
if (dateInput.TabIndex == postBackCtrl.TabIndex + 1)
dateInput.Focus();
}
currentTabIndex++;
}
}
}
}
}

Getting controls in a winform to disable them

I'm trying to get all controls in a winform disabled at the Load event.
I have a form (MDI) which loads a Login Form. I want to disable the controls behind the Login Form to only let the user enter his username and password, and then if the user is valid re-enable the controls again.
Just show the login form as a modal dialog, i.e., frm.ShowDialog( ).
Or, if you really want to disable each control, use the Form's Controls collection:
void ChangeEnabled( bool enabled )
{
foreach ( Control c in this.Controls )
{
c.Enabled = enabled;
}
}
I suggest doing it this way instead of simply setting the Form's Enabled propery because if you disable the form itself you also disable the tool bar buttons. If that is ok with you then just set the form to disabled:
this.Enabled = false;
However, if you are going to do this you may as well just show the login prompt as a modal dialog :)
Simple Lambda Solution
form.Controls.Cast<Control>()
.ToList()
.ForEach(x=>x.Enabled = false);
Container like Panel control that contains other controls
then I used queue and recursive function get all controls.
for (Control control in GetAllControls(this.Controls))
{
control.Enabled = false;
}
public List<Control> GetAllControls(Control.ControlCollection containerControls, params Control[] excludeControlList)
{
List<Control> controlList = new List<Control>();
Queue<Control.ControlCollection> queue = new Queue<Control.ControlCollection>();
queue.Enqueue(containerControls);
while (queue.Count > 0)
{
Control.ControlCollection controls = queue.Dequeue();
if (controls == null || controls.Count == 0)
continue;
foreach (Control control in controls)
{
if (excludeControlList != null)
{
if (excludeControlList.SingleOrDefault(expControl => (control == expControl)) != null)
continue;
}
controlList.Add(control);
queue.Enqueue(control.Controls);
}
}
return controlList;
}
Just for some fun with linq, because you can.....
What you could do is create a "BatchExecute" extension method for IEnumerable and update all your controls in 1 hit.
public static class BatchExecuteExtension
{
public static void BatchExecute<T>(this IEnumerable<T> list, Action<T> action)
{
foreach (T obj in list)
{
action(obj);
}
}
}
Then in your code....
this.Controls.Cast<Control>().BatchExecute( c => c.enabled = false);
Cool.
I agree that ShowDialog is the way to go, but to answer the original question, you can do this if you want to disable all controls:
foreach (Control c in this.Controls)
{
c.Enabled = false;
}
As Ed said, showing the form as a modal dialog will do what you want. Be sure to check the dialog result returned from ShowDialog in case they cancel it instead of clicking login.
But if you really want to disable all the controls on the form then you should be able to just disable the form itself, or some other parent control like a panel that has all controls in it. That will disable all child controls. This will also allow the child controls to go back to their previous state when the parent control is enabled again.
Trying the ShowDialog show this exception:
Form that is not a top-level form cannot be displayed as a modal dialog box. Remove the form from any parent form before calling showDialog.
What im doing is this:
private void frmControlPanel_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
ShowLogin();
//User = "GutierrezDev"; // Get user name.
//tssObject02.Text = User;
}
private void ShowLogin()
{
Login = new frmLogin
{
MdiParent = this,
Text = "Login",
MaximizeBox = false,
MinimizeBox = false,
FormBorderStyle = FormBorderStyle.FixedDialog,
StartPosition = FormStartPosition.CenterScreen
};
Login.ShowDialog();
}

Categories