Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

numeric input with completion detection

Status
Not open for further replies.

themcnasty

Programmer
May 31, 2007
4
US
I am very new to C# and Visual studio and have the Express 2005 edition. Specifically, I have never created or imported a custom control nor have any idea how, despite a rudimentary understanding of inheritance and having downloaded a numeric control from the codeproject (and not knowing how to get it into a project).

I am basically making a GUI for connecting to proprietary hardware (a PLC for industrial automation). I will have lots of numeric values and a few strings and bools that will update real time AND many will allow user input. The numeric values come into C# already formated like so: "-1234.00" or "1234" depending on sign and if it is int or float (number of decimal places varies by number and may need to be truncated). Bools come in as "TRUE" or "FALSE"

I HAVE to do two things. First, the user needs to be able to input things on the windows side without effecting their value on the PLC until they are done typing (typing 134 will not send values of 1 then 13 then 134). I also need to not overwrite the field they are editing with the current PLC value. I was thinking a universal method that makes all text controls lose focus when the user presses enter. Then I could prevent the datapoint with focus from updating from the PLC or changing the PLC value. How do/can I make the enter key remove focus from txt controls while acting in the default manner otherwise?

Second, I have to be able to very easily and cleanly display numeric values correctly, with decimal and sign just as they appear in the automatically formated string and truncate floats with tons of decimal places. Easy you say, but I also must restrict user input to specified ranges of values and the proper numeric characters (including sign and decimal). For instance, one field may only allow values of 96 to 104 and values entered outside this range should automagically go to the nearest limit value when the control loses focus or the user presses enter. Or maybe the value is a USINT, I don't want '-' or '.' being entered!

can anyone tell me how to go about this? I'm not new to programming, just new to windows GUI and C#. The custom control I have from the codingproject seems to do what I want for numeric input but I have no freaking clue how to get it from Shorty.Windows.Forms.csproj to control in my toolbar (Using 2005 Express, so not full featured Studio)!
 
I also want to add that there will be many buttons and some of them need to have colors that convey their status regardless of mouseover. Right now I am using the flat mode which both doesn't show 3D up or down state and mouseover shows the same color regardless of state. Is there an easy way to used 3D border style with two different colors for the state regardless of mouse position? Or maybe I just have to keep changing the color value every time the bool changes?
 
Holy Moses!

That's a long description. While I can't "solve" this problem for you I think I can give you some ideas that will get you started.

#1 - Displaying a float or decimal to a specified number of decimal places:

Float myfloat = 12.0123456789;

myfloat.ToString("N2"); // will output 12.01
myfloat.ToString("N4"); // will output 12.0123

#2 - Design
the user needs to be able to input things on the windows side without effecting their value on the PLC until they are done typing

You should be able to decide when to update something. Don't react to the TextChanged method of a textbox. Simply add an "update" button on your form to validate the data and update the PLC. If you need this to be a bit better, then react to the KeyDown event. If the user presses the enter key then do your validation and update there.

private void myTextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Enter)
{
if (Validate()) //write your own Validate method that checks the user input and returns true if it's ok to use and false if there's errors
{
//update your PLC here...
}

}
}

#3 - For numeric input, look at the NumericUpDown control. It is in the toolbox of VisualStudio by default. If this control doesn't work for you then right click on your solution and select "Add Existing Project". Then navigate to the codeproject project (hehe) that you downloaded and click ok. Next right click on References under your current project and go to "Add Reference". click on the "Projects" tab. Select the codeproject item listed there and click add.

Now you can use these items but putting a using statement at the top of your class. The using statement should be pointing at the new project.

using codeproject.project //or whatever the project namespace is that you downloaded.

I hope that gets you started...
 
if you want to run code only when a user is done typing, use the Validating and Validated events which fire when the user exits the current control. 2005 is better than previous versions in that you can decide what to do if the Validating event fails (e.Cancel = true).

trust me, KeyDown is only for those who really like juggling knives.


mr s. <;)

 
haha, thanks. The ToString("N#") tip is very useful. I think I'm going to monitor getting focus and change the color of the box and prevent it's update. Then on loss of focus I'll validate. If they press enter while a txtfield has focus then I'll make it lose focus, probably force focus onto some hidden control or something.

Thanks again!
 
you can also use string.format() function, for instanse consider the below example:

Code:
Decimal x = 12.3456;
string s = String.Format("{0:F2}",x); 
console.WriteLine(s); // will output 12.34

F2 is a control: the number after 'F' indicates the number of digits after decimal point.

Touraj Ebrahimi
 
What I ended up doing was going with numericUpDown controls (though I would prefer not having the actual up down arrows) and only updating fields that do not have focus. Then I update the PLC when a control Validates or (for strings) when the lose focus. I handle submitting changes on Return with this:

public MainHMI()
{
InitializeComponent();
this.KeyPreview = true;
}

private void MainHMI_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
this.tabMain.Select();
}
}

Setting focus to the button both triggers the validated event that writes to the PLC as well as setting focus to a control that is not monitored to prevent updates from PLC nor forces a write to the PLC when it loses focus. I'm thinking about catching esc also and updating the field with the latest PLC value rather than writing to the PLC.

Thanks everyone!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top