我想確定用戶在註冊時是否單擊管理員或用戶單選按鈕。我想我應該將它追加到文本文件的名稱和密碼所在行的末尾,但我該怎麼做?下面是相關代碼:從布爾方法傳遞新的值字符串?
單選按鈕檢查
public bool radioButtons()
{
string usertypebutton;
if (!userButton.Checked && !adminButton.Checked)
{
MessageBox.Show("You must select an account type");
return false;
}
else
{
if (userButton.Checked)
{
usertypebutton = "User";
}
else
{
usertypebutton = "Admin";
}
return true;
}
}
的StreamWriter進行註冊:
public void mySW()
{
string path = @"C:\Other\myFile.txt";
string userName = userNameBox.Text;
string password = passwordBox.Text;
string usertype = usertypebutton;
using (StreamWriter writer = new StreamWriter(path, true))
{
writer.WriteLine("Username: {0} Password: {1} Type: {3}" , userName, password, usertype);
// No need to close nor dispose your StreamWriter.
// You're inside a using statement for that!
}
MessageBox.Show("Thanks for registering! \n\nYou may now log in!", "Registration SuccessFul");
Application.OpenForms[0].Show();
this.Close();
}
登錄:
private void logonButton_Click(object sender, EventArgs e)
{
// Loads your users storage
var users = File.ReadAllLines(@"C:\Other\myFile.txt");
// Creates the line with username + password
var usernamePassword = String.Format("Username: {0} Password: {1}", userNameBox.Text, passwordBox.Text);
// Locates the user on your storage
var userFound = users.SingleOrDefault(_u => _u.Equals(usernamePassword));
if (userFound != null)
{
MessageBox.Show("Welcome back, " + userNameBox.Text);
}
else
{
MessageBox.Show("Sorry, you have entered incorrect details\n\nPlease try again");
userNameBox.Text = "";
passwordBox.Text = "";
}
}
所以(我認爲)基本上我想將單選按鈕方法的值usertypebutton傳遞給SW。我將如何做,因爲我已經傳遞了一個布爾值?
安東尼
您提到您在您的帖子中整理了以前的問題。 1)這個問題無關緊要,而且浪費時間閱讀。 2)你從未真正接受過上一個問題的答案。如果你有答案,那麼你應該將其標記爲已接受。 –
sorted :)謝謝 –
所以任何人都可以幫忙嗎? –