CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2002
    Location
    Texas
    Posts
    29

    .NET 2.0 form Load function

    I have a CE application which is a menu (not menu strip). Currently, I have a OnKeyDown function which checks to see which number the user typed and then launches the corresponding application on the handheld. When that application ends, they are back at my menu and may select another application to run.

    Here are my two problems:

    1) When they return to my menu form, the number they keyed in is still displayed in the txtChoice box. I want it to be blank when they come back. I tried a variety of Invalidates and Refreshes without success.

    2) In the Load function for the form I read a configuration file on the hanheld (which can change anytime). This file lists the various menu choices and their corresponding executables. So, whenever someone comes back from running an application, I'd like to reinvoke the Load function, or cause the config file to be re-read and the menu choices to be redisplayed.

    Ideas? Thanks! --cat

  2. #2
    Join Date
    Nov 2006
    Location
    North Bend, WA
    Posts
    487

    Re: .NET 2.0 form Load function

    1. Have you tried txtChoice.Text = ""? Invalidate() just tells the form to repaint itself, not to remove the data.

    2. Try handling the Activated message in your form.

    BTW, why do you have a text control if you have an OnKeyDown handler? Can they type other things into the text control?
    Last edited by dcell59; January 9th, 2007 at 07:01 PM.

  3. #3
    Join Date
    Sep 2002
    Location
    Texas
    Posts
    29

    Re: .NET 2.0 form Load function

    yes. i had txtChoice.Text = ""; txtChoice.Invalidate();

    It didn't seem to do anything. Explain your second suggestion, please?

  4. #4
    Join Date
    Nov 2006
    Location
    North Bend, WA
    Posts
    487

    Re: .NET 2.0 form Load function

    Invalidate() shouldn't be neccessary. A text control will redraw itself when the value is changed. Are you setting txtChoice.Text = "" in your OnKeyDown handler? I still don't understand why you even have a text control. Also, you should probably handle OnKeyUp rather than OnKeyDown. When you handle OnKeyDown and then run another application, the OnKeyUp message can get sent to the wrong window.

    As for part 2, you say you have a form. A form has an Activated event (at least it does in the standard .Net framework). This event is triggered when the form becomes active (has the focus). You should be able to reload your config file and update the UI in the handler for Activated.

  5. #5
    Join Date
    Sep 2002
    Location
    Texas
    Posts
    29

    Re: .NET 2.0 form Load function

    Below is the code for keydown and validating. If i have two entries in my invokeApp arraylist, and i enter "2" as my choice, the second app runs, and when i return the validating function fires and blanks out teh text entry box. perfect! BUT if i enter "3" as my choice, the keydown function correctly realises that it shouldn't run an app, so it does nothing. That's good, but it also doesn't fire the validating function, so the text box remains populated with "3", until I type a valid number. How can I get it to fire validating?





    private void txtChoice_KeyDown(object sender, KeyEventArgs e)
    {
    //MessageBox.Show("key - 49 " + (e.KeyValue - 49) + " count " + invokeApp.Count);
    if (e.KeyCode >= Keys.D1 && e.KeyCode <= Keys.D5)
    { //make sure they entered int between 1 and 5
    if ((e.KeyValue - 49) < invokeApp.Count)
    {//make sure they didn't key an int for an option that isn't there
    //MessageBox.Show("key value = " + e.KeyValue);
    //the value of key 1 is 49, 2 is 50, etc. subtract 49 to get int 0-4
    appPathAndName = @"\Program Files\" + (string)invokeApp[e.KeyValue - 49];
    Process.Start(appPathAndName, "");
    }
    else
    {
    e.Handled = true;
    }
    }
    else
    {
    e.Handled = true;
    }
    }

    void txtChoice_Validating(object sender, System.ComponentModel.CancelEventArgs e)
    {
    if (txtChoice.Text.Length > 0)
    {
    txtChoice.Text = "";
    txtChoice.Focus();
    e.Cancel = true;
    }
    }

  6. #6
    Join Date
    Sep 2002
    Location
    Texas
    Posts
    29

    Re: .NET 2.0 form Load function

    Ok...problem solved. I need to use the KeyPress function instead of the KeyDown.

    here it is:
    void txtChoice_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (!Char.IsDigit(e.KeyChar))
    {
    e.Handled = true;
    return;
    }
    int choice = int.Parse(e.KeyChar.ToString());
    if (choice >= 1 && choice <= 5)
    { //make sure they entered int between 1 and 5
    if (choice <= invokeApp.Count)
    {//make sure they didn't key an int for an option that isn't there
    appPathAndName = @"\Program Files\" + (string)invokeApp[choice - 1];
    Process.Start(appPathAndName, "");
    }
    else
    {
    e.Handled = true;
    }
    }
    else
    {
    e.Handled = true;
    }
    }

    thanks for the assist...it got me thinking in the right direction!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured