Jump to content
The simFlight Network Forums

Recommended Posts

Posted

Hi there.  I'm very new to programming, but necessity is the mother of invention so I decided to tackle a project I've been wanting to do.  So far I've been pretty successful at cobbling together the beginnings of a  application based on all the great examples you've provided.  Thanks for that.

I'm doing it in C#.  I have been able to load all the nearest airports into a DataGrid from your "Finding Airports in Range" example.  If I understand correctly, it's pulling from the MakeRunways G5.csv.  In that file I'm not seeing any frequency data.  I do see it by type in the f4 and f5 csv's.  Ideally, I'd like to pull in ICAO, Name, City, and Distance, plus COM type and freq into the same grid. 

Is this possible?  If so, I'm stumped on the best way to do it.  Again, likely because I'm just new to this.  Any insight would be greatly appreciated!

Best regards!
Tony

Posted

Hi Tony,

The coms frequencies for airports are available in the FsAirport.COMFrequencies property. This is a collection of FsAirportComFrequency objects which contain the name, type and frequency of all the coms available at the airport.

Getting them into the grid is not as simple as the other properties (e.g. ICAO) as the grid doesn't know what to do with collections, so you'll need to use the grid in a more laborious way:

The full code is pasted below. It will work on a form with a button and a datagrid.

The basic idea is this: (Same numbers are used in code comments)

  1. Create a new class that will hold the data displayed in each row of the grid.  
  2. Setup the columns in the gridview control
  3. Set current location and get airports in range
  4. Create a new list to store all the new grid items:
  5. Create a datagrid item for each airport
  6. Go through com frequencies for the airport and add an extra grid row for each
    public partial class frmAirportsWithComs : Form
    {

        // 1. Create a new class that will hold the data displayed in each row of the grid.
        private class DataGridItem
        {
            public string ICAO { get; set; }
            public string Name { get; set; }
            public string City { get; set; }
            public string Distance { get; set; }
            public string Bearing { get; set; }
            public string Coms { get; set; }
        }

        public frmAirportsWithComs()
        {
            InitializeComponent();
            FSUIPCConnection.Open();
            configureGrid();
            FSUIPCConnection.AirportsDatabase.Load();
        }

        // 2. Setup the columns in the gridview control
        private void configureGrid()
        {
            this.dataGridView1.AutoGenerateColumns = false;
            DataGridViewColumnCollection cols = this.dataGridView1.Columns;
            cols.Add(createColumn("ICAO", 60, null));
            cols.Add(createColumn("Name", 120, null));
            cols.Add(createColumn("City", 120, null));
            cols.Add(createColumn("Distance", 60, null));
            cols.Add(createColumn("Bearing", 60, null));
            cols.Add(createColumn("Coms", 120, null));
        }

        private DataGridViewColumn createColumn(string propertyName, int width, string format)
        {
            DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
            col.DataPropertyName = propertyName;
            col.HeaderText = propertyName;
            col.Name = propertyName;
            col.ReadOnly = true;
            col.Resizable = DataGridViewTriState.True;
            col.Width = width;
            if (!string.IsNullOrWhiteSpace(format))
            {
                col.DefaultCellStyle.Format = format;
                col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
            }
            return col;
        }

        private void btnLoad_Click(object sender, EventArgs e)
        {
            // 3. Set current location and get airports in range
            FSUIPCConnection.AirportsDatabase.SetReferenceLocation();
            FsAirportCollection airports = FSUIPCConnection.AirportsDatabase.Airports;
            airports = airports.InRangeOfNauticalMiles(40);
            airports = airports.WithRunway(rw => rw.LengthFeet > 100);
            
            // Now we have the airports we need

            // 4. Create a new list to store all the new grid items:
            List<DataGridItem> gridItems = new List<DataGridItem>();
            
            // 5. create a datagrid item for each airport
            foreach (FsAirport ap in airports)
            {
                // Create new grid item and add the list
                DataGridItem item = new DataGridItem();
                gridItems.Add(item);
                
                // Set the basic properties
                item.ICAO = ap.ICAO;
                item.Name = ap.Name;
                item.City = ap.City;
                item.Distance = ap.DistanceNauticalMiles.ToString("F2");
                item.Bearing = ap.BearingToMagnetic.ToString("000");

                // 6. Go through com frequencies for the airport and add an extra grid row for each
                for (int i = 0; i < ap.COMFrequencies.Count; i++)
                {
                    // get the next frequency for the airport
                    FsAirportComFrequency freq = ap.COMFrequencies[i];
                    // if this is not the first com frequency then make a new grid item so it appears on a new row
                    if (i > 0)
                    {
                        item = new DataGridItem();
                        gridItems.Add(item);
                    }
                    // Add the frequency information
                    item.Coms = freq.ComType.ToString() + ":" + freq.Frequency;
                }
            }

            // Display the grid items
            this.dataGridView1.DataSource = gridItems;
        }
    }

Paul

  • Like 1
Posted

Paul, wow, thank you!!  It's going to take me some time to dissect and absorb how you did it, but I think I get the basic gist.  Loop through the airports database and for each airport, pass in the airport to the FsAirport.COMFreqencies property to get all the frequencies at that airport, loop through those frequencies and add them to the datagrid, then move on to the next airport.

I did see the FsAirport.COMFrequencies property, and knew I could get all the frequencies for an airport, just wasn't sure if/how I bring the two together.  I'm glad there is a way!

Ultimately, I'd like to turn this into a real-time update, say every 5 seconds or so, so the app is continually displaying all available frequencies within a specified distance from the player.  Would it make more sense performance-wise to build the list just once, store it in memory (an array, maybe?), then call that versus rebuild the grid every time?

Again, thank you!

Posted

For regular updates you just need to keep calling the code I've put in btnLoad_Click(). That can all go in your timer tick() function (or a separate method that you call from tick()). All other code is configuration and can just be done once.

The time taken to build a new list for the datagrid is negligible (<1 ms). If you keep the list in memory and try to update it with the new information it's going to be far more trouble than it's worth.

Paul

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.