controlling where the focus and cursor go

Wizard for Windows Forms Forum

Posted 19 years ago by Mark Mickelsen
Avatar
First problem: I'm having trouble controlling where the focus is when an interior page comes up. I want to be able to decide exactly which text box has the blinking cursor in it based of runtime data. I compute where the cursor is supposed to be in the "page"_Enter event, that is, the event that fires when the page is loaded. When I know what box I want to have the focus, I execute a "box".focus() function to put the focus there. But when I run it, the focus doesn't appear anywhere. I've messed with TabStops and TabIndexes and everything else I can think of and I just can't get the cursor to appear when the page is first loaded without having to click the mouse somewhere. And then I don't know how to control where it will appear.

Second problem: I want the focus on a page to go down the page from text box to text box until it gets to a SAVE button and then I want it to stay there unless the user specifically clicks on a text box or on the NEXT button. I don't want it to move with the TAB key, BACKSPACE key, or any other keystroke. I can't figure out how to make this happen.

Any help with these problems would be greatly appreciated. I've been banging my head against the wall on them for about three days now. Please help.

Thanks,
Mark

Comments (21)

Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Mark,

What version of Wizard do you have? Also do you use the Wizard control or the WizardDialogForm wrapper?

For your second problem, I'm not sure if that's possible in .NET. You might be able to turn the TabStops off for the controls that you don't want to get focus however that will still allow mouse clicks to set focus. It should prevent Tab selection though.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
I am using the Wizard control. The version is 3.0.0106 using shared 1.0.0071
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
For this we have a workaround since it seems to be some sort of issue with the .NET framework. It's in our sample app aleady.

Look in the constructor of MainForm. The last line is this:
this.ActiveControl = this.Wizard;
Add that to your code and it should fix the issue.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
I don't know what I'm doing wrong, but I get an error from that line. It says it can't find "Wizard". I think I have the "using" statements I need, and everything else is found alright, so I don't know what's wrong.
Help.
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Oh, just replace this.Wizard with whatever Wizard control reference variable you use.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
Yeah, well, that's what I thought I was calling it. I'll have to check.
Thanks
Posted 19 years ago by Mark Mickelsen
Avatar
I think I have some idea why this isn't working for me, but I don't know how to fix it. When my Main program starts, I don't start an Actipro form, I use an ordinary Windows form, and then I bring the Wizard pages later as the result of a button click. Then I bring in interior pages, not the beginning exterior page. Here is the code segment that does all this. Maybe it will give you some idea of what I should set ActiveControl to.
namespace Test.Employee
{
    public class Form1 : System.Windows.Forms.Form
    {
        private Infragistics.Win.Misc.UltraButton ubtnGo;
        private Infragistics.Win.Misc.UltraButton ubtnStop;
        private System.ComponentModel.Container components = null;

        public Form1()
        {
            InitializeComponent();
        }

        protected override void Dispose( bool disposing )
        {
            if( disposing )
            {
                if (components != null) 
                {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        #region Windows Form Designer generated code
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            Infragistics.Win.Appearance appearance1 = new Infragistics.Win.Appearance();
            Infragistics.Win.Appearance appearance2 = new Infragistics.Win.Appearance();
            this.ubtnGo = new Infragistics.Win.Misc.UltraButton();
            this.ubtnStop = new Infragistics.Win.Misc.UltraButton();
            this.SuspendLayout();
            // 
            // ubtnGo
            // 
            appearance1.ForeColor = System.Drawing.Color.LawnGreen;
            this.ubtnGo.Appearance = appearance1;
            this.ubtnGo.Font = new System.Drawing.Font("Arial", 72F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.ubtnGo.Location = new System.Drawing.Point(184, 152);
            this.ubtnGo.Name = "ubtnGo";
            this.ubtnGo.Size = new System.Drawing.Size(264, 248);
            this.ubtnGo.TabIndex = 0;
            this.ubtnGo.Text = "GO";
            this.ubtnGo.Click += new System.EventHandler(this.ubtnGo_Click);
            // 
            // ubtnStop
            // 
            appearance2.ForeColor = System.Drawing.Color.Red;
            this.ubtnStop.Appearance = appearance2;
            this.ubtnStop.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
            this.ubtnStop.Location = new System.Drawing.Point(296, 432);
            this.ubtnStop.Name = "ubtnStop";
            this.ubtnStop.Size = new System.Drawing.Size(48, 23);
            this.ubtnStop.TabIndex = 1;
            this.ubtnStop.Text = "STOP";
            this.ubtnStop.Click += new System.EventHandler(this.ubtnStop_Click);
            // 
            // Form1
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(632, 546);
            this.Controls.Add(this.ubtnStop);
            this.Controls.Add(this.ubtnGo);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.ActiveControl = this.Wizard;
        }
        #endregion

        [STAThread]
        static void Main() 
        {
            Application.Run(new Form1());
        }

        private void ubtnGo_Click(object sender, System.EventArgs e)
        {
            //ClearControlArea();
            foreach(Control c in this.Controls)
            {
                if(c.GetType()==typeof(Infragistics.Win.Misc.UltraButton))
                    c.Hide();
            }

            int StoreID = 12;
            EmployeeUI EUI=new EmployeeUI(StoreID);
            EUI.Dock=DockStyle.Fill;
            this.Controls.Add(EUI);
            EUI.Show();
            return;
        }
    }
}
[Modified at 06/27/2005 09:52 PM]
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hope you don't mind, I added C# code formatting for your code to make it easier to read. There's a toolbar button for doing that when you post.

I assume your EmployeeUI is a Wizard control? If not can you point out what is? I don't really see any Wizard references in that code.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
Thank you. I didn't know that that was what those buttons did. Yes, EmployeeUI is a multipage Wizard control. I can send you that code if you need it. The main form here just has a big GO button on it to launch the user control pages for debugging. They will be launched by other software, but in the same manner, when integrated into the real application code. What else can I tell you about it?
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Maybe change the code paret to be this:
     private void ubtnGo_Click(object sender, System.EventArgs e)
        {
            //ClearControlArea();
            foreach(Control c in this.Controls)
            {
                if(c.GetType()==typeof(Infragistics.Win.Misc.UltraButton))
                    c.Hide();
            }

            int StoreID = 12;
            EmployeeUI EUI=new EmployeeUI(StoreID);
            EUI.Dock=DockStyle.Fill;
            this.Controls.Add(EUI);
            EUI.Visible = true;
            this.ActiveControl = EUI;
            return;
        }


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
No. This compiles and runs but doesn't transfer focus the way it should, either. Anything else I can try?
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Maybe email us a sample project that shows the problem. Keep in mind that we won't have licenses for the other third-party controls you use so you might have to make a more generic example.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
I'm emailing you a sanitized version of this project. There are two files, the Forms1.cs file and the EmployeeUI.cs file. This is all I need to see the problem. If you need anything else, please let me know.
Thanks,
Mark
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Thanks for the sample. I just replied to your email with what you need to change to get it working.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
Well, I thought we had this thing licked, but we don't. Now, the page appears to come with the focus the way it should but then exhibits some very weird behavior. There is a text box to receive some text and a verify button to tell the code to verify the text. (This button may seem unnecessary, but for various reasons, it is not.) As soon as the user has input 6 digits to the box, focus should automatically jump to the button to wait for the user to click it. When the user clicks the button, the code verifies the number that was input and proceeds on if it was valid. At least that is what is supposed to happen, and that is what used to happen before we "fixed" the focus problem. Now, when the page is first loaded, the cursor and focus are in the text box as they should be. I type 6 digits and the focus immediately goes to the button. The button is highlighted as you would expect, and I know that the focus is on the button because if I hit the TAB key at that moment, the focus moves to the cancel button at the bottom of the page. BUT, the text in the textbox is cleared out and a cursor appears in the textbox. If I click the button at this point my program dies because there is no text in the textbox and my code supposedly guaranteed that there would be 6 digits there. Somehow the focus and the cursor have been split apart. I know this because if I hit RETURN it will act exactly as if I had clicked the button. So, the focus must be on the button. But, if I start typing digits, they go into the textbox, so the cursor must be there. If I actually click on the textbox with the mouse, then everything gets pulled back together again and works fine from then on. Obviously, something still isn't right, and I don't have a clue as to what to do about it. If you could please help me again, I would greatly appreciate it.

Thanks,
Mark
Posted 19 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Mark,

Maybe try setting the TextBox's parent control's ActiveControl to the TextBox. Then do textBox.Focus(). See if that helps.

If not and you want us to look at it, please send us a complete project that can compile without external resources that demonstrates the problem and include exact steps on how to reproduce this in your project.


Actipro Software Support

Posted 19 years ago by Mark Mickelsen
Avatar
I've tried everything that you've suggested and I still can't get it to work right. I've created a project with only the files necessary to recreate the problem, compiled it and run it to make sure it runs and that the problem still happens, and put it in a RAR archive and emailed to you. I hope you can help me.
Thanks,
Mark
Posted 17 years ago by Warren J. Hairston - President, Logical Operators, Inc.
Avatar
Was this issue ever solved? I'm having a very similar (although much simpler) problem of controlling which field gets the focus.

I have a wizard page with three textboxes. I want the third textbox to have the focus whenever the page appears. (The first two textboxes hold calculated data by default, but the user can always TAB back to them if he/she needs to change the defaults.)

I have defined an event handler for the page's Enter event that appears to work perfectly for calculating the defaults, etc. However, if I add the following code at the end of that handler, nothing seems to change.

private void pageResults_Enter(object sender, EventArgs e)
{
    // ... other code here to calculate default values for Textbox1 and Textbox2 ...

    Textbox3.Select();   //set the focus to Textbox3
}   //EventHandler pageResults_Enter
The code in the event handler runs and the page appears, but the focus is still on the first textbox (Textbox1) - not the third textbox (Textbox3). It simply appears that the Textbox3.Select() statement is ignored.

I've also tried using the Textbox3.Focus() method instead of Select(), but the same thing happens.

Any ideas? Is there a different event where I should be attempting to set the focus for a WizardPage's child control?

Thanks in advance!
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Warren,

Have you tried just making the third textbox the lowest TabIndex in the page and then making the first textbox the second lowest, etc.?


Actipro Software Support

Posted 17 years ago by Warren J. Hairston - President, Logical Operators, Inc.
Avatar
Actually, I did try that. It works OK for the initial display of the wizard page, but it's not optimal. For example, if the user wants to TAB to the wizard buttons (Back, Next, etc.), then he has to TAB through Textbox1 and Textbox2 first. Also, if I add other fields after Textbox3, then I'll need to remember that their TabIndex properties need to be lower than those of Textbox1 and Textbox2.

This will work for now, it just seems strange to the end user. Anyway to address this in a future release?

Thanks again!
Posted 17 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
What if you try calling the Focus method on your child control in the Wizard.SelectionChanged event handler when appropriate? That should fire after the new page is displayed.


Actipro Software Support

The latest build of this product (v24.1.0) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.