C#无法从对话框更改标签和按钮属性 [英] C# Can't change labels and button properties from a dialogbox

查看:51
本文介绍了C#无法从对话框更改标签和按钮属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的主窗体和一个从main调用的对话框.在我的主要表单中,我有一个标签和一个按钮,这些属性我无法更改.我正在使用Visual Studio 2015,不确定是否存在与此相关的错误.我还确保将我的标签和按钮设置为公开以进行修改.

I have my main form and a dialogbox which is called from main. In my main form I have a label and a button that which properties I can't change. I'm using Visual Studio 2015, not sure if there is a bug regarding this. I also made sure my label and button are set to public to modify.

代码:(这是从对话框中获得的,它具有一个列表框,该函数在selectindexchange时触发)

Code: (this is from the dialog box, this has a list box the function is triggered at selectindexchange)

else if ((short)lbDiscountTypes.SelectedValue == 2) //Senior
                {
                    frm_Main main = new frm_Main();
                    main.VAT = false;
                    main.labelStatus.Text = "NON-VAT (SENIOR)";
                    main.labelStatus.BackColor = System.Drawing.Color.IndianRed;
                    main.labelStatus.ForeColor = System.Drawing.Color.WhiteSmoke;
                    main.btnNonVat.Enabled = false;
                    main.btnNonVat.BackColor = System.Drawing.Color.SlateGray;
                    main.btnNonVat.ForeColor = System.Drawing.Color.Navy;
                    main.labelVatAmount.Text = 0.00m.ToString();
                    main.Dispose();

                    //INQUIRE DISCOUNT TYPES
                    var Discount = GC.CSHR_DiscountTypes.Where(Filter => Filter.DiscountCode == (short)lbDiscountTypes.SelectedValue);
                    decimal DP = 0.00m;
                    foreach (var item in Discount)
                    {
                        DP = item.DiscountPercentage;
                    }

                    foreach (var item in GC.CSHR_SORepo
                                        .Where(Filter => Filter.Machine == MACHINE
                                            && Filter.SalesOrderNum == SALESORDERNUM
                                            && Filter.First_SRP == Filter.IMFSRP))
                    {
                        item.DiscountAmount = (item.SoldSRP * DP) / 100;
                        item.TotalAmount = (item.Quantity * item.SoldSRP) - item.DiscountAmount;
                        item.VATableSalesOnTotalAmount = (item.Quantity * item.SoldSRP) - item.DiscountAmount;
                        item.VATRate = 0.00m;
                        GC.SaveChanges();
                    }

                    Close();
                }

//INQUIRE DISCOUNT TYPES 下面的代码效果很好,但最上面的代码却不行.我已经使用了调试模式来检查行是否没有被跳过,也没有被跳过.

The code below //INQUIRE DISCOUNT TYPES works well but not the one on top. I've used debug mode to check if the lines are not being skipped over and they aren't.

推荐答案

您应注意:

  • 您正在创建不需要的主表单新实例(在对话框后面打开时),因此您需要使其不创建新实例

  • You are creating a new instance of your main form that you don't need (while it is open behind the dialog), so you need to get it not create a new instance

您正在处置所创建的主表单. main.Dispose();

You are disposing the main form you created. main.Dispose();

实际上,您正在创建一个主窗体的新实例,并为这些控件分配值,然后进行处理.您希望在其上看到其更改的主窗体的实例和实例处于打开状态,并且在对话框后面未发生变化.

In fact you are creating a new instance of main form and assigning values to those controls and then dispose it. While and instance of yor main form that you expect to see changes on it, is open and untouched behind your dialog.

要设置这些控件的值,您可以执行以下其中一种方法:

To set value of those controls you can do one of these ways:

选项1

将您的labelStatus和btnNonVat公开.在设计器中打开您的主窗体,然后选择labelStatus和btnNonVat并在属性网格中,将 Modifier 设置为public.然后编写以下代码:

Make your labelStatus and btnNonVat public. Open your main form in designer and select labelStatus and btnNonVat and in property grid, set Modifier to public. Then write this code:

//var main = Application.OpenForms.OfType<frm_Main>().FirstOrDefault();
var main = (frm_Main)Application.OpenForms["frm_Main"];
main.labelStatus.Text = "NON-VAT (SENIOR)";
main.labelStatus.BackColor = System.Drawing.Color.IndianRed;
main.labelStatus.ForeColor = System.Drawing.Color.WhiteSmoke;
main.btnNonVat.Enabled = false;
main.btnNonVat.BackColor = System.Drawing.Color.SlateGray;
main.btnNonVat.ForeColor = System.Drawing.Color.Navy;
main.labelVatAmount.Text = 0.00m.ToString();

选项2

将frm_Main的实例传递到对话框并使用它.

Pass an instance of your frm_Main to your dialog and work with it.

选项3

关闭对话框后,使用对话框中的值并设置主表单的值

After closing the dialog, use values from dialog and set values of your main form

这篇关于C#无法从对话框更改标签和按钮属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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