locked
How to create this user control? RRS feed

  • Question

  • Hi,
    I have to develop a small app for which I have to create a user control which is intended to be something like a Table View.
    On Form Load, the user should be asked about the number of rows of columns required on the form.
    Once the user sets the number of cells, each cell should have a button and a Text Box inside it (grouped together). Please help me in creating the User Control.
    Thanks!
    Sunday, September 26, 2010 1:05 PM

Answers

  • Try adding this to the start of the sub to clear the control first before recreating the rows and columns .

     TableLayoutPanel1.Controls.Clear()


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Marked as answer by Bin-ze Zhao Thursday, September 30, 2010 7:57 AM
    Monday, September 27, 2010 10:44 PM
  • Add an event handler when you create each button and create a sub to handle the event .

    Dim btn As New Button()
    AddHandler btn.Click,Addressof TheSubIMade

     


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    • Marked as answer by optimus_prime Saturday, October 2, 2010 2:36 AM
    Wednesday, September 29, 2010 1:37 PM

All replies

  • Learn how to create user control and when you get stuck then ask for help. It will be difficult for someone to start the project for you based on the description you posted.

    If you look at the right side of this page, below your Forum links, there are similar threads under related topics. Check those threads, any of the thread may get you started or gives you ideas.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, September 26, 2010 7:39 PM
  • I got till here but there are some errors which I would like to ask.

    Form1.Designer.vb

    Namespace MSDNForums
    	Partial Class Form1
    		''' <summary>
    		''' Required designer variable.
    		''' </summary>
    		Private components As System.ComponentModel.IContainer = Nothing
    
    		''' <summary>
    		''' Clean up any resources being used.
    		''' </summary>
    		''' <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
    		Protected Overrides Sub Dispose(disposing As Boolean)
    			If disposing AndAlso (components IsNot Nothing) Then
    				components.Dispose()
    			End If
    			MyBase.Dispose(disposing)
    		End Sub
    
    		#Region "Windows Form Designer generated code"
    
    		''' <summary>
    		''' Required method for Designer support - do not modify
    		''' the contents of this method with the code editor.
    		''' </summary>
    		Private Sub InitializeComponent()
    			Me.tableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
    			Me.rowsLabel = New System.Windows.Forms.Label()
    			Me.columnsLabel = New System.Windows.Forms.Label()
    			Me.rowsTextBox = New System.Windows.Forms.TextBox()
    			Me.columnsTextBox = New System.Windows.Forms.TextBox()
    			Me.button1 = New System.Windows.Forms.Button()
    			Me.SuspendLayout()
    			' 
    			' tableLayoutPanel1
    			' 
    			Me.tableLayoutPanel1.AutoSize = True
    			Me.tableLayoutPanel1.ColumnCount = 2
    			Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F))
    			Me.tableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F))
    			Me.tableLayoutPanel1.Location = New System.Drawing.Point(31, 109)
    			Me.tableLayoutPanel1.Name = "tableLayoutPanel1"
    			Me.tableLayoutPanel1.RowCount = 2
    			Me.tableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F))
    			Me.tableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F))
    			Me.tableLayoutPanel1.Size = New System.Drawing.Size(200, 100)
    			Me.tableLayoutPanel1.TabIndex = 0
    			' 
    			' rowsLabel
    			' 
    			Me.rowsLabel.AutoSize = True
    			Me.rowsLabel.Location = New System.Drawing.Point(31, 28)
    			Me.rowsLabel.Name = "rowsLabel"
    			Me.rowsLabel.Size = New System.Drawing.Size(86, 13)
    			Me.rowsLabel.TabIndex = 1
    			Me.rowsLabel.Text = "Number of Rows"
    			' 
    			' columnsLabel
    			' 
    			Me.columnsLabel.AutoSize = True
    			Me.columnsLabel.Location = New System.Drawing.Point(357, 28)
    			Me.columnsLabel.Name = "columnsLabel"
    			Me.columnsLabel.Size = New System.Drawing.Size(99, 13)
    			Me.columnsLabel.TabIndex = 1
    			Me.columnsLabel.Text = "Number of Columns"
    			' 
    			' rowsTextBox
    			' 
    			Me.rowsTextBox.Location = New System.Drawing.Point(123, 28)
    			Me.rowsTextBox.Name = "rowsTextBox"
    			Me.rowsTextBox.Size = New System.Drawing.Size(100, 20)
    			Me.rowsTextBox.TabIndex = 2
    			' 
    			' columnsTextBox
    			' 
    			Me.columnsTextBox.Location = New System.Drawing.Point(462, 28)
    			Me.columnsTextBox.Name = "columnsTextBox"
    			Me.columnsTextBox.Size = New System.Drawing.Size(100, 20)
    			Me.columnsTextBox.TabIndex = 2
    			' 
    			' button1
    			' 
    			Me.button1.Location = New System.Drawing.Point(647, 24)
    			Me.button1.Name = "button1"
    			Me.button1.Size = New System.Drawing.Size(75, 23)
    			Me.button1.TabIndex = 3
    			Me.button1.Text = "Do It!"
    			Me.button1.UseVisualStyleBackColor = True
    			AddHandler Me.button1.Click, New System.EventHandler(Me.button1_Click)
    			' 
    			' Form1
    			' 
    			Me.AutoScaleDimensions = New System.Drawing.SizeF(6F, 13F)
    			Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    			Me.ClientSize = New System.Drawing.Size(856, 524)
    			Me.Controls.Add(Me.button1)
    			Me.Controls.Add(Me.columnsTextBox)
    			Me.Controls.Add(Me.rowsTextBox)
    			Me.Controls.Add(Me.columnsLabel)
    			Me.Controls.Add(Me.rowsLabel)
    			Me.Controls.Add(Me.tableLayoutPanel1)
    			Me.Name = "Form1"
    			Me.Text = "Form1"
    			Me.ResumeLayout(False)
    			Me.PerformLayout()
    
    		End Sub
    
    		#End Region
    
    		Private tableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel
    		Private rowsLabel As System.Windows.Forms.Label
    		Private columnsLabel As System.Windows.Forms.Label
    		Private rowsTextBox As System.Windows.Forms.TextBox
    		Private columnsTextBox As System.Windows.Forms.TextBox
    		Private button1 As System.Windows.Forms.Button
    	End Class
    End Namespace
    
    

     

    Form1.vb

    Imports System.Collections.Generic
    Imports System.ComponentModel
    Imports System.Data
    Imports System.Drawing
    Imports System.Linq
    Imports System.Text
    Imports System.Windows.Forms
    
    Namespace MSDNForums
    	Public Partial Class Form1
    		Inherits Form
    		Public Sub New()
    			InitializeComponent()
    		End Sub
    
    		Private Sub button1_Click(sender As Object, e As EventArgs)
    			tableLayoutPanel1.RowCount = Integer.Parse(rowsTextBox.Text)
    			tableLayoutPanel1.ColumnCount = Integer.Parse(columnsTextBox.Text)
    
    			For col As Integer = 0 To tableLayoutPanel1.ColumnCount - 1
    				For rows As Integer = 0 To tableLayoutPanel1.RowCount - 1
    					Dim p As New Panel()
    					Dim tb As New TextBox()
    					Dim btn As New Button()
    					p.Controls.Add(tb)
    					p.Controls.Add(btn)
    					btn.Location = New Point(0, tb.Top + 20)
    					tableLayoutPanel1.Controls.Add(p, col, rows)
    				Next
    			Next
    		End Sub
    	End Class
    End Namespace
    
    


    Looks like the number of rows keep on adding to themselves when I change the number of rows and columns from the text box.

    Secondly, if I have to use this Form on different screen resolutions, I would like to keep the size of controls relative so that they adjust themselves. How can that be done?

     

    Monday, September 27, 2010 6:22 PM
  • Try adding this to the start of the sub to clear the control first before recreating the rows and columns .

     TableLayoutPanel1.Controls.Clear()


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    • Marked as answer by Bin-ze Zhao Thursday, September 30, 2010 7:57 AM
    Monday, September 27, 2010 10:44 PM
  • Thanks for help.

    A Button and a TextBox now appears in each cell of the column. I want to perform an operation if a user clicks inside the region of the cell. How can I check that if the user has clicked inside the cell?

    I want to access the Click event of each of these buttons together (a grouped event handler). How can I proceed?

    Wednesday, September 29, 2010 1:30 PM
  • Add an event handler when you create each button and create a sub to handle the event .

    Dim btn As New Button()
    AddHandler btn.Click,Addressof TheSubIMade

     


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261

    • Marked as answer by optimus_prime Saturday, October 2, 2010 2:36 AM
    Wednesday, September 29, 2010 1:37 PM
  • Add an event handler when you create each button and create a sub to handle the event .

    Dim btn As New Button()
    AddHandler btn.Click,Addressof TheSubIMade

    I also want to access different button click events for these buttons. What should I do in the case I have to do different operations on different button clicks?

    For eg: Basically, what I am trying to do is is this.

    If I click the button in Row 1 and Column1 in the TableLayoutPanel, I choose a .txt file through a Open File Dialog and then display in the corresponding TextBox.

    Thanks!
    Saturday, October 2, 2010 6:40 AM
  • First you wanted one group eventhandler now you want different ones for each button ? I showed you how to wire a button.clicki event to an eventhandler . If you want other eventhandlers then wire them up the same way . Give me an example of what other events you want to create for the buttons .


    Coding4fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
    Please format the code in your posts with the button . Makes it easier to read . Or use the Forum Code Formatter by JohnWein http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/bf977a14-d9d4-4e84-9784-bf76b9e23261
    Saturday, October 2, 2010 11:01 AM
  • First you wanted one group eventhandler now you want different ones for each button ? I showed you how to wire a button.clicki event to an eventhandler . If you want other eventhandlers then wire them up the same way . Give me an example of what other events you want to create for the buttons .

    Yes, I wanted to add some additional features to the app by accessing individual event handlers for these buttons. I am creating cells using a TableLayoutPanel in which each cell has a button. Since, I specify the number of buttons on runtime, so I was doubtful about how to add separate event handlers for each of them since I wouldn't know about the number of buttons.

    I would try to explain here:

    When a button is clicked from any of the buttons in the TableLayout on Form1, Form2 opens in which the user chooses the BackgroundImage for a button, Text for a button, frame color etc. If I keep a grouped event handler, all of these buttons on Form1 would get the same background image, color etc.

    If I click on a button which is in the 3rd row and 3rd column in TableLayout, I want only the properties of that button to be affected and not all of them together.

    Thanks for help.

    Saturday, October 2, 2010 11:52 AM