Newbie here - a few questions..
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Newbie here - a few questions..

  1. #1
    Join Date
    Jun 2011
    Posts
    8

    Newbie here - a few questions..

    Hello everyone! I'm working on my very first program ever, it's a calculator for (flightsim) pilots that calculates how fast you need to descend in order to reach your target at the altitude you want.

    Here's what it looks like right now..


    It works very well, but there are a few functions I want to add before I release it. I have managed to make it accept only numerous input and backspace, and if you write alphabetic letters, "Only numbers allowed" pops up.

    BUT when I leave one or more text-boxes empty and press calculate, it crashes. What I need is a popup that says I have to write something in the input fields. How should I do that?

    I also want to add some tooltips that pops up when I move the mouse cursor over the text fields. But again - how?

    Would be very nice if some of you could help me, but keep in mind that I'm completely new to this and it's my first day of programming something. So please try to explain in an "easy" way...

    Greets,
    Even

  2. #2
    Join Date
    Jun 2011
    Posts
    8

    Re: Newbie here - a few questions..

    Oh, forgot to post the picture of it.. here:


  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie here - a few questions..

    Sounds like an annoying design to me. How about this?

    1. Instead of showing a popup when the user enters invalid data (which is extremely annoying...), don't allow it to begin with. Use a masked textbox or handle the KeyPress event yourself and filter out invalid data.

    2. Simply disable the "Calculate" button until all data is valid. You can do this easily by updating the Enabled property from within an Application.Idle event handler (WinForms) or by binding the IsEnabled property of the button to a set of properties using a custom converter (WPF)

  4. #4
    Join Date
    Mar 2007
    Posts
    90

    Re: Newbie here - a few questions..

    Quote Originally Posted by Even92LN
    when I leave one or more text-boxes empty and press calculate, it crashes. What I need is a popup that says I have to write something in the input fields. How should I do that?
    Assuming you are using Visual Studio, try to use the debugger to find out what went wrong or attach the project to a message so we can take a look at the code. You will probably need to test for a non empty text box value using its Text (!= String.Empty) or Text.Length ( > 0) property before trying to test if it contains chars.

    Walkthrough: Debugging a Windows Form

    Visual Studio Debugging Tutorial

    Quote Originally Posted by Even92LN
    I also want to add some tooltips that pops up when I move the mouse cursor over the text fields. But again - how?
    Consider using the ToolTip Class.

  5. #5
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Newbie here - a few questions..

    I like using the numeric up/down control, as it forces numeric entry for you, you can set a larger increment so values will 'spin-up' faster for the user, and will let you assing a min and max value... one thing that annoys me about it however is that is doesnt take a default value if a user leaves it blank... but it gives a good starting point for numeric input in my opinion.

  6. #6
    Join Date
    Jun 2011
    Posts
    8

    Re: Newbie here - a few questions..

    Thanks for your replies! Will look futher into it tomorrow. Btw, here's what my file looks like now:

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication5
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void label2_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label3_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label5_Click(object sender, EventArgs e)
            {
    
            }
    
            private void label6_Click(object sender, EventArgs e)
            {
    
            }
    
            private void button1_Click(object sender, EventArgs e)
            {
                {
                    if (textBox2.Text == "")
                    {
                        MessageBox.Show("Only numbers allowed");
                    }
                    int dist = int.Parse(textBox1.Text);
                    int cAlt = int.Parse(textBox2.Text);
                    int tAlt = int.Parse(textBox3.Text);
                    int spd = int.Parse(textBox4.Text);
                    int vs;
                    vs = -((cAlt - tAlt) * spd) / (dist * 60);
    
                    textBox5.Text = vs.ToString();
                }
            }
            
    
            private void textBox2_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                textBox1.Text = "";
                textBox2.Text = "";
                textBox3.Text = "";
                textBox4.Text = "";
                textBox5.Text = "";
    
            }
    
            private void textBox5_TextChanged(object sender, EventArgs e)
            {
    
                textBox5.ReadOnly = true;
    
            }
    
            private void textBox1_TextChanged(object sender, EventArgs e)
            {
                
            }
    
            private void textBox3_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox4_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                { //accept only numbers and back space.
                    if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b'))
                    {
                        MessageBox.Show("Only numbers allowed");
                        e.Handled = true;
                    }
                    else
                    {
                        e.Handled = false;
                    }
    
                } 
    
                }
    
            private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
            {
                { //accept only numbers and back space.
                    if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b'))
                    {
                        MessageBox.Show("Only numbers allowed");
                        e.Handled = true;
                    }
                    else
                    {
                        e.Handled = false;
                    }
    
                } 
            }
    
            private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
            {
                { //accept only numbers and back space.
                    if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b'))
                    {
                        MessageBox.Show("Only numbers allowed");
                        e.Handled = true;
                    }
                    else
                    {
                        e.Handled = false;
                    }
    
                } 
            }
    
            private void textBox4_KeyPress(object sender, KeyPressEventArgs e)
            {
                { //accept only numbers and back space.
                    if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b'))
                    {
                        MessageBox.Show("Only numbers allowed");
                        e.Handled = true;
                    }
                    else
                    {
                        e.Handled = false;
                    }
    
                } 
            }
    
            private void chart1_Click(object sender, EventArgs e)
            {
    
            }
    
            private void toolTip1_Popup(object sender, PopupEventArgs e)
            {
                
    
            }
    
            private void label11_Click(object sender, EventArgs e)
            {
    
            }
    
            private void toolTip1_Popup_1(object sender, PopupEventArgs e)
            {
                ToolTip toolTip1 = new ToolTip();
                toolTip1.ShowAlways = true;
                toolTip1.SetToolTip(textBox1, "Click me to execute.");
    
            }
    
            private void toolTip1_Draw(object sender, DrawToolTipEventArgs e)
            {
    
            }
    
            private void label12_Click(object sender, EventArgs e)
            {
    
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
    
            private void toolTip1_Popup_2(object sender, PopupEventArgs e)
            {
    
            }
            }
        }

  7. #7
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie here - a few questions..

    If you are going to allow invalid data (which you shouldn't) then you should be using int.TryParse instead of int.Parse. Currently you only check for an empty string and this is why you are crashing (one reason anyway).

  8. #8
    Join Date
    Jun 2011
    Posts
    8

    Re: Newbie here - a few questions..

    Ok, I tried to do what BigEd suggested; to disable the caclulate button until the fields had a valid value. I can't say I understood much of what you wrote, BigEd (sorry, but I'm VERY new to this...) but I found this, which looked like a perfect coding for what I was trying to do: http://stackoverflow.com/questions/4...box-is-valid-c

    So I tried to use that and wrote this in my project file:


    Code:
            private void textBox6_TextChanged(object sender, EventArgs e)
            {
                this.button3.Enabled = !string.IsNullOrEmpty(this.textBox6.Text);
            }
    Here's a picture (I have added some new functions): (EDIT: I know, should be "pounds")

    textBox6 = the top text box
    button3 = the first calculate button

    No errors, but when I start the program the button is still enabled. BUT if I write something and clear it again, either using backspace or the "clear fields" button, it gets disabled. And when I write somtehing again it enables like it should. What can I do to fix this, so the button is disabled from the program startup?


    Thanks...
    Last edited by Even92LN; June 27th, 2011 at 04:42 PM.

  9. #9
    Join Date
    Jun 2011
    Location
    .NET4.0 / VS 2010
    Posts
    71

    Re: Newbie here - a few questions..

    You can disable the button 2 ways that I can think of. First one method is when you click the button in its properties there will be one called (Enabled) set it to false and it will start the application with the button disabled.

    The second method is clicking the form twice which will give you a form_load and inside this method you add button3.Enabled = false; This will run when the application loads and will disable your button at the beginning.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie here - a few questions..

    The problem is that the TextChanged event is not being fired initially.

    What I would do for a small WinForms project is like so:

    Code:
    public class Form1 : Form
    {
        public Form1()
        {
            Application.Idle += Application_Idle;
        }
       
        private void Application_Idle( object sender, EventArgs e )
        {
            button3.Enabled = !String.IsNullOrWhiteSpace(textBox6.Text);
        }
    }
    I would also recommend giving meaningful names to your controls

  11. #11
    Join Date
    Jun 2011
    Posts
    8

    Re: Newbie here - a few questions..

    Thanks a lot everyone, working well now!

    Have worked a little more on the application, and I must say I'm quite surprised by what I have achieved so far But I wonder how I can get the time format in "HH:MM" instead of "decimal-hours" in the "Scheduled flight time" textbox:


    This is the "Calculate" button at the moment:
    Code:
            private void calcTimeButton_Click(object sender, EventArgs e)
            {
                double fdist = int.Parse(FlightDist.Text);
                double aspd = int.Parse(GroundSpeed.Text);
                double ft;
    
                ft = Math.Round((fdist / aspd), 2);
    
                FlightTime.Text = ft.ToString();
    
            }
    
    // fdist = Flight distance
    // aspd = average ground speed
    // ft = scheduled Flight Time
    Last edited by Even92LN; July 11th, 2011 at 11:41 AM.

  12. #12
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Newbie here - a few questions..

    Got this from stackoverflow site... I didn't bother trying it or anything...

    Code:
    int hours = (int)hoursDecimal;
    decimal minutesDecimal = ((hoursDecimal - hours) * 60);
    int minutes = (int)minutesDecimal;
    int seconds = (int)((minutesDecimal - minutes) * 60);
    Once you have that, I would just format it using
    Code:
    String.Format("{0}:{1}.{2}", hours, minutes, seconds);

  13. #13
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Newbie here - a few questions..

    Just use the DateTime or TimeSpan class, let them do the work for you.

  14. #14
    Join Date
    May 2011
    Location
    Washington State
    Posts
    220

    Re: Newbie here - a few questions..

    Kudos BigEd... wasn't thinking TimeSpan had an option to take in the decimal form...

    Code:
                    TimeSpan ts = TimeSpan.FromHours(2.5);
                    Console.WriteLine(String.Format("{0}:{1}", ts.Hours, ts.Minutes));

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center