0

Hello i have a Windows Forms application that runs on a touch screen and it has textboxes where a user inputs numbers.
When a user clicks the TextBox a virtual numpad pops up.

private void textboxClicked(object sender, EventArgs e) // this code openes the numPad
{
    curTextBox = sender as TextBox;   // puts all the params in the curTextBox that i declared at the top of the class
    curTextBox.Text = null; // clears the textbox
    vnumPadForm.Location = PointToScreen(new Point(curTextBox.Right, curTextBox.Top));
    vnumPadForm.Show();
}

The problem I have is when the user clicks a textbox on the far right, the numpad goes beyond the screen so only half of it is showing.

How would I make it so that if the space between the textbox and edge of the screen is too small, the numpad would pop up on the leftside of the TextBox?

Well, I couldn't really find a solution yet.

Jimi
  • 29,621
  • 8
  • 43
  • 61
saptech
  • 15
  • 2
  • You said wpf but the question ist tagged with winforms. What's the target? – jeb May 04 '23 at 18:01
  • You have to evaluate whether `PointToScreen(new Point(curTextBox.Right, curTextBox.Top)).X + vnumPadForm.Width < Screen.FromControl(curTextBox).WorkingArea.Width` (absolute, in case the current screen has negative coordinates). You may also want to check the vertical position -- The notes here may come in handy: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) – Jimi May 04 '23 at 18:14
  • Out of curiosity, is there a reason you made your own on-screen number-pad instead of using Windows' built-in OSK? [Windows' OSK supports _input scopes_ for things like numeric-only-input](https://learn.microsoft.com/en-us/windows/iot/iot-enterprise/os-features/on-screen-keyboard#support-for-input-scopes). e.g. https://learn.microsoft.com/en-us/windows/apps/design/input/use-input-scope-to-change-the-touch-keyboard – Dai May 04 '23 at 18:30
  • i was thinking the same, but it is not said that the target platform is a windows device. – jeb May 04 '23 at 18:31
  • @jeb The OP is using WinForms.... _that kinda narrows it down a bit_. – Dai May 04 '23 at 19:07

1 Answers1

0

Assuming vnumPadForm is a Form you can calucate it's width and measure if it fits into the remaining space.

As stated in the comments you could calculate the window width in a different way like using

Screen.PrimaryScreen.WorkingArea.Right

private void curTextBox_Click(object sender, EventArgs e)
{
    curTextBox = sender as TextBox;  
    curTextBox.Text = null;

    ShowNumPad(curTextBox.Right, curTextBox.Top, curTextBox.Left);
}

private void ShowNumPad(int right, int top, int left)
{
    var vnumPadForm = new vnumPadForm();
    vnumPadForm.WindowState = FormWindowState.Normal;
    vnumPadForm.StartPosition = FormStartPosition.Manual;

    // show at the top right corner
    vnumPadForm.Location = PointToScreen(new Point(right, top));

    if (right + vnumPadForm.Width > this.Width)
    {
        // not enough space, show at the left top corner
        right = left - vnumPadForm.Width;
        vnumPadForm.Location = PointToScreen(new Point(right,top));
    }

    vnumPadForm.BringToFront();
    vnumPadForm.Show();
} 

 

Note that I initialised the vnumPadForm in the method because i did not know where it is initialized. WindowState and StartPosition are required in order to set the location as stated here:

Form.Location doesn't work

I used BringToFront wich might be optional for your case.

jeb
  • 848
  • 5
  • 16