Click here to Skip to main content
15,880,392 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted
Comments
Sergey Alexandrovich Kryukov 17-Jan-15 18:30pm    
It doesn't look as a question, and isn't clear. As all combo boxes, WPF combo box consists of list box and text box parts; you can put there whatever you want. What do you call "history" here, editable of not? If you want, you can treat the set of combo box items as history of the text box content, but technically this is still a set of some items. What's the problem, exactly?
Also, don't mix up UI and data presented in UI. The object which "would remember last 10 systems" is data, it may or may not be a part of a UI control...
—SA
Member 11382477 18-Jan-15 0:22am    
Thanks for the response. I guess what I am asking for is someone to help me with the code or to point me in the right direction to a demo or article that may help me as I am still learning. What I am looking for is way to store/cache systems names into a dropdown list as they are entered by the user so that they can be retrieved the next time the user runs the application. Like for example the Microsoft Remote Desktop client or the address bar in Internet Explorer. For a little more information: The application does not get installed but instead is just an .exe that the user can run so I cannot use a database. What I was thinking of using was an .XML file created and stored in the users AppData\Roaming folder. Every time the user enters a name in the hostname field and then presses an action button the application would check the .XML file to see if the name already exists and if not add it and update the dropdown in the ComboBox. If an .XML file or a ComboBox is not the correct way of doing this please let me know.
Kornfeld Eliyahu Peter 18-Jan-15 2:03am    
Do you have any DB for that application? If do, save values there...If not you may store it in the configuration file...

1 solution

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
---------------------------------------------------
C#
<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
---------------------------------------------------
C#
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;
        }
    }
}

---------------------------------------------------
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900