可编辑的ComboBox历史记录 [英] Editable ComboBox History
问题描述
请原谅我在WPF应用程序和C#中我是一个完整的NUB我已经完成了很多脚本,但这是我的第一个这类项目。我正在创建一个提供远程协助的简单应用程序,并且有一个框,用户可以输入主机名并单击连接,这样就可以了。我想使该主机名字段成为一个可编辑的ComboBox,它将记住即使在关闭应用程序并重新启动它之后输入的最后10个系统。我到处都看到这种类型的字段,但我不知道如何使它工作。我会包含一些我的代码,但到目前为止我没有尝试过的任何代码已接近工作。谢谢
这是我提出的代码我决定放弃xml并使用txt文件,因为我需要存储的数据不是多维的。
.xaml
------------------------ ---------------------------
< window x: class = WpfApplication3.MainWindow xmlns:x = #unknown >
xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml
Title = Window1 Height = 300宽度= 300 >
< grid>
< stackpanel>
< combobox x:name = tbHostname width = 120 iseditable = 真 />
< button x:name = btDisableSC content = Test1 verticalalignment = 顶部 click = btDisableSC_Click height = 20 grid.columnspan = 2 />
< / stackpanel >
< / grid >
< / window >
----------------------------- ----------------------
.cs
- -------------------------------------------------
使用系统;
使用 System.IO;
使用 System.Collections;
使用 System.Collections.Generic;
使用 System.Data;
使用 System.Drawing;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;
命名空间 WpfApplication3
{
/// < 摘要 >
/// MainWindow.xaml $ b的交互逻辑$ b /// < < span class =code-summarycomment> / summary >
public partial class MainWindow:Window
{
string strXMLDataSourceFLDR = Environment.GetEnvironmentVariable( APPDAT )+ \\MyApplicationFLDR;
string strSourceHostsTXT = Environment.GetEnvironmentVariable( APPDATA)+ \\ MyApplicationFLDR \\Hosts.TXT ;
string strHostName;
ArrayList alHosts = new ArrayList();
public MainWindow()
{
funcCheckHostDataSource();
funcReadHostsFile();
InitializeComponent();
funcPopulatetbHostname();
}
public void funcCheckHostDataSource()
{
if (!Directory.Exists(strXMLDataSourceFLDR))
{
Directory.CreateDirectory(strXMLDataSourceFLDR);
}
if (!File.Exists(strSourceHostsTXT))
{
使用(StreamWriter objSWFile = File.CreateText(strSourceHostsTXT))
{
objSWFile.Close();
}
}
}
public void funcReadHostsFile()
{
使用(StreamReader objSRFile = File.OpenText(strSourceHostsTXT))
{
string strReadLine = ;
while ((strReadLine = objSRFile.ReadLine())!= null )
{
alHosts.Add(strReadLine.ToLower());
}
objSRFile.Close();
}
}
public void funcPopulatetbHostname( )
{
tbHostname.ItemsSource = alHosts;
}
private void btDisableSC_Click( object sender,RoutedEventArgs e)
{
if (这 .tbHostname.Text == String .Empty)
{
MessageBox.Show( 请填写目标系统的主机名!);
}
else
{
strHostName = tbHostname.Text.Replace( \\, 跨度>);
funcUpdateCombobox1(strHostName);
}
}
public void funcUpdateCombobox1( string strNewHostName)
{
ArrayList alHostsNew = new ArrayList();
alHostsNew.Add(strNewHostName.ToLower());
foreach ( string item in alHosts)
{
int alHostsNewCount = alHostsNew.Count;
if (alHostsNewCount < = 14 )
{
if (strNewHostName!= item)
{
alHostsNew.Add(item);
}
}
}
使用(StreamWriter objSWFile = File.CreateText(strSourceHostsTXT))
{
foreach ( string item in alHostsNew)
{
objSWFile.WriteLine(item);
}
objSWFile.Close();
}
alHosts = alHostsNew;
tbHostname.ItemsSource = alHostsNew;
}
}
}
-------------------- -------------------------------
Please excuse me I am a total NUB when it comes to WPF applications and C# I have done a lot of scripts but this is my first project of this type. I am creating a simple application to offer remote assistance and have a box were the user can enter a hostname and click connect and that works just fine. I would like to make that hostname field an editable ComboBox that would remember the last 10 systems that were entered even after closing the application and re-launching it. I see this type of field all over the place but I am not sure how to make it work. I would have included some of my code but so far nothing I have tried has come close to working. Thanks
Here is the code that I came up with I decided to abandon xml and use a txt file instead as the data that I needed to store was not multidimensional.
.xaml
---------------------------------------------------
<window x:class="WpfApplication3.MainWindow" xmlns:x="#unknown"> xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <grid> <stackpanel> <combobox x:name="tbHostname" width="120" iseditable="True" /> <button x:name="btDisableSC" content=" Test1 " verticalalignment="Top" click="btDisableSC_Click" height="20" grid.columnspan="2" /> </stackpanel> </grid> </window>
---------------------------------------------------
.cs
---------------------------------------------------
using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; namespace WpfApplication3 { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { string strXMLDataSourceFLDR = Environment.GetEnvironmentVariable("APPDATA") + "\\MyApplicationFLDR"; string strSourceHostsTXT = Environment.GetEnvironmentVariable("APPDATA") + "\\MyApplicationFLDR\\Hosts.TXT"; string strHostName; ArrayList alHosts = new ArrayList(); public MainWindow() { funcCheckHostDataSource(); funcReadHostsFile(); InitializeComponent(); funcPopulatetbHostname(); } public void funcCheckHostDataSource() { if (!Directory.Exists(strXMLDataSourceFLDR)) { Directory.CreateDirectory(strXMLDataSourceFLDR); } if (!File.Exists(strSourceHostsTXT)) { using (StreamWriter objSWFile = File.CreateText(strSourceHostsTXT)) { objSWFile.Close(); } } } public void funcReadHostsFile() { using (StreamReader objSRFile = File.OpenText(strSourceHostsTXT)) { string strReadLine = ""; while ((strReadLine = objSRFile.ReadLine()) != null) { alHosts.Add(strReadLine.ToLower()); } objSRFile.Close(); } } public void funcPopulatetbHostname() { tbHostname.ItemsSource = alHosts; } private void btDisableSC_Click(object sender, RoutedEventArgs e) { if (this.tbHostname.Text == String.Empty) { MessageBox.Show("Please fill in the Hostname of the target System!"); } else { strHostName = tbHostname.Text.Replace("\\", ""); funcUpdateCombobox1(strHostName); } } public void funcUpdateCombobox1(string strNewHostName) { ArrayList alHostsNew = new ArrayList(); alHostsNew.Add(strNewHostName.ToLower()); foreach (string item in alHosts) { int alHostsNewCount = alHostsNew.Count; if (alHostsNewCount <= 14) { if (strNewHostName != item) { alHostsNew.Add(item); } } } using (StreamWriter objSWFile = File.CreateText(strSourceHostsTXT)) { foreach (string item in alHostsNew) { objSWFile.WriteLine(item); } objSWFile.Close(); } alHosts = alHostsNew; tbHostname.ItemsSource = alHostsNew; } } }
---------------------------------------------------
这篇关于可编辑的ComboBox历史记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!