山西念星科贸有限公司 2024-05-31 02:35 采纳率: 72%
浏览 17
已结题

在C#方法中使用了.Refresh();刷新了控件,但是不是最新值

问题遇到的现象和发生背景

在C#方法中使用了.Refresh();刷新了控件,但是不是最新值

######自定义TheMethodOfUsingAStack类中
public class TheMethodOfUsingAStack
{
    public (string, string, string) GetTheFormThatCallsTheMethod(int stackCount)
    {
        StackTrace stackTrace = new StackTrace();
        MethodBase informationForForms = stackTrace.GetFrame(stackCount).GetMethod();
        string formsName = informationForForms.DeclaringType.Name;
        string methodName = stackTrace.GetFrame(stackCount).GetMethod().ToString();
        StackFrame stackFrame = stackTrace.GetFrame(stackCount);
        MethodBase stackInForMation = stackFrame.GetMethod();
        string namespaceName = stackInForMation.DeclaringType.Namespace;
        return (formsName, methodName, namespaceName);
    }
    public Form GetTheFormOfTheRetrievalMethodAndObtainTheFormInstance(string formName, int stackCount)
    {
        var getTheReturnValueOfTheFormThatCallsTheMethod = this.GetTheFormThatCallsTheMethod(stackCount);
        string formNamespace = getTheReturnValueOfTheFormThatCallsTheMethod.Item3;
        formName = getTheReturnValueOfTheFormThatCallsTheMethod.Item1;
        string formReferenceName = formNamespace + "." + formName;
        Type formType = Type.GetType(formReferenceName);
        if (formType != null)
        {
            Form formInstance = (Form)Activator.CreateInstance(formType);
            formInstance.Refresh();
            return formInstance;
        }
        else
        {
            throw new ArgumentException("窗体名称有误,没有通过指定的的窗体名称找到对应的窗体");
        }

    }
    public Control GetTheControlOfTheFormCorrespondingToTheRetrievalMethodAndObtainTheControlInstance(string controlName, int stackCount)
    {
        var getTheReturnValueOfTheFormThatCallsTheMethod = this.GetTheFormThatCallsTheMethod(stackCount);
        string formName = getTheReturnValueOfTheFormThatCallsTheMethod.Item1;
        Form formInstance = this.GetTheFormOfTheRetrievalMethodAndObtainTheFormInstance(formName, stackCount + 1);
        Type formType = formInstance.GetType();
        FormAndControlOperation formAndControlOperation = new FormAndControlOperation();
        Control ControlInstance = ((Control)formAndControlOperation.GetControlByNameAndGetControlInstance(formInstance, controlName));
        ControlInstance.Refresh();
        if (ControlInstance == null)
        {
            throw new ArgumentException("没有通过指定的的控件称找到对应的控件");
        }
        else
        {
            return ControlInstance;
        }
    }
    public string GetTheControlForCallingTheMethodAndObtainTheValueOfTheControlProperty(string controlName, string propertyName, int stackCount)
    {
        Control control = this.GetTheControlOfTheFormCorrespondingToTheRetrievalMethodAndObtainTheControlInstance(controlName, stackCount);
        control.Refresh();
        Type controlType = control.GetType();
        PropertyInfo controlPropertyInformation = controlType.GetProperty(propertyName);
        if (controlPropertyInformation != null)
        {
            string theSpecifiedWindowControlAttributeValuea = controlPropertyInformation.GetValue(control, null).ToString();
            return theSpecifiedWindowControlAttributeValuea;
        }
        else
        {
            throw new ArgumentException("控件名称或者控件方法有误,调取不到该控件");
        }
    }
}
######自定义窗体TheMethodOfUsingAStack类中
public partial class revenueWindow : Form
{
    public revenueWindow()
    {
        InitializeComponent();
    }
    private void revenueWindow_Load(object sender, EventArgs e)
    {
    }
   
    private void incomeAmountTextBox_TextChanged(object sender, EventArgs e)
    {
        TheMethodOfUsingAStack theMethodOfUsingAStack = new TheMethodOfUsingAStack();
        string a = theMethodOfUsingAStack.GetTheControlForCallingTheMethodAndObtainTheValueOfTheControlProperty("incomeAmountTextBox", "Text", 3);
        MessageBox.Show(a);//a值永远是创建后的初始值,不是最新的

    }
}
运行结果
          自定义窗体TheMethodOfUsingAStack类中

MessageBox.Show(a);//a值永远是创建后的初始值,不是最新的

我想要达到的结果
          自定义窗体TheMethodOfUsingAStack类中

MessageBox.Show(a);//a值是最新的值

  • 写回答

7条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2024-05-31 02:53
    关注

    🕝凌晨好,题主!目前:🔴[离线]
    📢参考通义千问、由郭老师整理提供!
    📣今日诗号:尽挹西江沧溟,步行云,青虹影深。扣舷独舒啸,肝胆冰雪,了无尘心。

    问题分析:

    您遇到的问题是,虽然您使用了 Control.Refresh() 方法尝试刷新控件,但在 MessageBox.Show(a) 中获取到的 a 值仍然是控件的初始值,而不是用户输入的最新值。

    这主要是因为您在 GetTheControlForCallingTheMethodAndObtainTheValueOfTheControlProperty 方法中,每次都通过 Activator.CreateInstance(formType) 创建了一个新的窗体实例。这意味着您获取到的控件实例并不是实际显示在屏幕上的那个控件实例,而是新创建的、未经用户交互的实例,因此其属性值保持初始状态。

    解决方案:

    要解决这个问题,您需要获取当前显示的窗体和控件实例,而不是创建新的实例。

    您可以通过以下步骤修改您的代码:

    1. 修改 GetTheFormOfTheRetrievalMethodAndObtainTheFormInstance 方法:
    public Form GetTheFormOfTheRetrievalMethodAndObtainTheFormInstance(string formName, int stackCount)
    {
        // ... (保留获取命名空间和窗体名称的代码)
    
        // 获取当前应用程序域中所有打开的窗体
        FormCollection openForms = Application.OpenForms;
    
        // 查找指定名称的窗体
        foreach (Form form in openForms)
        {
            if (form.GetType().FullName == formReferenceName)
            {
                return form;
            }
        }
    
        // 如果未找到,则抛出异常
        throw new ArgumentException("窗体名称有误,没有通过指定的的窗体名称找到对应的窗体");
    }
    
    1. 修改 GetTheControlOfTheFormCorrespondingToTheRetrievalMethodAndObtainTheControlInstance 方法:
    public Control GetTheControlOfTheFormCorrespondingToTheRetrievalMethodAndObtainTheControlInstance(string controlName, int stackCount)
    {
        // ... (保留获取窗体实例的代码)
    
        // 在窗体中查找指定名称的控件
        Control controlInstance = formInstance.Controls.Find(controlName, true).FirstOrDefault();
    
        if (controlInstance == null)
        {
            throw new ArgumentException("没有通过指定的的控件称找到对应的控件");
        }
        else
        {
            return controlInstance;
        }
    }
    

    修改后的完整代码:

    public class TheMethodOfUsingAStack
    {
        // ... (其他方法保持不变)
    
        public Form GetTheFormOfTheRetrievalMethodAndObtainTheFormInstance(string formName, int stackCount)
        {
            // ... (获取命名空间和窗体名称)
    
            FormCollection openForms = Application.OpenForms;
            foreach (Form form in openForms)
            {
                if (form.GetType().FullName == formReferenceName)
                {
                    return form;
                }
            }
    
            throw new ArgumentException("窗体名称有误,没有通过指定的的窗体名称找到对应的窗体");
        }
    
        public Control GetTheControlOfTheFormCorrespondingToTheRetrievalMethodAndObtainTheControlInstance(string controlName, int stackCount)
        {
            // ... (获取窗体实例)
    
            Control controlInstance = formInstance.Controls.Find(controlName, true).FirstOrDefault();
    
            if (controlInstance == null)
            {
                throw new ArgumentException("没有通过指定的的控件称找到对应的控件");
            }
            else
            {
                return controlInstance;
            }
        }
    }
    

    通过以上修改,您就可以在 incomeAmountTextBox_TextChanged 事件处理程序中获取到 incomeAmountTextBox 控件的最新值了。

    其他建议:

    • 尽量避免在事件处理程序中创建新的窗体实例,尤其是在需要访问当前窗体或控件的情况下。
    • 您可以考虑使用更直接的方式来访问控件属性,例如在 revenueWindow 类中添加一个公共属性来获取 incomeAmountTextBox.Text 的值。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 6月6日
  • 已采纳回答 5月31日
  • 赞助了问题酬金15元 5月31日
  • 修改了问题 5月31日
  • 展开全部

悬赏问题

  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红
  • ¥15 想问问富文本拿到的html怎么转成docx的
  • ¥15 我看了您的文章,遇到了个问题。