需要另一个类中的文本框值
在我的课堂上,我定义了一些文本框,请参见下面的C#代码.用户必须填写他的用户名/密码,我想在另一个类中重复使用这些值.
所以我在上课的时候试过了:
Hi,
In my class I defined some textboxes, see C# code below. The user must fillin his username/password, I want to reuse those values in another class.
So I tried this in my initial class:
public string username {
get {
return this.textBoxUsername.Text;
}
set {
this.textBoxUsername.Text = value;
}
}
public UserControlStart(string test)
{
this.textBoxUsername.Text = xc;
}
在我想重用用户名/密码值的其他类中,我尝试了以下操作:
In my other class where I want to reuse the username/password value I tried this:
UserControlStart p1 = new UserControlStart();
MessageBox.Show(p1.username);
完整代码
Complete Code
using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;
namespace Form {
public partial class UserControlStart : UserControl {
public string xc, yc;
public UserControlStart() {
InitializeComponent();
}
public void buttonSignIn_Click(object sender, EventArgs e) {
if (textBoxUsername.Text.Length == 0) {
errorProvider.SetError(textBoxUsername, "Please enter your username");
return;
} else if (textBoxUsername.Text.Length > 0) {
errorProvider.Clear();
}
if (textBoxPassword.Text.Length == 0) {
errorProvider.SetError(textBoxPassword, "Please enter your password");
return;
} else if (textBoxPassword.Text.Length > 0) {
errorProvider.Clear();
}
this.Controls.Clear();
Start.ActiveForm.Size = new System.Drawing.Size(500, 560);
this.Controls.Add(new UserControlForm());
}
public string username {
get {
return this.textBoxUsername.Text;
}
set {
this.textBoxUsername.Text = value;
}
}
public UserControlStart(string test)
{
this.textBoxUsername.Text = xc;
}
}
}
我的问题是我没有在其他班级得到用户名和密码值>
预先感谢!
My issue is that I don''t get the username and password value in my other class
Thanks in advance!
问题是您没有显示该课程:
The problem is that you haven''t displayed the class:
UserControlStart p1 = new UserControlStart();
MessageBox.Show(p1.username);
所要做的只是构造控件的实例,并尝试从中显示属性.由于该控件实际上不会显示给用户,因此他无法填写任何详细信息.
相反,您可能已经在设计器中的窗体上放置了控件的实例.按名称使用该实例,它应该可以工作.该代码可能是:
All that does is constructs an instance of the control, and try to display a property from it. Since the control is at no point actually displayed to the user, he can''t fill in any details.
Instead, you have probably put an instance of your control onto your form in the designer. Use that instance by name, and it should work. The code is probably:
MessageBox.Show(userControlStart1.username);
如果您使用的是标准Visual Studio名称,而没有使它们与您的程序更相关.
if you have used the standard Visual Studio names without making then more relevant to you program.
您好,会员7762422,
您可以在第二个类中创建一个公共变量和一个重载的构造函数,如下所示:
Hi Member 7762422,
You can create a public variable and an overloaded constructor in the second class like this :
public string Uname;
Other_Class (string UserName)
{
Uname = UserName
}
从初始类调用它时,您可以使用:
and when calling it from the initial class you can use :
Other_Class other = new Oter_Class(textBoxUsername.Text)
希望对您有所帮助,
:)
I hope this help,
:)
两件事.
1.在此代码中:
Two things.
1. In this code:
public UserControlStart(string test)
{
this.textBoxUsername.Text = xc;
}
您传递了字符串test
但从未使用过,那么为什么要传递它呢?如果您的代码中充满了冗余代码,则调试起来会更加困难.
2.您具有:
you pass in a string test
but never use it, so why pass it? If your code is full of redundant code, it makes it much harder to debug.
2. You have:
public string xc, yc;
它们应该是private
否 public
.我怀疑您在尝试使访问级别起作用的同时更改了访问级别,但是拥有公共成员是一个非常非常糟糕的主意.如果要从班级外部访问,请改用公共属性.
如果我正确解释了您要执行的操作,则希望用户登录,然后为您的应用程序显示另一个表单,我认为这就是您要使用以下代码执行的操作:
they should be private
NOT public
. I suspect that you changed the access level at some time whilst trying to get this to work but it is a very, very bad idea to have public members. Use public properties instead, if you want access from outside your class.
If I have correctly interpreted what you are trying to do you want the user to Login and then display another Form for your application and I think that this is what you are trying to do with this code:
this.Controls.Clear();
Start.ActiveForm.Size = new System.Drawing.Size(500, 560);
this.Controls.Add(new UserControlForm());
Start
和<big>Start</big>.ActiveForm.Size
一样是什么?什么是UserControlForm
?为什么要尝试将其插入UserControlStart
的Controls
集合中?
您在代码中引用了这些内容,而没有解释它们的含义和应做的事情,仍然希望人们能够诊断出您的问题并为您提供帮助.
从我能从您的代码中辨别出的是,您正在以完全错误的方式来处理此问题.通常的方法是拥有一个登录表单/对话框,显示该对话框并获取用户凭据,将其关闭,然后打开该应用程序的主表单.登录对话框和主窗体应该完全分开,不要像看起来那样纠结在一起.登录对话框应该处理与验证用户身份有关的所有事情,并且用户名和密码一经验证,就应立即丢弃.绝对不要在应用程序中传递它们(尤其是密码),因为这会使系统对黑客开放.
What is Start
as in <big>Start</big>.ActiveForm.Size
? What is UserControlForm
, and why on earth are you trying to insert it into the Controls
collection of your UserControlStart
?
You have references to these in your code without explaining what they are and what they are supposed to do and still expect people to be able to diagnose your problem and help you.
From what I am able to discern from your code you are approaching this in entirely the wrong way. The normal way would be to have a Login Form/Dialog, show that and get the users credentials, close it and then open the main Form for the application. The Login Dialog and the Main Form should be entirely separate not entangled as you seem to be doing. The Login Dialog should handle everything to do with authenticating the user, and the username and password should be discarded as soon as they are verified. They should NEVER, EVER be passed around in your application (particularly the password) as this leaves the system wide open to hacking.