I'm looking for instructions to convert a demo project into a class
-
Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks
-
Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks
I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well. How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code. I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
I'm not planning on even looking at 1000 lines of your code - I don't have the time - but I'd start by looking at creating a UserControl[^] rather than a generic class - they display directly. And then either embedding a DataGridView or deriving from one. If you are planning on using it in tabs, I'd also consider creating another UserControl, derived from TabPage which encapsulated the DataGridView UserControl to make that easier to use as well. How easy this is for you to do will depend on how you wrote the original code: it may be worth your creating a new project and "playing with it" until you get the idea before you start launching into moving existing code. I use UserControls a lot - they help to contain code and simplify interfaces between modules. Worth looking at!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Thanks for the reply. I read the MS article, and I also found this video. This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about? https://www.youtube.com/watch?v=l5L_q_jI494[^]
-
Thanks for the reply. I read the MS article, and I also found this video. This video seems a little more understandable for me being new to this, as I can see it done, would this also be a good representation of what you are talking about? https://www.youtube.com/watch?v=l5L_q_jI494[^]
Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much! :laugh: Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Difficult to tell - his voice and the microphone "wind noise" was so annoying I couldn't watch that much! :laugh: Seriously, give it a try on a "new project" and see what happens. It really does help with simplifying interfaces between modules and speeds up development too.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
:laugh: That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure? I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail. Thanks.
-
:laugh: That means you didn't make it to 4:20 as about that point a random hammer starts banging the wall behind him ... or he is residing in a cheap motel, I'm not sure? I've started with a new project and a user control class. I can't get the class to instantiate, but I will keep try and report back if I fail. Thanks.
Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Build the project, and it should add the new control to the top of the toolbox. Drag and drop one onto your form, then look at the designer.cs file - it'll show you the code VS uses to instantiate it.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?
dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1(); dgv1.ColumnCount = 25; dgv1.RowCount = 20;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace UserControlDemo
{
public partial class dataGridViewUserControl1 : UserControl
{
public dataGridViewUserControl1()
{
InitializeComponent();
}private int columnCount; public int ColumnCount { get { return columnCount; } set { columnCount = value; } } private int rowCount; public int RowCount { get { return rowCount; } set { rowCount = value; } } private void UserControl1\_Load(object sender, EventArgs e) { // create datagridviewcolumns for (int i = 0; i < columnCount; i++) { dataGridView1.Columns\[i\].Name = "column " + i; } // create datagridviewrows for (int i = 0; i < rowCount; i++) { string\[\] rowName = new string\[\] { "row" + i.ToString() }; dataGridView1.Rows.Add(rowName); } } }
}
-
Ok, I tried the code below but it did not work and my basic class is at the bottom. Under designer.cs, should I be using some of the code under InitializeComponent()?
dataGridViewUserControl1 dgv1 = new dataGridViewUserControl1(); dgv1.ColumnCount = 25; dgv1.RowCount = 20;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;namespace UserControlDemo
{
public partial class dataGridViewUserControl1 : UserControl
{
public dataGridViewUserControl1()
{
InitializeComponent();
}private int columnCount; public int ColumnCount { get { return columnCount; } set { columnCount = value; } } private int rowCount; public int RowCount { get { return rowCount; } set { rowCount = value; } } private void UserControl1\_Load(object sender, EventArgs e) { // create datagridviewcolumns for (int i = 0; i < columnCount; i++) { dataGridView1.Columns\[i\].Name = "column " + i; } // create datagridviewrows for (int i = 0; i < rowCount; i++) { string\[\] rowName = new string\[\] { "row" + i.ToString() }; dataGridView1.Rows.Add(rowName); } } }
}
If you look at the designer file, you'll find some more code: of nothing else there will be a Controls.Add method call to add the new control instance to the forms Controls collection - that's the line which actually makes it display within the form. (For your app, it'll eventually be in the TabPage.Controls collection to make it a "child" of that control rather than the firm itself, but that's the advanced bit :laugh: )
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Hi, In an attempt to graduate into a more professional programmer, I'm trying to comment my code more and use classes more. I have a demo project working and sadly enough I'm not very well versed in creating/using my own classes but would like to change that. I have a datagridview demo project that I've written with the idea that I will have multiple datagridviews in my final project, on multiple tabs. Instead of cutting and pasting and changing the code multiple times, it seems like my own class would benefit me greatly in this instance, where I could instantiate the same object multiple times. If this is correct, can I have help creating a class out of this demo project? It's ~1000 lines of code, but I'll be happy to post it if that would help. Thanks
you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:
V.
(MQOTD rules and previous solutions) -
you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:
V.
(MQOTD rules and previous solutions)I don't think graduating is used in an educational context in this instance, I think he is just trying to become (graduate to) a better developer.
Never underestimate the power of human stupidity RAH
-
you´re graduating and you have no clue how to develop something more descent than this? Don´t get me wrong, I like the fact you´re willing to improve, but when I was graduating I had to do way and way and way better then this... :confused:
V.
(MQOTD rules and previous solutions)Not from school, you misread. I'm self taught and trying to graduate to the next level.
-
Not from school, you misread. I'm self taught and trying to graduate to the next level.
OK apologies. In that case, best way to learn is go through a good book. Digest it from A to Z and do all the exercises starting with the "Hello World" program. Tutorials on the internet are not always that good. Also look up encapsulation, inheritence and polymorphism, the corner stones for OO programming. That should help you getting a good structure in your application. Hope this helps.
V.
(MQOTD rules and previous solutions)