Panel with Buttons in VB.Net

aiman_alnasir
aiman_alnasir used Ask the Experts™
on
I am trying to organize my form by having the buttons inside the panel. I would like a hand on;
Q1- How to have the buttons in new line?
Q2- Once I click on any of those buttons from the form it should become disable (Button1.Enabled = False)
Q3- How to have the next available time from my database on the next buttons and so on &. ? It seems like its only reading the first available time from the database as you see on the form below!

dbcmd.CommandText = "Select * From Resv_Table where Status = Yes ORDER BY Time ASC"
        Dim time As Date = dbcmd.ExecuteScalar()
        Dim d, z As Integer
        'Dim myAppt As Button
        Dim Panel1 As New Panel()
        Dim Button1 As New Button()
 
        Panel1.Location = New Point(12, 12)
        Panel1.Size = New Size(872, 728)
        Panel1.BorderStyle = BorderStyle.Fixed3D
        'setting the borderstyle of the panel
        Me.Controls.Add(Panel1)
 
        For d = 0 To 12
            Button1 = New Button
            Button1.Size = New Size(95, 45)
            Button1.Location = New Point(20 + z, 30)
            Button1.Text = time
            Panel1.Controls.Add(Button1)
            'adding the label and checkbox to the panel
            z = z + 105
        Next d

Open in new window

Panel.jpg
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
As you are looping 12 times .Is this fixed ?

How many record you have in database ?

Why did you used ExecureScaller . use executereader.

Commented:
Here is an example of how I would do it.  First if you are creating a bunch of buttons based on a database, i would use a repeater.  Below are pictures of my test data table schema, sample data in the table, the VB.NET code, and a picture of the app running. Let me know if you have questions.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
 
        <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
            <ItemTemplate>
                <asp:Button ID="Button1" runat="server" Text='<%# Bind("Resv_Date", "{0:d}") %>' Width="150" OnClick="DateButtonClicked" />
                <br />
            </ItemTemplate>
        </asp:Repeater>
 
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MyCS %>" 
            SelectCommand="SELECT * FROM [Resv_Table] ORDER BY [Resv_Date]">
        </asp:SqlDataSource>
 
    </form>
</body>
</html>
 
=======================================
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub DateButtonClicked(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim btn As Button = sender
        btn.Enabled = False
    End Sub
 
End Class

Open in new window

TableDef.JPG
TheData.JPG
working.JPG

Author

Commented:
What you did is really good. However, you send me the code on an ASP language. This is not a VB.Net!

Regarding your questions; see my comments under each questions;

As you are looping 12 times .Is this fixed ?
No, it may go to 20 or 25 something

How many record you have in database ?
It will be from 20 to 25

Why did you used ExecureScaller . use executereader 
O.K, I will go with it.
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

Commented:

Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = "Data Source=AVANI2003\SQLEXPRESS08;Initial Catalog=TESTAPPLICATION;Integrated Security=True"
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandText = "Select * from Resv_Table"
        cmd.Connection = con
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim xLocation As Integer = 100
        Dim yLocation As Integer = 100
        While (reader.Read())
            Dim b As Button = New Button()
            b.Size = New Size(100, 50)
            b.Text = DateTime.Parse(reader("resv_date").ToString()).ToString("MM-dd-yyyy")
            b.Location = New Point(xLocation, yLocation)
            yLocation = yLocation + 55
            Panel1.Controls.Add(b)
        End While
        reader.Close()
        con.Close()

Open in new window

Commented:
Please use your connection string.

Commented:

Imports System.Data.SqlClient
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = "Data Source=.\SQLEXPRESS08;Initial Catalog=TESTAPPLICATION;Integrated Security=True"
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandText = "Select * from Resv_Table"
        cmd.Connection = con
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim xLocation As Integer = 100
        Dim yLocation As Integer = 100
        While (reader.Read())
            Dim b As Button = New Button()
            b.Size = New Size(100, 50)
            b.Text = DateTime.Parse(reader("resv_date").ToString()).ToString("MM-dd-yyyy")
            b.Location = New Point(xLocation, yLocation)
            yLocation = yLocation + 55
            AddHandler b.Click, AddressOf Button_click
            Panel1.Controls.Add(b)
        End While
        reader.Close()
        con.Close()
    End Sub
 
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = CType(sender, Button)
        MessageBox.Show(btn.Text)
    End Sub
End Class

Open in new window

Commented:
This code disable button on click.

Imports System.Data.SqlClient
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = "Data Source=.\SQLEXPRESS08;Initial Catalog=TESTAPPLICATION;Integrated Security=True"
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandText = "Select * from Resv_Table"
        cmd.Connection = con
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim xLocation As Integer = 10
        Dim yLocation As Integer = 100
        While (reader.Read())
            Dim b As Button = New Button()
            b.Size = New Size(100, 50)
            b.Text = DateTime.Parse(reader("resv_date").ToString()).ToString("MM-dd-yyyy")
            b.Location = New Point(xLocation, yLocation)
            yLocation = yLocation + 55
            AddHandler b.Click, AddressOf Button_click
            Panel1.Controls.Add(b)
        End While
        reader.Close()
        con.Close()
    End Sub
 
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = CType(sender, Button)
        MessageBox.Show(btn.Text)
        btn.Enabled = False
    End Sub
End Class

Open in new window

Author

Commented:
Still, there are some error;

' I dont have SQL server on my machine
con.ConnectionString = "Data Source=.\SQLEXPRESS08;Initial Catalog=TESTAPPLICATION;Integrated Security=True"

'This is the connection which I used
Imports System.Data.OleDb

Private Sub Reserve_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\mytable.accdb;Persist Security Info=False"

Author

Commented:
Dim con As New OleDb.OleDbConnection
con.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source = C:\mytable.accdb;Persist Security Info=False"
       
con.Open()

Dim dbcmd As New OleDbCommand
dbcmd.Connection = con
dbcmd.CommandText = "Select * From Resv_Table where Status = Yes ORDER BY Time ASC"

'it allow me to use dbcmd.ExecuteScalar() but it doesn't allow me to use dbcmd.Executereader()
Dim time As Date = dbcmd.ExecuteScalar()

Dim reader As OleDbDataReader = dbcmd.ExecuteReader()
        Dim Panel1 As New Panel
        Dim xLocation As Integer = 10
        Dim yLocation As Integer = 100
        While (reader.Read())
            Dim b As Button = New Button()
            b.Size = New Size(100, 50)
            b.Text = DateTime.Parse(reader("Time").ToString()).ToString("MM-dd-yyyy")
            b.Location = New Point(xLocation, yLocation)
            yLocation = yLocation + 55
            AddHandler b.Click, AddressOf Button_Click
            Panel1.Controls.Add(b)
        End While
        reader.Close()
        con.Close()

When I run it, it doesn't show any buttons ?

Author

Commented:
Ohhhhh, sorry it works by using the above command. I just forget to use

Panel1.Location = New Point(12, 12)
        Panel1.Size = New Size(518, 334)
        Panel1.BorderStyle = BorderStyle.Fixed3D
        'setting the borderstyle of the panel
        Me.Controls.Add(Panel1)

Thanks a lot. Now, I will try to have the buttons fill the first row in the panel before it goes to next line.

Author

Commented:
Could you please see the attached picture. I want to arrange them.
buttons-with-panels.jpg

Commented:
How many button should staty in first row ?

Commented:
put your final code over here
Commented:
below is sample . Make changes as per your database and panel formation



Imports System.Data.SqlClient
Public Class Form1
 
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim con As SqlConnection = New SqlConnection()
        con.ConnectionString = "Data Source=.\SQLEXPRESS08;Initial Catalog=TESTAPPLICATION;Integrated Security=True"
        Dim cmd As SqlCommand = New SqlCommand()
        cmd.CommandText = "Select * from Resv_Table"
        cmd.Connection = con
        con.Open()
        Dim reader As SqlDataReader = cmd.ExecuteReader()
        Dim xLocation As Integer = 100
        Dim yLocation As Integer = 100
        Dim colCount As Integer = Convert.ToInt32(Panel1.Width / 100)
        Dim colCountForeachRow As Integer = 0
        While (reader.Read())
            Dim b As Button = New Button()
            b.Size = New Size(100, 50)
            b.Text = DateTime.Parse(reader("resv_date").ToString()).ToString("MM-dd-yyyy")
            b.Location = New Point(xLocation, yLocation)
            AddHandler b.Click, AddressOf Button_Click
            xLocation += b.Width + 5
            Panel1.Controls.Add(b)
            colCountForeachRow += 1
            If colCount = colCountForeachRow Then
                xLocation = 100
                yLocation = yLocation + 55
                colCountForeachRow = 0
            End If
        End While
        reader.Close()
        con.Close()
    End Sub
 
    Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = CType(sender, Button)
        MessageBox.Show(btn.Text)
        btn.Enabled = False
    End Sub
End Class

Open in new window

Author

Commented:
Thank you so much. Now, I can go ahead and continue my work.
A++

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial