C#textBox记住内容

C#textBox记住内容,第1张

首先楼主肯定要把原先使用过的用户名和密码按照一定格式保存下来,否则Windows才不会帮你存呢。然后使用Textbox的AutoComplete功能(NET 20才有)就能实现,源代码如下: using System; using SystemCollectionsGeneric; using SystemComponentModel; using SystemData; using SystemDrawing; using SystemText; using SystemWindowsForms; using SystemIO; namespace AutoComplete { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private string accountFilePath = EnvironmentGetFolderPath(EnvironmentSpecialFolderApplicationData) "\\accountsdat"; private IDictionary accounts = new SortedList(); private void Form1_Load(object sender, EventArgs e) { ReloadAccounts(); } private void SaveButton_Click(object sender, EventArgs e) { if (accountsContainsKey(UsernameTextBoxText)) { accounts[UsernameTextBoxText] = PasswordTextBoxText; } else { accountsAdd(UsernameTextBoxText, PasswordTextBoxText); } using (StreamWriter writer = new StreamWriter(accountFilePath)) { foreach (KeyValuePair account in accounts) { writerWriteLine(accountKey); writerWriteLine(accountValue); } } ReloadAccounts(); } private void ReloadAccounts() { UsernameTextBoxAutoCompleteCustomSourceClear(); accountsClear(); if (!FileExists(accountFilePath)) { return; } using (StreamReader reader = new StreamReader(accountFilePath)) { while (true) { string username = readerReadLine(); if (stringIsNullOrEmpty(username)) break; string password = readerReadLine(); if (stringIsNullOrEmpty(password)) break; accountsAdd(username, password); UsernameTextBoxAutoCompleteCustomSourceAdd(username); } } } private void UsernameTextBox_Leave(object sender, EventArgs e) { string password = stringEmpty; if (accountsTryGetValue(UsernameTextBoxText, out password)) PasswordTextBoxText = password; } } }

Text1:直接写“Text1”的话其实是使用了它的默认属性 Text 属性的。其它控件也有默认属性,例如Command1的默认属性是Caption,默认属性即指在你不说明对那个属性进行 *** 作时就直接使用默认属性。
Text1Text:指的是它的Text属性,即“Text1Text”跟“Text1”两种写法是一样的,只是“Text1”未指出对那个属性进行 *** 作,所以使用它的默认Text属性而已。
Text1Value:在VB中,是没有这种写法的,即Text1是没有Value这种属性,也没有这种方法。这是错误语句。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/12897125.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-28
下一篇 2023-05-28

发表评论

登录后才能评论

评论列表(0条)

保存