问题遇到的现象和发生背景
在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值是最新的值