簡體   English   中英

如何解決當前內容中不存在的“ myLogin”?

[英]How can i fix the 'myLogin' does not exist in the current content?

我一直在通過C#課程做運動,但是我沒有使它起作用,這是實際的作業:

創建一個名為“ LoginPasswordUserControl”的用戶控件。 “ LoginPasswordUserControl”包含一個顯示字符串“ Login:”的標簽(loginLabel),一個用戶輸入登錄名的文本框(loginTextBox),一個顯示字符串“ Password:”的標簽(passwordLabel),最后是一個文本框(passwordTextBox)用戶輸入密碼的位置(不要忘記在TextBox的“屬性”窗口中將屬性Passwordchar設置為“ *”)。

LoginPasswordUserControl必須提供公用的只讀屬性Login和Password,允許應用程序從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";
        }
    }
}

首先,注釋掉myLogin.Login代碼(您的代碼中帶有紅色波浪形)。

接下來,成功構建(編譯)項目。 然后,LoginPasswordUserControl將被添加到您的工具箱中。

在此處輸入圖片說明

接下來,將LoginPasswordUserControl從工具箱拖到窗體的設計圖面上。 它將被創建為loginPasswordUserControl1。

您可以通過在設計器中右鍵單擊新實例化的UserControl的名稱,選擇屬性,然后更改名稱。

在此處輸入圖片說明

在此處輸入圖片說明

那對我有用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM