如何从另一个表单设置按钮的属性 [英] How to set properties of the button from another form

查看:109
本文介绍了如何从另一个表单设置按钮的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我在网上寻找此但没有运气

我有两个表格(Form1,Form2)

Form1将包含Listbox1和Item(如Burger)按钮

Form2将包含用于设置Form1按钮的属性和值的文本框

示例

TextBox1.text应设置Button Text(汉堡的名称) )

TextBox2应设置按钮值(汉堡价格)

TextBox3应设置按钮图像



我需要在Form1上从Form2创建一个新按钮,当然我需要每当我​​打开Form2并需要创建新按钮



我是什么尝试过:



我想我需要数据表,因为我需要更新一次价格或图片

对于更新,我可以在Form2中使用ComboBox和项目列表(按钮)

所以基本上我知道如何在数据库中插入但是如何将其设置为Button,当然还有如何创建新的b来自Form2的utton



你能帮帮我吗

谢谢

解决方案

< BLOCKQUOTE>不要。当你开始访问另一个表单上的控件时,你将两个锁定在一起,你不能再修改一个而不考虑对另一个的影响 - 控件是私有的,有一个原因,这个原因是为了防止这种事情是明确的反对OOP的原则。



相反,Form2创建一个事件(或几个事件),当它想要更改Form1时它会引发,而Form1处理事件,使用Form2上的属性可以访问修改后的信息。

也许BurgerSelected,PriceChanged或类似的东西会不错?



这听起来很复杂,但它不是,不是真的 - 它使你的代码更具可读性和可维护性。

看看这个:在两种形式之间传递信息,第2部分:儿童到父母 [ ^ ] - 它包含一个例子。


按钮就像任何其他控件一样。它们列在其父级的控件集合中。它们的属性可以在运行时设置。这就是设计者为其生成代码的原因。看看你的Form1.designer.cs文件。在那里你看到它是如何完成的。但是不要在那里更改代码,因为设计师的任何后续更改都会覆盖您的代码更改。


Hello i was looking on web for this but no luck
I have two Forms (Form1,Form2)
Form1 Will contain Listbox1 and Item (like Burger) Buttons
Form2 will contain textboxes to set a properties and values of Form1 buttons
Example
TextBox1.text should set Button Text (name of the burger)
TextBox2 should set button value (Price of the burger)
TextBox3 should set a Button Image

and i need to create a new button from Form2 on Form1 of course i will need that whenever i open the Form2 and need to create new button

What I have tried:

I imagine i will need data tables for this because i will need to update maybe once the price or image
For updates i can have ComboBox in Form2 with the list of Items (Buttons)
So basically I know how to Insert in database but how to set that to the Button and of course how to create new button from Form2

Can you help me with this
Thank You

解决方案

Don't. When you start accessing controls on a different form, you lock the two together and you can no longer modify one without considering the effects on the other - controls are private for a reason, and that reason is to prevent this kind of thing which is decidedly against the principles of OOPs.

Instead, Form2 creates an event (or several events) which it raises when it wants to change Form1, and Form1 handles the events, using Properties on Form2 to access teh revised information.
Perhaps "BurgerSelected", "PriceChanged" or similar would be good?

This sounds complex, but it isn't, not really - and it makes your code a lot more readable and maintainable.
Have a look at this: Transferring information between two forms, Part 2: Child to Parent[^] - it includes an example.


Buttons are just like any other control. They are listed in their parent's Controls collection. Their properties can be set at runtime. That's what the designer generates code for. Have a look at your Form1.designer.cs file. There you see how it's done. But don't change code there because any later change in designer will overwrite your change in code.


这篇关于如何从另一个表单设置按钮的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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