• New Patreon Tier and Early Access Content available. If you would like to support AVForums, we now have a new Patreon Tier which gives you access to selected news, reviews and articles before they are available to the public. Read more.

Any VB.NET people around?

I am making a WinForms application in VB.NET and part of it has a questionnaire in it. I am trying to use a Table Layout Panel to display the question and the answer box which come from a database.

I thought a good way to name the Labels and Textboxes would be to use a For loop and have each question number appeanded to it.
e.g

For i = 1 To NumOfQuestions
Dim TxtboxQ & i As New TextBox
Next

which would output

Dim TxtboxQ1 As New TextBox
Dim TxtboxQ2 As New TextBox
Dim TxtboxQ3 As New TextBox
etc.

Is it possible to do this?
 
Last edited:

Stuey1

Well-known Member
I have recently done something similar in what i think is quite a long winded and 'Wrong' way (but it worked and im new to .net) its in C# but i can post it here if you think you could just take the logic from it if you want?

Code:
HtmlTable New_Table = new HtmlTable();
            HtmlTableRow New_Row;
            Int32 RowCount;

            foreach (Panel pan in getCtls(Page).OfType<Panel>())
            {
                if (pan.ID.ToString() == "pnl_Questions")
                {
                    pan.Controls.Add(New_Table);
                    New_Row = new HtmlTableRow();
                    New_Table.Rows.Add(New_Row);

                    HtmlTableCell New_Cell = new HtmlTableCell();
                    New_Row.Cells.Add(New_Cell);
                    New_Cell.InnerText = "Question Category";

                    HtmlTableCell New_Cell1 = new HtmlTableCell();
                    New_Row.Cells.Add(New_Cell1);
                    New_Cell1.InnerText = "Question";

                    HtmlTableCell New_Cell2 = new HtmlTableCell();
                    New_Row.Cells.Add(New_Cell2);
                    New_Cell2.InnerText = "Score";

                    HtmlTableCell New_Cell3 = new HtmlTableCell();
                    New_Row.Cells.Add(New_Cell3);
                    New_Cell3.InnerText = "Question Weight";

                    for (RowCount = 1; RowCount <= Question_Count; RowCount++)
                    {
                        New_Row = new HtmlTableRow();
                        New_Table.Rows.Add(New_Row);

                        HtmlTableCell New_Cat_Cell = new HtmlTableCell();
                        TextBox New_Cat_Textbox = new TextBox();
                        HtmlTableCell New_Q_Cell = new HtmlTableCell();
                        TextBox New_Q_Textbox = new TextBox();
                        HtmlTableCell New_Score_Cell = new HtmlTableCell();
                        DropDownList New_Score_Dropdown = new DropDownList();
                        HtmlTableCell New_Weight_Cell = new HtmlTableCell();
                        TextBox New_Weight_Textbox = new TextBox();
                        HtmlTableCell New_Val_Cell = new HtmlTableCell();
                        CustomValidator New_Validation = new CustomValidator();


                        New_Row.Cells.Add(New_Cat_Cell);
                        New_Cat_Cell.Controls.Add(New_Cat_Textbox);
                        New_Cat_Textbox.Text = Get_Questions.Rows[RowCount - 1]["Question_Group_Description"].ToString();
                        New_Cat_Textbox.Width = 150;
                        New_Cat_Textbox.ID = "Cat" + RowCount;
                        New_Cat_Textbox.Enabled = false;
                        New_Cat_Textbox.ForeColor = Color.Black;

                        New_Row.Cells.Add(New_Q_Cell);
                        New_Q_Cell.Controls.Add(New_Q_Textbox);
                        New_Q_Textbox.Text = Get_Questions.Rows[RowCount - 1]["Question_Text"].ToString();
                        New_Q_Textbox.Width = 500;
                        New_Q_Textbox.TextMode = TextBoxMode.MultiLine;
                        New_Q_Textbox.Wrap = true;
                        New_Q_Textbox.Font.Name = "Arial";
                        New_Q_Textbox.ID = "Q" + RowCount;
                        New_Q_Textbox.Enabled = false;
                        New_Q_Textbox.ForeColor = Color.Black;

                        New_Row.Cells.Add(New_Score_Cell);
                        New_Score_Cell.Controls.Add(New_Score_Dropdown);
                        String Question_Type_Test = Get_Questions.Rows[RowCount - 1]["Question_Type"].ToString();
                        New_Score_Dropdown.Width = 150;
                        New_Score_Dropdown.ID = "Score" + RowCount;

                        if (!IsPostBack)
                        {
                            if (Question_Type_Test == "Yes/No")
                            {
                                New_Score_Dropdown.Items.Add("Please Select Result");
                                New_Score_Dropdown.Items.Add("Yes");
                                New_Score_Dropdown.Items.Add("No");
                            }

                            if (Question_Type_Test == "Normal")
                            {
                                New_Score_Dropdown.Items.Add("Please Select Result");
                                New_Score_Dropdown.Items.Add("Exceeding");
                                New_Score_Dropdown.Items.Add("Developmental");
                                New_Score_Dropdown.Items.Add("Poor");
                            }

                            if (Question_Type_Test == "Failure")
                            {
                                New_Score_Dropdown.Items.Add("Please Select Result");
                                New_Score_Dropdown.Items.Add("Exceeding");
                                New_Score_Dropdown.Items.Add("Developmental");
                                New_Score_Dropdown.Items.Add("Failure");

                            }
                        }

                        New_Row.Cells.Add(New_Weight_Cell);
                        New_Weight_Cell.Controls.Add(New_Weight_Textbox);
                        New_Weight_Textbox.Text = Get_Questions.Rows[RowCount - 1]["Question_Weighting"].ToString() + "%";
                        New_Weight_Textbox.Width = 40;
                        New_Weight_Textbox.ID = "Weight" + RowCount;
                        New_Weight_Textbox.Enabled = false;
                        New_Weight_Textbox.ForeColor = Color.Black;

                        New_Row.Cells.Add(New_Val_Cell);
                        New_Val_Cell.Controls.Add(New_Validation);
                        New_Validation.ControlToValidate = "Score" + RowCount;
                        New_Validation.ErrorMessage = "Question #" + RowCount + " has not been answered";
                        New_Validation.Text = "*";
                        New_Validation.ServerValidate += CustomValidator1_ServerValidate;

                    }

Code:
        IEnumerable<Control> getCtls(Control par)
        {
            List<Control> ret = new List<Control>();
            foreach (Control c in par.Controls)
            {
                ret.Add(c);
                ret.AddRange(getCtls(c));
            }
            return (IEnumerable<Control>)ret;

        }

I don't know if this will help you but i think its similar to what you mean, and to be honest I'm pretty new to all this so it could be really wrong but im just trying to help :smashin:
 
Can you just use a datagrid to populate the form directly from the database?

I have had a look at DataGrids and it could possibly work but I don't like how they look in the form, they look very much like a database which would scare users off. It also looks as if all information can be edited, I want only the answers the user is giving to be editable.

It appears it is not possible in VB.NET to have dynamic variable names, but I can't think of a different way to achieve it :confused:
 

FruitBat

Well-known Member
Your code doesn't look right to me. It looks like some kind of compiler directive version of a for loop.

I think you mean something like

Code:
For i = 1 To NumOfQuestions
    Dim txt As New TextBox
    txt.Name = "TxtboxQ" & i
    tableLayoutPanel1.Controls.Add(txt,0,i)
Next

having said that, I would agree with imightbewrong that a datagrid is a lot easier.
 

imightbewrong

Distinguished Member
I have had a look at DataGrids and it could possibly work but I don't like how they look in the form, they look very much like a database which would scare users off. It also looks as if all information can be edited, I want only the answers the user is giving to be editable.

The interesting part is the binding - you can bind any control to a dataset - so load a dataset and then stuff it into your control and it will all come out with minimal code.

It appears it is not possible in VB.NET to have dynamic variable names, but I can't think of a different way to achieve it :confused:

Not sure what you mean here.

Is there a particular reason why you are writing a questionnaire app? Are there not free/paid libraries you can just grab?
 
I have recently done something similar in what i think is quite a long winded and 'Wrong' way (but it worked and im new to .net) its in C# but i can post it here if you think you could just take the logic from it if you want?

I don't know if this will help you but i think its similar to what you mean, and to be honest I'm pretty new to all this so it could be really wrong but im just trying to help :smashin:

Thanks Stuey I don't know any C# but I will see if I can work it out :thumbsup:

yep you could do all that in three/four lines by letting the libraries do the leg work for you.

is this a piece of say some sort of coursework?

Yes it is for my Computing A level coursework, we have to follow the system development life cycle atm I am developing my program.
 
Your code doesn't look right to me. It looks like some kind of compiler directive version of a for loop.

I think you mean something like

having said that, I would agree with imightbewrong that a datagrid is a lot easier.

Ah that is a good point using txt.Name works I think.

The interesting part is the binding - you can bind any control to a dataset - so load a dataset and then stuff it into your control and it will all come out with minimal code.

Ooo I will have a closer look into DataGrids then :)

Is there a particular reason why you are writing a questionnaire app? Are there not free/paid libraries you can just grab?

I could use a free or paid library but as it's for my coursework I wouldn't get any marks for it, the program itself isn't centered around questionnaires but there is one part that is.
 

FruitBat

Well-known Member
I have had a look at DataGrids and it could possibly work but I don't like how they look in the form, they look very much like a database which would scare users off. It also looks as if all information can be edited, I want only the answers the user is giving to be editable.

You can play about with the appearance of a DataGridView. Here's a quick and dirty with ShowColumnHeaders and ShowRowHeaders set to false, Divider width increased from 0 and first column ReadOnly plus colour changed.
 

Attachments

  • DataGridView1.jpg
    DataGridView1.jpg
    24 KB · Views: 40
Thanks FruitBat I have started using the DataGridView and it's going well!!
I have made a quick n' dirty static version for now as we have to present what we have done so far to the rest of the class tomorrow, in fact the static version would probably be fine anyway but I want to make it more complicated :D
 

The latest video from AVForums

Fidelity in Motion's David Mackenzie talks about his work on disc encoding & the future of Blu-ray
Subscribe to our YouTube channel

Full fat HDMI teeshirts

Support AVForums with Patreon

Top Bottom