Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
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);
}
Posts: 335
Threads: 22
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Aug 2011
Reputation:
0
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);
}
Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
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..
Posts: 768
Threads: 40
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: May 2011
Reputation:
0
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
Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
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?
Posts: 335
Threads: 22
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Aug 2011
Reputation:
0
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
Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
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"] + "");
}
Posts: 768
Threads: 40
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: May 2011
Reputation:
0
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 >.
Posts: 313
Threads: 20
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Jul 2011
Reputation:
0
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");
}
}
Posts: 528
Threads: 50
Thanks Received:
0 in 0 posts
Thanks Given: 0
Joined: Oct 2011
Reputation:
0
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