Author Topic: Get User Input in either feet/inches or inches form  (Read 3254 times)

0 Members and 1 Guest are viewing this topic.

Keith Brown

  • Swamp Rat
  • Posts: 601
Get User Input in either feet/inches or inches form
« on: February 12, 2014, 04:31:14 PM »
Has anyone had experience grabbing an elevation from a form and then using that elevation in some calculations? I am struggling with how to accept input on the form.  I would like to be able to emulate what AutoCAD does and if the user enters the value as a single number then I would convert it to the string value.  Also if the user entered in the string value then I would validate it to make sure that it is correct.  I know that I can use Autodesk.AutoCAD.Runtime.Converter to switch back and forth between string and double but how would you check the input on the form to make sure it is in the correct form and if incorrect then invalidate the users input and revert back to the original input?
 
Any thoughts and/or ideas would be appreciated.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Get User Input in either feet/inches or inches form
« Reply #1 on: February 12, 2014, 04:45:01 PM »
WinForm or WPF?

Each has ways for validation

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Get User Input in either feet/inches or inches form
« Reply #2 on: February 12, 2014, 05:13:43 PM »
WinForm or WPF?

Each has ways for validation


I am using windows forms on this project.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Get User Input in either feet/inches or inches form
« Reply #3 on: February 12, 2014, 06:35:59 PM »
One way is to use Validating and Validated event.
I will post example shortly

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Get User Input in either feet/inches or inches form
« Reply #4 on: February 12, 2014, 07:00:38 PM »
Using Validating and Validated events gives you way to catch error when textbox loses focus.

This little example will not let user move to another control until they have entered a valid input. Normally would add check if empty or whitespace so they could clear it and move on.

Uses a errorprovider and changes backcolor to notify user.
Pics below for error and good.






code
Code - C#: [Select]
  1.   public partial class Form1 : Form
  2.     {
  3.         private Color TextBackColor { get; set; }
  4.         public Form1()
  5.         {
  6.             InitializeComponent();
  7.             TextBackColor = textBox1.BackColor;
  8.         }
  9.  
  10.         private void button1_Click(object sender, EventArgs e)
  11.         {
  12.          
  13.         }
  14.  
  15.         private void textBox1_Validating(object sender, CancelEventArgs e)
  16.         {
  17.             try
  18.             {
  19.                 textBox2.Text = Converter.StringToDistance(textBox1.Text).ToString(CultureInfo.InvariantCulture);
  20.             }
  21.             catch (Autodesk.AutoCAD.Runtime.Exception ex)
  22.             {
  23.                 e.Cancel = true;
  24.                 if (ex.ErrorStatus == ErrorStatus.InvalidInput)
  25.                 {
  26.                     errorProvider1.SetError(textBox1, "Invalid");
  27.                     textBox1.BackColor = Color.LightCoral;
  28.                 }
  29.                 else
  30.                 {
  31.                     throw;
  32.                 }
  33.             }
  34.         }
  35.  
  36.         private void textBox1_Validated(object sender, EventArgs e)
  37.         {
  38.             errorProvider1.SetError(textBox1, "");
  39.             textBox1.BackColor = TextBackColor;
  40.         }
  41.     }
  42.  


Attached project used Autodesk's plugin and referenced 2013 assemblies

Here is a link for more info
http://msdn.microsoft.com/en-us/library/vstudio/system.windows.forms.control.validating(v=vs.100).aspx

Keith Brown

  • Swamp Rat
  • Posts: 601
Re: Get User Input in either feet/inches or inches form
« Reply #5 on: February 13, 2014, 09:43:07 AM »
Thanks Jeff,  I will take a look at this today.  It is the final piece of the puzzle for me.
Keith Brown | AutoCAD MEP Blog | RSS Feed
AutoCAD MEP 2014 / Revit MEP 2014 / EastCoast CAD/CAM addon / Visual Studio 2013

Jeff H

  • Needs a day job
  • Posts: 6151
Re: Get User Input in either feet/inches or inches form
« Reply #6 on: February 18, 2014, 11:15:38 AM »
Here is something else and have not opened it tried it since posted
http://www.theswamp.org/index.php?topic=41412.msg465234#msg465234