计算WindowsForm应用程序中的距离....... [英] Calculating Distance in WindowsForm application.......

查看:67
本文介绍了计算WindowsForm应用程序中的距离.......的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我是C#的新程序员。



我写了一个代码在WindowsForm应用程序,但它不起作用。

任何人都可以帮我解决它有什么问题。



这里是我的书面代码:

 使用 System.Collections.Generic; 
使用 System.ComponentModel;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Windows.Forms;

命名空间 WindowsFormBasic2
{
public partial class Form1:Form
{
public Form1()
{
InitializeComponent();
}

private void calculate_Click( object sender,EventArgs e)
{
double x1 = double .Parse( this .x1.Text);
double y1 = double .Parse( this .y1.Text);
double x2 = double .Parse( this .x2.Text);
double y2 = double .Parse( this .y2.Text);

double 距离= Math.Sqrt((x1 - x2)*(x1 - x2)+(y1 - y2)*(y1 - Y2));
this .result.Text = distance.ToString();

}

private void result_Click(< span class =code-keyword> object
sender,EventArgs e)
{

}
}
}

解决方案

表达式写得正确,其他一切都可以......任何东西。例如,文本框中的字符串格式可能不正确,然后应该抛出异常。您可能需要考虑使用 TryParse 并显示输入错误。



另请注意,您应该更好地处理您的Forms应用程序中的异常正常。请看我过去的答案:如何制作一个在滚动条到达底部时停止的循环 [ ^ ]。



-SA


欢迎来到C#编程! ...我们都是新的一次:)



从你对谢尔盖的回复,这里显然是TextBox'x1中格式输入不正确的问题; 。如您所知,在不使用'TryParse的情况下,当您调用'Parse尝试获取任何转换为​​Double的任何入口TextBox中的字符串时,您只会出错。更多关于'下面的TryParse。



从我所看到的,如果四个TextBox有正确的数字格式条目,你的代码应运行并显示计算的斜边作为文本'结果控制的值(无法从你的代码中确切地知道控件的结果是什么类型)。



在你的代码中使用相同的名称对于TextBox和Type Double的变量。您的代码无错误编译的唯一原因是您使用this限定代码中TextBox名称的使用。



对对象和变量使用相同的名称不是一件好事!在编程实践开始时,尝试使用助记符名称是一个非常好的主意,这些名称在以后查看代码时会传达对象或变量的含义和/或用法。使用长的,描述性的,可变的名称并不会花费太多。



关于如何命名对象有很多想法(几乎是哲学)和变量;你可以随时学习。匈牙利表示法非常受欢迎;它以Xerox PARC和(后来)微软程序员Charles Simonyi的国籍命名:[ ^ ]。



现在,让我们关注'TryParse,很明显你需要添加才能拥有一个可以优雅处理的强大解决方案错误。



我认为掌握使用'TryParse'的巨大价值在于它需要你理解'out的使用'变量的修饰符,并且,希望还能理解通过引用传递变量的内容,使用'ref modifier for variables,是。



考虑这个函数:

  private   bool  getADouble(TextBox tbDoubleCandidate, ref   double  theDouble)
{
if Double .TryParse(tbDoubleCandidate.Text, out theDouble))
{
// 保证将theDouble设置为一个值,此处
return true ;
}
其他
{
// < span class =code-comment>在这种情况下不需要,但是良好做法
theDouble = Double .NaN;

MessageBox.Show( TextBox中输入错误: + tbDoubleCandidate。名称);

return false ;
}
}

它需要两个参数:一个TextBox,一个引用到一个Double类型的变量。



它返回一个布尔值,表示解析是否成功。如果解析失败,它会向用户显示一条消息,告诉他们哪个TextBox出现了错误的条目。



现在,我们来看看如何使用这个函数: pre lang =xml> if(!getADouble(tbX1,ref x1))return;
if(!getADouble(tbX2,ref x2))return;
if(!getADouble(tbY1,ref y1))return;
if(!getADouble(tbY2,ref y2))return;

这里我重命名了你的TextBoxes tbX1,tbX2,tbY1,tbY2。



请注意,每次调用'getADouble above的第二个参数前面都有术语'ref:这意味着我们将指针传递给变量本身,而不是传递变量。我们想要使用ref参数的 modified 值。



如果对'getADouble'的任何调用的结果都是假的:我们退出你的代码来计算斜边。



一般情况下:



1. out参数在使用时可以取消初始化:任何使用它的方法应该将其设置为某个值。 out参数使函数返回多个结果。



2. ref参数必须在使用前设置为某个值;在这种情况下:因为x1,x2,y1,y2都是值类型(Double)变量,它们都会自动初始化为0.0。



以上是非常简明的ref摘要,以及更好的概述:



1. ref:[ ^ ]



2. out:[ ^ ]



现在努力了解ref和out,我相信,对我们来说非常有帮助掌握C#和.NET FrameWork的更多高级功能。


错误是:FormatException未处理。输入字符串的格式不正确



来自这一行:

double x1 = double.Parse(this.x1.Text);


Hi Everyone

I'm a new programmer in C# .

I wrote a code in WindowsForm app but it doesn't work.
Can anybody help me what's wrong with it.

here's my written code:

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormBasic2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void calculate_Click(object sender, EventArgs e)
        {
            double x1 = double.Parse(this.x1.Text);
            double y1 = double.Parse(this.y1.Text);
            double x2 = double.Parse(this.x2.Text);
            double y2 = double.Parse(this.y2.Text);

            double distance = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            this.result.Text = distance.ToString();

        }

        private void result_Click(object sender, EventArgs e)
        {

        }
    }
}

解决方案

The expression is written correctly, and everything else could be… anything. For example, strings in text boxes could be in bad format, and then an exception should be thrown. You may want to consider using TryParse and show the input error instead.

Also note that you should better handle exceptions in your Forms application properly. Please see my past answer: How do i make a loop that will stop when a scrollbar reaches the bottom[^].

—SA


Welcome to C# Programming ! ... we were all "new" once :)

From your reply to Sergey, here, obviously there's a problem with an incorrect format entry in TextBox 'x1;. As you know, without using 'TryParse, you simply are going to have an error when you call 'Parse to try and get the string in any of your entry-TextBoxes converted to a Double. More on 'TryParse below.

From what I can see, if the four TextBoxes have correct numeric format entries, your code should run and display the calculated hypotenuse as the Text value of the 'result Control (can't tell, from your code, exactly what type of Control 'result is).

In your code you use the same names for both TextBoxes and variables of Type Double. The only reason your code compiles without error is that you qualify the use of the TextBox names in your code with "this."

Using the same names for objects and variables is not a good thing ! It's a very good idea, at the start of your programming practice, to try and use mnemonic names, names that, when you look at your code later, will convey the meaning and/or use of the objects or variables. It doesn't "cost" much more to use long, descriptive, variable names.

There are many ideas (almost philosophies, really) about how to name objects, and variables; you can always study those later. Hungarian notation has been very popular; it's named after the nationality of Xerox PARC and (later) Microsoft programmer, Charles Simonyi: [^].

Now, let's focus on 'TryParse, which it is clear you need to add in order to have a robust solution that can gracefully handle errors.

The great value in your mastering the use of 'TryParse is, I think, in the fact that it will require you to understand the use of the 'out modifier for variables, and, hopefully, also to understand what passing a variable by reference, using the 'ref modifier for variables, is.

Consider this function:

private bool getADouble(TextBox tbDoubleCandidate, ref double theDouble)
{
    if (Double.TryParse(tbDoubleCandidate.Text, out theDouble))
    {
        // theDouble is guaranteed to have been set to a value, here
        return true;
    }
    else
    {
        // not required in this case, but good practice
        theDouble = Double.NaN;

        MessageBox.Show("incorrect entry in TextBox: " + tbDoubleCandidate.Name);

        return false;
    }
}

It takes two parameters: a TextBox, and a reference to a variable of type Double.

It returns a boolean value signifying whether the parsing was successful, or not. If the parsing fails, it displays a message to the user telling them in which TextBox the incorrect entry appeared.

Now, let's look at how this function is used:

if (! getADouble(tbX1, ref x1) ) return;
if (! getADouble(tbX2, ref x2)) return;
if (! getADouble(tbY1, ref y1)) return;
if (! getADouble(tbY2, ref y2)) return;

Here I have renamed your TextBoxes tbX1, tbX2, tbY1, tbY2.

Notice that the second parameter in each call to 'getADouble above is preceded by the term 'ref: this means we are passing a pointer to the variable itself, not passing the value of the variable. We want to use the modified value of the ref parameter.

If the result of any of the calls to 'getADouble is false: we exit your code for calculating the hypotenuse.

In general:

1. an out parameter can be un-initialised at the time it is used: any method that uses it should set it to some value. out parameters enable a function to return more than one result.

2. a ref parameter must be set to some value before being used; in this case: because x1, x2, y1, y2 are all value type (Double) variables, they are all automatically initialised to 0.0.

The above is a very simplified summary of ref, and out, for a better overview:

1. ref: [^]

2. out: [^]

Effort on your part to understand ref and out now, will, I believe, be very helpful to you in mastering more advanced features of C# and the .NET FrameWork.


The error is : FormatException was unhandled."Input string was not in a correct format"

from this line:
double x1 = double.Parse(this.x1.Text);


这篇关于计算WindowsForm应用程序中的距离.......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆