C# Learning Woes

D

David Cuffee

I am a VB6 programmer learning C#. I probably would have been better off
being totally new to programming learning C# because of my knowledge of VB6
is totally making this harder for me to grasp.

What I am trying to do is I have a form (lets call it Form1) with a button
called Browse. When the Browse button is clicked it creates in instance of a
new window which has a ListBox on it (lets call it Form2). The ListBox is
loaded with the names of some tables in a databases. What i am trying to do
is once the user selects one of the tables and then clicks the OK button on
the window with the ListBox. Form2 would disappear. Then back on form1 there
is another button called AddColumns. When the add columns button is clicked
I want to do a query on the table that was selected from the Listbox on
form2. The problem is in C# I dont think there is a way to save the data
retrived from the Listbox on form2 and have the AddColumns click event to be
able to see that info anyway. There has to be a way to do this. In VB6 this
would be trival but with the way encapsulation is in C# I have not figured
out how to do it yet. HELP!

David
 
T

Tamir Khason

You can use public variables either in Form1 or Form2 to save your data. To
be sure that user clicked OK and not Cancel button on Form2 you will have to
open it as ShowDialog() wich returns what button was clicked
 
S

Shakir Hussain

David,

while coding in c#, you have to think little bit of VC++ way, and little bit
of VB way.

To your problem
(Step1)What I am trying to do is I have a form (lets call it Form1) with a
button called Browse.

C# code

Form1 pForm = new Form1(); //this form has browse button in it
pForm.ShowDialog();
(Step2)When the Browse button is clicked it creates in instance of a new
window which has a ListBox on it (lets call it Form2)

c# code

private void browseButton_click(object sender, System.EventArgs e)
{
//create form2 which has list box
Form2 pForm = new Form2();
if(pForm.ShowDialog() == DialogResult.OK)
{
//set the selected table name into the global variable
mStrTableName = pForm.SelectedTableName; //this is a user
defined property in form 2.
}
}
(Step3)once the user selects one of the tables and then clicks the OK
button on the window with the ListBox. Form2 would disapp

c# code inside form2

//define a global variable
string mSelectedTableName;

//define a property to store the listbox value
public string SelectedTableName
{
get{return mSelectedTableName;}
set{mSelectedTableName = value;}
}

private void OKButton_click(object sender, System.EventArgs e)
{
//store the selected value of listbox in the property
this.SelectedTableName = this.listBox1.SelectedText;

//close the form
DialogResult = DialogResult.OK;
this.Close();
}
(Step4)Then back on form1 there is another button called AddColumns. When
the add columns button is clicked I want to do a query on the table that was
selected from the Listbox on form2

c# code in form 1 again

private void AddColumnsButton_click(object sender, System.EventArgs e)
{
//use the variable mStrTableName here which already has selected
table name (Step2)
}
 
D

David Cuffee

Thanks a lot Shakir. I will try this as soon as I get to work. I actually
tried just creating a public varible in form1 but form2 couldnt see it and
vice versa. I think your way will probably work better.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top