Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
AnswerRe: C# and Excel Pin
Anas Abbas20-May-10 23:53
Anas Abbas20-May-10 23:53 
QuestionHow Do I Handle A Textbox Change Correctly? Pin
Roger Wright20-May-10 16:23
professionalRoger Wright20-May-10 16:23 
AnswerRe: How Do I Handle A Textbox Change Correctly? Pin
Dr.Walt Fair, PE20-May-10 16:57
professionalDr.Walt Fair, PE20-May-10 16:57 
AnswerRe: How Do I Handle A Textbox Change Correctly? Pin
Luc Pattyn20-May-10 17:25
sitebuilderLuc Pattyn20-May-10 17:25 
GeneralRe: How Do I Handle A Textbox Change Correctly? Pin
Roger Wright20-May-10 17:52
professionalRoger Wright20-May-10 17:52 
GeneralRe: How Do I Handle A Textbox Change Correctly? Pin
Luc Pattyn20-May-10 17:56
sitebuilderLuc Pattyn20-May-10 17:56 
AnswerRe: How Do I Handle A Textbox Change Correctly? Pin
William Winner21-May-10 8:15
William Winner21-May-10 8:15 
GeneralRe: How Do I Handle A Textbox Change Correctly? Pin
Peter_in_278023-May-10 14:37
professionalPeter_in_278023-May-10 14:37 
Hi Roger,
Hope this doesn't arrive too late - I saw your post the other day just as I was running out the door. I don't know C# from ****, but here's pseudo-code that I've implemented in various other language/platforms. The common idea is a form with multiple text boxes, typically numeric. Modifying any one of these updates all the others affected. I'll use a real simple example: temperature conversion - boxes are celsius, fahrenheit, kelvin.
I'm assuming an event-driven environment with an accessible exception-handling mechanism, although the code can be mangled to fit dumber boxes.
// form-level variables
float deg_cel, deg_fahr, deg_kel;

// manifest constants (literals for readability)
const CELSIUS = 1, FAHRENHEIT = 2, KELVIN = 3;
// celsius textbox onchange handler (may also need to call on other events, depending on what you can get)
try
    {
    deg_cel = parse_to_float(text_of_cel_box);
    // if that worked, keep going. if it failed, we took the exception
    if (deg_cel < CEL_MIN || deg_cel > CEL_MAX)
        throw new exception("out of range");
    update_all(CELSIUS);
    }
catch (exception exc)
    {
    // something went wrong above.
    if (exc ...)
        // can pull apart for finer-grained handling if required
        // maybe throw up modal box saying what's wrong...
    // beep, highlight cel_box, maybe position cursor if you did your own parse_to_float
    }

// fahrenheit, kelvin onchange handlers are essentially the same...

// common update routine update_all(which)
// phase 1 - update all flavours of the data
switch(which)
    {
    case CELSIUS:
        deg_fahr = deg_cel * 1.80 + 32.0;
        deg_kel = deg_cel + 273.16;
        break;
    case FAHRENHEIT:
        ... etc
    }
// phase 2 - display what (might have) changed
if (which != CELSIUS)
    text_of_cel_box = appropriate_format(deg_cel);
if (which != FAHRENHEIT)
    text_of_fahr_box = appropriate_format(deg_fahr);
... etc

The biggest gotcha is if this last update of the text boxes causes another onchange event... Then you need some (dirty-ish) code to stop it infinitely recursing up its fundament.
The structure of the update routine looks a bit overkill for this simple example, but it scales nicely to things like one I did for location conversions - lat/long in different formats, zone/easting/northing on multiple grids, map sheet number/name, etc - about a dozen text boxes all up.

Cheers & HTH,
Peter
Software rusts. Simon Stephenson, ca 1994.

AnswerRe: How Do I Handle A Textbox Change Correctly? Pin
Peter_in_278023-May-10 14:40
professionalPeter_in_278023-May-10 14:40 
QuestionWinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 11:54
Matt U.20-May-10 11:54 
AnswerRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 12:11
sitebuilderLuc Pattyn20-May-10 12:11 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 13:12
Matt U.20-May-10 13:12 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 13:34
sitebuilderLuc Pattyn20-May-10 13:34 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 14:48
Matt U.20-May-10 14:48 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 14:56
sitebuilderLuc Pattyn20-May-10 14:56 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 14:59
Matt U.20-May-10 14:59 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 15:13
Matt U.20-May-10 15:13 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 15:25
sitebuilderLuc Pattyn20-May-10 15:25 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 15:29
Matt U.20-May-10 15:29 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 15:44
sitebuilderLuc Pattyn20-May-10 15:44 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 15:55
Matt U.20-May-10 15:55 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Luc Pattyn20-May-10 16:17
sitebuilderLuc Pattyn20-May-10 16:17 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 Pin
Matt U.20-May-10 16:31
Matt U.20-May-10 16:31 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 [modifed] Pin
Luc Pattyn20-May-10 16:49
sitebuilderLuc Pattyn20-May-10 16:49 
GeneralRe: WinForms App Only Runs When Pressing F5 (Start Debugging) :: VS2008 & .NET 3.5 [modifed] Pin
Matt U.20-May-10 16:58
Matt U.20-May-10 16:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.