我如何修复“我的登录”'目前的内容中不存在?

时间:2017-09-06 18:18:30

标签: c#

我一直在从我的c#课程中进行锻炼,而且我没有让它工作,这是实际的任务:

创建名为" LoginPasswordUserControl"的用户控件。 " LoginPasswordUserControl" 包含一个标签(loginLabel),显示字符串"登录:",一个文本框(loginTextBox),其中 用户输入登录名,标签(passwordLabel),显示字符串"密码:", 最后,用户输入密码的文本框(passwordTextBox)(不要忘记设置 属性Passwordchar到" *"在TextBox的“属性”窗口中)。

LoginPasswordUserControl必须提供Public只读属性 允许应用程序检索用户输入的登录名和密码 来自loginTextBox和passwordTextBox。

这是LoginPasswordUserControl代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginApp
{
    public partial class LoginPasswordUserControl : UserControl
    {
        public LoginPasswordUserControl()
        {
            InitializeComponent();
        }

        public string Login
        {
            get
            {
                return loginTextBox.Text;
            }
        }
        public string Password
        {
            get
            {
                return passwordTextBox.Text;
            }
        }
    }
 }

以下是应用程序的代码,我收到错误消息: 名称' myLogin'在当前内容中不存在

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace UserControlTest
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void OK_btn_Click(object sender, EventArgs e)
    {

        if (myLogin.Login != "" && myLogin.Password != "")

        {

            displayLabel.Text = "The information entered is:";

            loginLabel.Text =
            "Login: " + myLogin.Login;

            passwordLabel.Text =
            "Password: " + myLogin.Password;

        } 
        else

            displayLabel.Text = "Enter information above";
        }
    }
}

1 个答案:

答案 0 :(得分:-2)

首先,注释掉myLogin.Login代码(代码中有红色波浪形)。

接下来,成功构建(编译)项目。然后将LoginPasswordUserControl添加到您的工具箱中。

enter image description here

接下来,将LoginPasswordUserControl从“工具箱”拖动到表单的设计图面。它将创建为loginPasswordUserControl1。

您可以通过在设计器中右键单击它来更改新实例化的UserControl的名称,选择属性并更改名称。

enter image description here

enter image description here

这对我有用。