关于C#winforms程序设计的基本问题 [英] Elementary question about C# winforms program design

查看:56
本文介绍了关于C#winforms程序设计的基本问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,


来自另一个开发环境,似乎我在非常基本的问题上将我的

头撞到墙上,所以我希望你可以给我一个

推进正确的方向。


我工作的应用程序将包含

- a带有常用菜单和工具栏的mainform,而且不多。

- 一些带有各种编辑控件的模态数据编辑表单,一个

OK按钮和一个取消按钮

- 包含各种各种

类型的50个字段的数据类的实例。


我的问题与C#不存在这一事实有关不接受全局

变量,因此数据类不能直接在

名称空间中实例化。

在哪里以及如何你会实例化数据类,以便编辑

表格可以访问数据吗?


问候

笛卡尔

解决方案

9月8日,10:59 * am,Descartes < descart(AT)welho(DOT)编写:


来自另一个开发环境,看来我敲了我的

头非常基本的问题,所以我希望你能给我一个正确的方向推动。


我工作的应用程序将包括

- 一个带有常用菜单和工具栏的主变形,而且不多。

- 一些带有各种编辑控件的模态数据编辑表格,

OK按钮和取消按钮

- 包含各种

类型的50个字段的数据类实例。


我的问题与C#不接受全局

变量这一事实有关,因此数据类不能直接在

命名空间中实例化。 br />
您将在何处以及如何实例化数据类,以便编辑

表单可以访问数据?



很可能在主窗体中 - 如果它持有正在编辑的

实例的概念。然后在构建它们时将其传递给每个单独的编辑

表格。


Jon


Jon Skeet [C#MVP]写道:


很可能是主要形式 - 如果它持有

的概念;正在编辑的

实例。然后在构建它们时将其传递给每个单独的

编辑

表格。



Wau!这是一个快速的答案,Jon,谢谢。


让我再继续这一点了。

我想是在宣布一个数据类单独的文件(让我们把它称为MyData.cs)因为在字段之间会有相当多的getter和

setter和交互规则。只是保持它

与表格分开。

嗯,这当然不会阻止我实例化MyData

类主要形式。对吗?

MyData myData = new MyData;


然后,在构建它们时将myData传递给编辑表单我

会写

MyFirstDataEditor FirstDataEditor = new MyFirstDataEditor(myData)

,在那个构造函数中,我会用

编辑的数据填充控件。 br />
然后显示表格并获得DialogResult,如果FirstDataEditor.ShowDialog == DialogResult.OK

{
我会写


//

}

我应该如何(取决于DialogResult)存储或

丢弃编辑后的数据。

我认为这应该在编辑表格中处理,而不是在主要的

表格中处理。


问候

笛卡尔


9月8日,11:45 * am,Descartes < descart(AT)welho(DOT)comwrote:


< snip>


我应该如何(取决于在DialogResult上)商店或

丢弃编辑过的数据。

我认为这应该在编辑表格处理,而不是在主要处理

形式。



可能。当然,这在一定程度上取决于是否可能同时存在多个表格等等。等待变更很少很容易,而且它不是什么东西我有很多经验。它将取决于你如何进行绑定。

也取决于你是如何进行绑定的。如果你做每个对话框

只复制数据中的初始值,然后在退出时复制值

(在OK情况下但不取消)然后可能使生活

更容易。


Jon


Dear All,

Coming from another development environment, it appears that I bang my
head in the wall on very basic matters, so I hope you can give me a
push in the right direction.

The application that I work on will consist of
- a mainform with a usual menu and toolbar and not much more.
- a number of modal data editing forms with various edit controls, an
OK button and a Cancel button
- an instance of a data class containing some 50 fields of various
types.

My question relates to the fact that C# doesn''t accept "global"
variables, so the data class can not be instantiated directly in the
namespace.
Where and how would you instantiate the data class so that the editing
forms can access the data?

Regards
Descartes

解决方案

On Sep 8, 10:59*am, "Descartes" <descart (AT) welho (DOT) comwrote:

Coming from another development environment, it appears that I bang my
head in the wall on very basic matters, so I hope you can give me a
push in the right direction.

The application that I work on will consist of
- a mainform with a usual menu and toolbar and not much more.
- a number of modal data editing forms with various edit controls, an
OK button and a Cancel button
- an instance of a data class containing some 50 fields of various
types.

My question relates to the fact that C# doesn''t accept "global"
variables, so the data class can not be instantiated directly in the
namespace.
Where and how would you instantiate the data class so that the editing
forms can access the data?

Quite possibly in the main form - if it''s holding the concept of "the
instance being edited". Then pass it to each of the individual editing
forms as you construct them.

Jon


Jon Skeet [C# MVP] wrote:

Quite possibly in the main form - if it''s holding the concept of
"the
instance being edited". Then pass it to each of the individual
editing
forms as you construct them.

Wau! That was a fast answer, Jon, thank you.

Let me ride on this a little further.
I was thinkin of declaring the data class in a separate file (let''s
call it MyData.cs) because there will be quite a few getters and
setters and interaction rules between the fields. Just to keep it
separate from the forms.
Well, this of course doesn''t prevent me from instantiating the MyData
class in the main form. Right?
MyData myData = new MyData;

And then, to pass myData to the editing forms as I construct them I
would write
MyFirstDataEditor FirstDataEditor = new MyFirstDataEditor(myData)
and in that constructor I would populate the controls with data for
editing.
Then to display the form and get the DialogResult, I would write
if FirstDataEditor.ShowDialog == DialogResult.OK
{
//
}
How should I then (depending on the DialogResult) either store or
discard the edited data.
I think this should be handled in the editing form and not in the main
form.

Regards
Descartes


On Sep 8, 11:45*am, "Descartes" <descart (AT) welho (DOT) comwrote:

<snip>

How should I then (depending on the DialogResult) either store or
discard the edited data.
I think this should be handled in the editing form and not in the main
form.

Potentially. Of course, it partly depends on whether there might be
multiple forms up at the same time, etc. Discarding changes is rarely
easy, and it''s not something I have a lot of experience with. It will
also depend on how you''re doing the binding . If you make each dialog
only copy the initial values from the data, and then copy values back
on exit (in the OK situation but not Cancel) then that may make life
easier.

Jon


这篇关于关于C#winforms程序设计的基本问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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