i came from a vb6 background and I'm slowly testing the c# waters. my problem is i have difficulty in adapting an object oriented approach in my programs as i tend to pattern my programs the way i use to code in vb6. take for example this data entry part of software i am creating, as you see i am still coding it the way i code in vb6.
namespace WLMS
{
public partial class frmBA : Form
{
enum status
{
add,
edit,
delete,
complete,
datafill,
}
status stat;
clsSqlCommands sqlCommands = new clsSqlCommands();
string connectionString = ConfigurationManager.ConnectionStrings["Main"].ConnectionString;
int dataID = 0;
public frmBA()
{
InitializeComponent();
}
private void displayInGrid()
{
DataTable dt = new DataTable();
dt = sqlCommands.dataFill("select series,baName,baLoc from tblBA order by baName",connectionString);
if (dt != null)
{
dgBA_List.DataSource = dt;
dgBA_List.Columns[0].HeaderText = null;
dgBA_List.Columns[1].HeaderText = "BA NAME";
dgBA_List.Columns[2].HeaderText = "BA LOCATION";
dgBA_List.Columns[0].Visible = false;
dgBA_List.Columns[1].Width = 100;
dgBA_List.Columns[2].Width = 200;
dataID = 0;
}
}
private void frmBA_Load(object sender, EventArgs e)
{
displayInGrid();
}
private void tlADD_Click(object sender, EventArgs e)
{
groupBox1.Enabled = true;
clearTextBoxes(groupBox1);
txtBAName.Focus();
stat = status.add;
}
private void tlEDIT_Click(object sender, EventArgs e)
{
if (dataID != 0)
{
groupBox1.Enabled = true;
stat = status.edit;
}
else
MessageBox.Show("click on item to edit");
}
private void tlDELETE_Click(object sender, EventArgs e)
{
deleteData();
}
private void tlSAVE_Click(object sender, EventArgs e)
{
if (checkFilledTextBoxes(groupBox1) == true)
{
switch (stat)
{
case status.add:
{
addNewData();
break;
}
case status.edit:
{
editData();
break;
}
default:
{
break;
}
}
}
}
private bool checkForDuplicates()
{
DataRow dtr = sqlCommands.getOneRow("select count(*) as cnt from tblba where baName = '" + txtBAName.Text + "' and baLoc = '" + txtBALoc.Text + "'", connectionString);
if (Convert.ToInt16(dtr["cnt"]) < 1)
{
return false;
}
else
return true;
}
private void editData()
{
if (!checkForDuplicates())
{
sqlCommands.dataManipulate("update tblBa set baName = '" + txtBAName.Text + "',baLoc = '" + txtBALoc.Text + "' where series = " + dataID + "", connectionString);
clearTextBoxes(groupBox1);
groupBox1.Enabled = false;
stat = status.complete;
displayInGrid();
MessageBox.Show("Record Edited");
}
else
MessageBox.Show("Duplicate record");
}
private void deleteData()
{
DialogResult dialogResult = MessageBox.Show("Are you sure?", "", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
sqlCommands.dataManipulate("delete from tblBa where series = " + dataID + "", connectionString);
clearTextBoxes(groupBox1);
groupBox1.Enabled = false;
stat = status.complete;
displayInGrid();
MessageBox.Show("Record deleted");
}
else
MessageBox.Show("Duplicate record");
}
private void addNewData()
{
if (!checkForDuplicates())
{
sqlCommands.dataManipulate("insert into tblBa (baName,baLoc) values ('" + txtBAName.Text + "','" + txtBALoc.Text + "')", connectionString);
clearTextBoxes(groupBox1);
txtBAName.Focus();
stat = status.complete;
displayInGrid();
MessageBox.Show("Record Added");
}
else
MessageBox.Show("Duplicate record");
}
private void clearTextBoxes(GroupBox gprx)
{
foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
{
txtBx.Text = "";
}
}
private Boolean checkFilledTextBoxes(GroupBox gprx)
{
foreach (TextBox txtBx in gprx.Controls.OfType<TextBox>())
{
if (txtBx.Text == "")
return false;
}
return true;
}
private void tlEXIT_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void dgBA_List_CellClick(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
DataGridViewRow row = dgBA_List.Rows[rowIndex];
dataID = Convert.ToInt16(row.Cells[0].Value);
txtBALoc.Text = row.Cells[1].Value.ToString();
txtBAName.Text = row.Cells[2].Value.ToString();
groupBox1.Enabled = false;
}
}
}
it has two textboxes that are inside a groupbox, one datagridview and 5 toolstrip buttons to add,edit,delete,save and exit. My question is, how can i restructure my code to have an object oriented approach?
PLEASE HELP.. thanks