0

In my code in POI class I want to implement some method, at first it will take data from .xls file.Then it will show city and population name in the datagridview table.Now from datagridview table it will take only the city name which will show in the 1st Combobox. Then if someone click on the 1st combobox item it will show some places in that city on the second combobox. However I wrote the code. Now I got puzzled how to call it from Form1.cs and how it will work, because at this moment it is not working at all.

My code for POI class is

public class POI
{
    Form f;
    public static ComboBox Combo_list1 = new ComboBox();
    public static ComboBox Combo_list2 = new ComboBox();
    public static DataGridView dataTable = new DataGridView();
    Dictionary<string, List<string>> poi = new Dictionary<string, List<string>>();

    public void List(Form f)
    {
        f = new Form();
        var startPath = Application.StartupPath;
        string folderName = Path.Combine(startPath, "POI_List");
        System.IO.Directory.CreateDirectory(folderName);
        string SavedfileName = "POI_list.json";
        var Saving_path = Path.Combine(folderName, SavedfileName);

        string fileName = "Zensus_Gemeinden_org.xlsx";
        var path = Path.Combine(startPath, fileName);

        String name = "Gemeinden_31.12.2011_Vergleich";
        String constr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" +
            path + ";Extended Properties='Excel 12.0 XML;HDR=YES;';";

        OleDbConnection con = new OleDbConnection(constr);
        OleDbCommand oconn = new OleDbCommand("Select [3] as City,[4] as Population, * From [" + name + "$D7:E11300] Where [4] > 10000", con);
        con.Open();

        OleDbDataAdapter sda = new OleDbDataAdapter(oconn);
        DataTable data = new DataTable();

        sda.Fill(data);
        dataTable.DataSource = data;



        for (int i = 0; i < data.Rows.Count; i++)
        {
            Combo_list1.Items.Add(data.Rows[i]["City"]);
        }
        string Place_Json = "Place_List:" + JsonConvert.SerializeObject(data, Formatting.Indented);
        File.WriteAllText(Saving_path, Place_Json);

        foreach(string line in File.ReadLines("POIList.txt"))
        {
            string[] parts = line.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            poi.Add(parts[0], new List<string>());
            poi[parts[0]] = new List<string>(parts.Skip(1));

        }

    }

    public void Combo_list_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (Combo_list1.SelectedItem != null)
        {
            string txt = Combo_list1.SelectedItem.ToString();
            if (poi.ContainsKey(txt))
            {
                List<string> points = poi[txt];
                Combo_list2.Items.Clear();
                Combo_list2.Items.AddRange(points.ToArray());

            }
        }
    }
}

I want to call both method in my form1.cs class. but it is not working. Form1.cs look like this-

public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
        POI.Combo_list1 = comboBox1;
        POI.dataTable = dataGridView1;
        //POI.List();
        //POI

    }

    private void button1_Click(object sender, EventArgs e)
    {

    }
 }

sorry for such a long code.Answer is greatly appreciate.

cmoha
  • 73
  • 1
  • 2
  • 10
Nowshin
  • 67
  • 1
  • 2
  • 11
  • I think you should investigate what the `static` modifier does. – Charles Mager Jan 10 '16 at 18:39
  • @CharlesMager if I use static then for poi.Add(parts[0], new List()); poi[parts[0]] = new List(parts.Skip(1)); I get an error. It cannot access Dictionary> poi = new Dictionary>(). it saysAn object reference is required for the non-static field, method, or property 'POIList.POI.poi' – Nowshin Jan 10 '16 at 18:43

2 Answers2

0

in the way your code is currently written you need to instantiate a POI object in order to call those methods. you could either do that or make those methods static.

gilmishal
  • 1,884
  • 1
  • 22
  • 37
  • I add POI object. after that first combobox is showing properly. But the second one is not changing and show an error .No overload for method 'Combo_list_SelectedIndexChanged' takes 0 arguments. @gilmishal – Nowshin Jan 10 '16 at 18:56
  • Combo_list_SelectedIndexChanged needs to receive two parameters a sender and an event arguments. It actually seems like an event callback, I am not sure what you are trying to do. Unless this methods needs to be a callback for an event, you can drop the parameters and it will work – gilmishal Jan 10 '16 at 19:02
  • @gilmshal I have added one picture. I actually want to change the second combobox item accoding to the first combobox value. In the first combobox I set the city name and second combobox I set locations name. But I want to know how to handle on object sender and event average in this case – Nowshin Jan 10 '16 at 19:22
0

POI.List() of course will not work beacuse POI isn't an static class. Instead you should initialize a POI class object

POI instaceObj = new POI();
instanceObj.List();

Also I think that you should improve your code. Hope this help.

Luis Carlos
  • 345
  • 3
  • 10
  • @Luis_Carlos I have modified my answer in the answer section. Please see it.Now the first combox is shoing properly.. but from the second it shows -No overload for method 'Combo_list_SelectedIndexChanged' takes 0 arguments – Nowshin Jan 10 '16 at 18:54