使用C#编写一些代码的帮助,以在表单之间使用信息. [英] Help with writing some code in C# To use information between forms.
问题描述
我只会用语言说出来.
如果选中了form2上的复选框,并且最大化了form1.
在form1上,执行:
I''ll just say it in words.
If checkbox on form2 is checked and form1 is maximized.
on form1, do:
if (shown == true)
{
telep.Location = new Point(0, 26);
telep.Size += new System.Drawing.Size(0, 80);
shown = false;
}
else if (shown == false)
{
shown = false;
}
否则,什么也不做.
**显示的是布尔值.
otherwise, do nothing.
**Shown is a Boolean value.
推荐答案
什么,基本逻辑有问题?
这什么都不做:
What, problems with elementary logic?
This does nothing:
if (shown == false) {
shown = false;
}
if (shown == true)
和if (shown == false)
都是荒谬的:您在拥有布尔值的同时创建布尔值;同样,当您检查第一个条件时,再次检查"else if
"是荒谬的,因为布尔变量只有两个值.这两个问题似乎使您无法理解.
重要说明:
可为空类型bool?
的变量具有三个值:null
,false
和true
.在这种情况下,您的代码将具有一定的意义.
您应该使用
Both if (shown == true)
and if (shown == false)
are ridiculous: you create Boolean value while you already have Boolean value; also, when you checked first condition, checking with "else if
" again is ridiculous, as the Boolean variable has only two values. Both problems look like you fail to understand it.
Important note:
The variable of the nullable type bool?
has three values: null
, false
and true
. You code would have some sense in this case.
You should use
if (shown)
DoSomething();
else
DoSomethingElse();
但是,对于您的情况,"else
"部分没有任何意义,正如我在第一个代码示例中已经解释的那样.
同样,您不显示声明"shown
"的位置;它应该是表单字段成员.
剩下的问题是在片段中硬编码的立即大小/位置常量.这不好.此外,您可能还想在表单未显示时更改大小/位置.这样,您要做的就是:
However, the "else
" part makes no sense in your case as I already explained in my first code sample.
Also, you don''t show where "shown
" is declared; it should be a form field member.
The remaining problem is immediate size/location constants hard-coded in your fragment; this is bad. Besides, you probably wanted to change the size/location when the form is not yet shown. In this way, what you had to do would be this:
if (!shown) {
shown = false;
//change location/size here
}
//nothing else here
最后:问题看起来微不足道;但这很简单,但是我碰巧为首次"问题提供了一种非常通用的解决方案,主要是为了娱乐和演示一些相当先进的技术:
希望您在这里……只有一次 [
Finally: the problem looks trivial; and it is trivial, however, I happened to offer a very universal solution to the "first time" problem, mostly for fun and demonstration of some quite advanced techniques:
Wish You Were Here… Only Once[^]
这篇关于使用C#编写一些代码的帮助,以在表单之间使用信息.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!