Click here to Skip to main content
15,885,811 members
Home / Discussions / C#
   

C#

 
GeneralRe: Using sortedlist or sorteddictionary Pin
Henry Minute28-May-09 8:26
Henry Minute28-May-09 8:26 
GeneralRe: Using sortedlist or sorteddictionary Pin
michaelgr128-May-09 8:34
michaelgr128-May-09 8:34 
QuestionFile System C# Pin
sonya_rbi28-May-09 7:08
sonya_rbi28-May-09 7:08 
AnswerRe: File System C# Pin
DaveyM6928-May-09 7:28
professionalDaveyM6928-May-09 7:28 
AnswerRe: File System C# Pin
Luc Pattyn28-May-09 7:51
sitebuilderLuc Pattyn28-May-09 7:51 
QuestionAn elegant binding solution to getting the user to choose a COM port Pin
edwaugh28-May-09 7:02
edwaugh28-May-09 7:02 
AnswerRe: An elegant binding solution to getting the user to choose a COM port Pin
Ennis Ray Lynch, Jr.28-May-09 7:27
Ennis Ray Lynch, Jr.28-May-09 7:27 
AnswerRe: An elegant binding solution to getting the user to choose a COM port Pin
edwaugh28-May-09 9:30
edwaugh28-May-09 9:30 
Hi Ennis,

Thanks very much for your response. You are absolutely right about the SelectedItem thing I don't know how I didn't manage that already. Also, I quite like my comments they help me remember why I did stuff!

I guess it would be nice to have some cunning syntax that allowed you to bind the ComboBox directly to the string array from GetPortList() but I suppose it's not a big deal to do the copy really.

It's nice to know I am improving my c# so I have not made too many obvious style errors!

I'll post my final code below as I expect these posts hang around and someone else may find it useful.

Cheers

Ed

namespace Sensor_Controller_User_Interface
{
    /// <summary>
    /// Interaction logic for SerialSettingsWindow.xaml
    /// </summary>
    public partial class SerialSettingsWindow : Window
    {
        private string tempComPort;
        private int tempBaudRate;
        private System.Collections.ArrayList portList;

        /// <summary>
        /// A window dialog to get com port settings for the serial port interface.
        /// 
        /// I ended up implementing the two list boxes in different ways. The PortListBox needs to be
        /// updated with the com ports available on a specific machine using the GetPortList() method,
        /// I couldn't get data binding on the selected item to work when doing this so I have implemented
        /// it in code. This is not an especially big deal but perhaps not as elegant as it could be. The
        /// BaudRateList is just a data binding to a list of number strings in the XAML, this seems to work
        /// fine but maybe there is a way to test the selected COM port and create a list of baud rates that
        /// reflects what the adaptor supports.
        /// </summary>
        public SerialSettingsWindow()
        {
            InitializeComponent();
            
            // Create the ObservableCollection supporting ArrayList and fill it
            portList = new System.Collections.ArrayList(16);
            foreach (string port in System.IO.Ports.SerialPort.GetPortNames())
            {
                portList.Add(port);
            }

            // Record the current values of the properties in case we decide to cancel.
            // This is only important when the data binding is working as the properties
            // gets updated automatically so it has no impact on the ComPortList at the moment.
            tempComPort = Properties.Settings.Default.SerialInterfaceComPort;
            tempBaudRate = Properties.Settings.Default.SerialInterfaceBaudRate;

            // Set the ItemSource and get the SelectedIndex by searching the ArrayList
            comPortListBox.ItemsSource = portList;
            comPortListBox.SelectedItem = Properties.Settings.Default.SerialInterfaceComPort;

            // BaudRateList binds directly to the application properties file
            baudRateListBox.DataContext = Properties.Settings.Default;
        }


        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            // Reset the selected values
            Properties.Settings.Default.SerialInterfaceComPort = tempComPort;
            Properties.Settings.Default.SerialInterfaceBaudRate = tempBaudRate;
            Properties.Settings.Default.Save();
            this.Close();
        }

        private void doneButton_Click(object sender, RoutedEventArgs e)
        {
            // Manually update the properties value for the ComPort and Save
            Properties.Settings.Default.SerialInterfaceComPort = comPortListBox.SelectionBoxItem.ToString();
            Properties.Settings.Default.Save();
            this.Close();
        }
    }
}

Smile | :)
QuestionA read only CheckBox in WPF Pin
edwaugh28-May-09 6:46
edwaugh28-May-09 6:46 
Questioncreating a calendar Pin
benjamin yap28-May-09 6:23
benjamin yap28-May-09 6:23 
AnswerRe: creating a calendar Pin
dan!sh 28-May-09 6:35
professional dan!sh 28-May-09 6:35 
GeneralRe: creating a calendar Pin
benjamin yap28-May-09 6:40
benjamin yap28-May-09 6:40 
GeneralRe: creating a calendar Pin
dan!sh 28-May-09 7:02
professional dan!sh 28-May-09 7:02 
AnswerRe: creating a calendar Pin
led mike28-May-09 7:02
led mike28-May-09 7:02 
QuestionFull text Search Pin
ellllllllie28-May-09 6:10
ellllllllie28-May-09 6:10 
AnswerRe: Full text Search Pin
EliottA28-May-09 8:45
EliottA28-May-09 8:45 
QuestionNumber of DataGridViewRow's that have Visible == true Pin
Dan Neely28-May-09 5:55
Dan Neely28-May-09 5:55 
AnswerRe: Number of DataGridViewRow's that have Visible == true Pin
dan!sh 28-May-09 6:25
professional dan!sh 28-May-09 6:25 
GeneralRe: Number of DataGridViewRow's that have Visible == true Pin
Dan Neely28-May-09 7:05
Dan Neely28-May-09 7:05 
GeneralRe: Number of DataGridViewRow's that have Visible == true Pin
dan!sh 28-May-09 7:17
professional dan!sh 28-May-09 7:17 
GeneralRe: Number of DataGridViewRow's that have Visible == true Pin
Dan Neely28-May-09 7:25
Dan Neely28-May-09 7:25 
QuestionSending the button name to a method. Pin
outerhell28-May-09 4:52
outerhell28-May-09 4:52 
AnswerRe: Sending the button name to a method. Pin
Michael Bookatz28-May-09 4:54
Michael Bookatz28-May-09 4:54 
GeneralRe: Sending the button name to a method. Pin
outerhell28-May-09 5:05
outerhell28-May-09 5:05 
AnswerRe: Sending the button name to a method. Pin
Pete O'Hanlon28-May-09 4:55
mvePete O'Hanlon28-May-09 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.