winform設計一個登錄界面和修改密碼的界面-自動切換窗體(問題[已解] 望一起討論)(技術改變世界-cnblog)


需求

登錄界面:

1.要求 密碼 文本可以顯示和隱藏 字符 password屬性     

2.顯示輸入按鈕button

要求顯示輸入按鈕 按下去之后,實現 名字變成“取消”,取消之后密碼又是隱藏的

3.要求只能輸入錯誤 3次,3次之后自動退出

4.要求點擊修改密碼 切換到 修改密碼界面

 

修改密碼界面:

1.要求 新密碼 2次輸入一致,並且不能和舊密碼一樣

2.實現: 密碼不能為空 提示     新舊密碼一致 提示 自動清空     新密碼2次輸入不一致 提示並清空

3.利用 ComboBox 控件實現, 利用  清除 按鈕 清除 所在的行,(要求 如果不選擇 comboBox,清除 按鈕是不可使用的)

 

問題在最后:

登錄界面:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowsFormsApplication1;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int i = 3;
private void btnLogin_Click(object sender, EventArgs e)
{
if (i == 0)
{
this.Close();
}

if (txtUsername.Text.ToLower() != "admin")
{
MessageBox.Show("用戶名不存在");
i--;
return;
}
if (txtPassword.Text != "888888")
{
MessageBox.Show("密碼錯誤");
i--;
return;
}
else
{
MessageBox.Show("登錄成功","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
}

}

private void Form1_Load(object sender, EventArgs e)
{
//txtPassword.PasswordChar = '*';
txtPassword.UseSystemPasswordChar = true;
}

private void btnShow_Click(object sender, EventArgs e)
{
if (btnShow.Text== "顯示輸入")
{
txtPassword.UseSystemPasswordChar = false;
btnShow.Text = "取消";
return;
}
if (btnShow.Text == "取消")
{
txtPassword.UseSystemPasswordChar = true;
btnShow.Text = "顯示輸入";
return;
}
}

private void btnChangePassword_Click(object sender, EventArgs e)
{
WindowsFormsApplication1.Form1 newForm1 = new WindowsFormsApplication1.Form1();
newForm1.Show();//可以修改 前一個窗體
//newForm1.ShowDialog();//要想修改前一個窗體必須關閉這個窗體



}





}
}

修改密碼界面:

View Code
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


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

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
button3.Enabled = true;
}

private void Form1_Load(object sender, EventArgs e)
{

comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;

comboBox1.Items.Add("第一行");
comboBox1.Items.Add("第二行");
comboBox1.Items.Add("第三行");
//comboBox1.SelectedIndex = 0;
textBox1.UseSystemPasswordChar = true;
textBox2.UseSystemPasswordChar = true;
textBox3.UseSystemPasswordChar = true;
button3.Enabled = false;

}

private void button2_Click(object sender, EventArgs e)
{
this.Close();
}

private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == string.Empty || textBox2.Text == string.Empty || textBox3.Text == string.Empty)
{
MessageBox.Show("密碼不能為空");
return;
}
if (textBox1.Text != "888888")
{
MessageBox.Show("舊密碼不正確");
textBox1.Clear();
return;
}
if (textBox2.Text != textBox3.Text)
{
MessageBox.Show("2次新密碼不一致");
textBox2.Clear();
textBox3.Clear();
return;
}
if (textBox1.Text == textBox2.Text)
{
MessageBox.Show("新密碼和原密碼一樣");
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
}


}

private void button3_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex==0)
{
textBox1.Clear();
return;
}
if (comboBox1.SelectedIndex == 1)
{
textBox2.Clear();
return;
}
if (comboBox1.SelectedIndex == 2)
{
textBox3.Clear();
return;
}

}
}
}

 

問題1: show() 與 showDialog()的區別,這個我給出答案

答:show() 調用的時候,這個時候會出現 2個界面,登錄和修改界面,並且 用戶是可以修改父界面(登錄界面的)

      showDialog()調用,是不可以修改父界面的,必須關閉 當前的子界面,也就是修改密碼界面

 

問題2:

要實現 按“修改密碼”按鈕的時候的時候,調用修改密碼界面並且隱藏 登錄界面,當 ”修改密碼界面“ 確定或者取消的時候,自動彈出 登錄界面並且隱藏 修改密碼界面。

 

結論:對於 問題2

 

我以前的做法是,為了實現彈出窗口,我是 建立了 2個  解決方案,其實沒那個必要,在一個 解決方案里,生成多個Form1.cs即可

這樣類似於  多個類 繼承於 Form

 

感謝nfyz

對於登錄界面:

 private void btnChangePassword_Click(object sender, EventArgs e)
{
this.Visible = false;
            Form2 form2 = new Form2();
            form2.ShowDialog();
            this.Visible = true;
}
這樣即可,當點擊 修改密碼 按鈕就會隱藏 登錄界面,然后打開了 修改密碼界面,注意 這個時候 系統會停在這里,當你關閉 修改密碼界面之后,登錄界面又回來了。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM