我试图为我的文本框创建一个AutoCompletion。
尝试#1
string[] fileDB = Account.filedbContents;
string[] lines = { };
using (StreamReader sr = new StreamReader(@FILE_PATH)
while ((textboxWebsite.Text = sr.ReadLine()) != null)
{
lines.Add(sr.ReadLine());
}
尝试#2
textboxWebsite.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textboxWebsite.AutoCompleteSource = AutoCompleteSource.CustomSource;
var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange()
string[] substrings = new string[] { textboxWebsite.Text };
substrings.SelectMany(substring => Enumerable.Range(0, ))
string line = File.Read(@"C:\Users\snogueir\Desktop\Coding\sandbox\keychainarray.txt");
}
AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
collection.AddRange(arr);
this.textboxWebsite.AutoCompleteCustomSource = collection;
头部有点油炸。我试图在用户进入文本框时进行自动完成,它会尝试在网站中找到最相似的现有条目 .txt文件(想象一下在文本框中键入' face'会建议' facebook'如果它存在的话)
website1=username1=password1
website2=username2=password2
website3=username3=password3
这些都没有编译。
我正在考虑使用String.Split(' ='),但我能想到的唯一方法就是为字面上的每一行创建一个单独的数组,然后返回credentialarray [0] (因为那将是网站)。
帮助!
答案 0 :(得分:2)
如果您需要将网站,用户名和密码的组合存储在一起,我会创建一个可以存储这些项目的简单类,并且知道如何通过解析您的一个文件行来创建自己,如下所示:
class FileEntry
{
public string Website { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public static FileEntry Parse(string fileLine)
{
var result = new FileEntry();
if (string.IsNullOrWhiteSpace(fileLine)) return result;
var lineParts = fileLine.Split('=');
result.Website = lineParts[0];
if (lineParts.Length > 1) result.UserName = lineParts[1];
if (lineParts.Length > 2) result.Password = lineParts[2];
return result;
}
}
然后,当您解析文本文件时,您可以为每一行创建其中一个并将它们存储在List中。然后,您可以将每个列表项的Website
属性绑定到文本框的自动完成:
// Store the list at the class level so other methods can access it:
private List<FileEntry> fileEntries;
private void Form1_Load(object sender, EventArgs e)
{
var filePath = @"C:\Users\snogueir\Desktop\Coding\sandbox\keychainarray.txt";
fileEntries = File.ReadAllLines(filePath).Select(FileEntry.Parse).ToList();
textboxWebsite.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textboxWebsite.AutoCompleteSource = AutoCompleteSource.CustomSource;
var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(fileEntries.Select(fe => fe.Website).ToArray());
textboxWebsite.AutoCompleteCustomSource = autoComplete;
}
这样做的一个好处是,您现在可以使用一个列表来查找他们选择的网站的用户名和密码:
var userSelection = fileEntries.FirstOrDefault(fe =>
fe.Website.Equals(textboxWebsite.Text, StringComparison.OrdinalIgnoreCase));
var userName = userSelection?.UserName;
var password = userSelection?.Password;
答案 1 :(得分:1)
你走在正确的轨道string.split
是最简单的解决方案
但您的代码中存在错误。
var lines = from a in File.ReadLines(@"C:\Users\snogueir\Desktop\Coding\sandbox\keychainarray.txt")
let b = a.Split('=').FirstOrDefault()
select b;
textboxWebsite.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textboxWebsite.AutoCompleteSource = AutoCompleteSource.CustomSource;
var autoComplete = new AutoCompleteStringCollection();
autoComplete.AddRange(lines.ToArray());
textboxWebsite.AutoCompleteCustomSource = autoComplete;