![]() |
|
[C#] Use a listBox from a Class. - Printable Version +- LCKB (https://lckb.dev/forum) +-- Forum: ** OLD LCKB DATABASE ** (https://lckb.dev/forum/forumdisplay.php?fid=109) +--- Forum: Programmers Gateway (https://lckb.dev/forum/forumdisplay.php?fid=196) +---- Forum: Coders Talk (https://lckb.dev/forum/forumdisplay.php?fid=192) +---- Thread: [C#] Use a listBox from a Class. (/showthread.php?tid=1035) Pages:
1
2
|
- Nikolee - 08-20-2012 Hey guys, im trying to make a Class for the MySql Commands. It worked but i have one Problem, how can i fill a listBox from a Class? Someone send me a Code with a List but i dont understand it. public List<string>[] Select() { Form1 f1 = new Form1(); string query = "SELECT * FROM `t_magic` `a_name` WHERE `a_index` = 48"; List<string>[] list = new List<string>[3]; list[0] = new List<string>(); list[1] = new List<string>(); list[2] = new List<string>(); if (this.OpenConnection() == true) { DataTable datatable = new DataTable(); MySqlCommand cmd = new MySqlCommand(query, connection); MySqlDataReader dataReader = cmd.ExecuteReader(); this.connection.Close(); return list; } else { return list; } } I wanna do it like this, but this is from the MainForm not from a Class. DataTable datatable = new DataTable(); String sql1 = "SELECT * FROM t_characters a_index WHERE a_nick =" + textBox1.Text; MySqlDataAdapter dataAdapter = new MySqlDataAdapter(sql1, connection); dataAdapter.Fill(datatable); foreach (DataRow row in datatable.Rows) { ListViewItem item = new ListViewItem( new string[] { row["a_index"].ToString()}); listView1.Items.Add(item); } - HateMe - 08-20-2012 set the listbox as public , protected Internal or Internal then u can call the listbox from a other class like this: for example public static void test() { string myitem = "this is a test"; Form1 a = new Form1(); a.listBox_online_list.Items.Add(myitem); } - Nikolee - 08-20-2012 I did it now like your Example to see if it work, but there is always the same problem i dont get it: "Magic_Editor.Form1.listBox1_SelectedIndexChanged(object, System.EventArgs)" ist "Methode" und im angegebenen Kontext nicht gültig. i made the listBox public.. - Wizatek - 08-20-2012 Its weird to manage gui stuff from within another class. What i would do is make the function return what u need to have, and populate the listbox from within the Form1 class - Nikolee - 08-20-2012 public List<string>[] Select() { string query = "SELECT * FROM `t_magic` `a_name` WHERE `a_index` = 48"; List<string>[] list = new List<string>[3]; list[0] = new List<string>(); list[1] = new List<string>(); list[2] = new List<string>(); if (this.OpenConnection() == true) { MySqlCommand cmd = new MySqlCommand(query, connection); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { list[0].Add(dataReader["a_index"] + ""); list[1].Add(dataReader["a_name"] + ""); list[2].Add(dataReader["a_type"] + ""); } dataReader.Close(); this.connection.Close(); return list; } else { return list; } } So but it dont return anything // i tried it so too: Form1 f1 = new Form1(); f1.listBox1.Items.Add("Worked"); But that too dont work lol and thats the simplest way, what i do wrong? - HateMe - 08-20-2012 well i never used Form objects in other classes always used it in the Form.cs and passed the info to the class where i wanted and i think thats the simplest way - Nikolee - 08-20-2012 private void button2_Click(object sender, EventArgs e) { MySql1 f1 = new MySql1(); f1.Select(); listBox1.Items.Add(f1.Select()); } That work now only need to try out how to add these while (dataReader.Read()) { list[0].Add(dataReader["a_index"] + ""); list[1].Add(dataReader["a_name"] + ""); list[2].Add(dataReader["a_type"] + ""); } - Wizatek - 08-20-2012 Why not do it like this struct sData { public int index; public string name; public int type; public sData( int index, string name, int type) { this.index = index; this.name = name; this.type = type; } } class MySql1 { public List<sData> Select() { string query = "SELECT * FROM `t_magic` `a_name` WHERE `a_index` = 48"; List<sData> list = new List<sData>(); if (this.OpenConnection() == true) { MySqlCommand cmd = new MySqlCommand(query, connection); MySqlDataReader dataReader = cmd.ExecuteReader(); while (dataReader.Read()) { sData data = new sData ( int.parse(dataReader["a_index"]), dataReader["a_name"], int.parse(dataReader["a_type"]) ); list.Add(data); } dataReader.Close(); this.connection.Close(); return list; } else { return list; } } } hmm sorry, for some reason it don't let me copy/paste tabs >. - someone - 08-20-2012 Let me understand you want to create a new form(window) and add items to the list. Here is a simple example how you can controll another class, from another class. //the form class public partial class Form1 : Form { private MyClass mc = new MyClass(); public Form1() { InitializeComponent(); mc.setup(new object[] { this }); } private void button1_Click(object sender, EventArgs e) { mc.Add(); } //interface method from the window form public void addTextLine(string msg) { listBox1.Items.Add(msg); } } //my class class MyClass { //an instance to the form class public Form1 windowsForm; //get the data from yje form class public void setup(object[] args) { if (args != null) { windowsForm = (Form1)args[0]; } } //methods to access the form class public void Add() { windowsForm.addTextLine("stuff"); } } - Nikolee - 08-20-2012 Let me understand you want to create a new form(window) and add items to the list. Here is a simple example how you can controll another class, from another class. //the form class public partial class Form1 : Form { private MyClass mc = new MyClass(); public Form1() { InitializeComponent(); mc.setup(new object[] { this }); } private void button1_Click(object sender, EventArgs e) { mc.Add(); } //interface method from the window form public void addTextLine(string msg) { listBox1.Items.Add(msg); } } //my class class MyClass { //an instance to the form class public Form1 windowsForm; //get the data from yje form class public void setup(object[] args) { if (args != null) { windowsForm = (Form1)args[0]; } } //methods to access the form class public void Add() { windowsForm.addTextLine("stuff"); } } no, i made a Class "MySql1" for my Commands etc.. But now i wanna fill the listBox from Form1 .. and it wont work .. i try now wizas recommend |