将值从一种形式的方法转换为另一种形式
我在使用另一种形式的形式方法时遇到问题
我这样编码:
(我在textbox1中输入了123)
i have problem with using a method of a form in another form
i coded like this:
(i entered 123 in textbox1)
public class Form1 : Form
{
private string user_code;
public string UserCode
{
get { return user_code; }
}
public bool LoginUser()
{
user_code = null;
if(textBox1.Text==123){
user_code="usercode";
}
*
*/
}
}
和在form2中的用法:
and usage in form2:
Form1 form1 = new Form1();
form1.LoginUser();
MessageBox.Show(form1.UserName);
现在输出是一个空字符串,我用断点检查了一下,我发现当我在form2中调用loginuser时,textbox1中的值变为空,并且如果条件变为false
您如何看待问题?
TIA
now output is an empty string,i checked with breakpoint and i saw that when i call loginuser in form2 the value in textbox1 become empty and if condition become false
what do you think about problem??
TIA
在此处查看我的解决方案(解决方案2) ^ ],这是一种简便的方法.
Check out my solution here(solution 2) Need data back in form1 Textbox1 while coming back from form2[^] for an easy way of doing this.
我认为您需要回到基础知识! :laugh:
您不想这样做:
I think you need to go back to basics! :laugh:
You do not want to do this:
Form1 form1 = new Form1();
正如new
关键字建议的那样,它创建Form1的新实例,而不是访问具有已填充文本框的现有显示版本.您需要访问当前显示的现有版本,否则将无法获得其文本框内容!
这有点像您将手机放入您的汽车的杂物箱,然后惊讶地看着您我的汽车的杂物箱,发现它不是"在那里!
As the new
keyword suggests, it creates a new instance of Form1 rather than accessing the existing, displayed version which has the filled-in textBox. You need to access the existing version which is curently displayed, ort you will not be able to get at it''s textbox content!
It''s a bit like you putting your phone into the glove box of your car, then being surprised when you look in the glove box of my car to find it isn''t there!