-
Oct 8th, 2012, 09:38 PM
#1
Thread Starter
Member
[RESOLVED] vb.net control button in popup
Hi, I'm new to programming and I need help with something really basic..
I have a form with a datagridview and an "insert" button that opens a popup window (new form). This popup contains labels and textboxes, and 2 buttons (insert & cancel). How do you add event handlers to the buttons inside the popup? For example, if the user clicks "Cancel", the popup should close.
Here's the code I have so far:
Public Sub InsertButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InsertButton.Click
Dim insertPopup As New Form
insertPopup.Size = New System.Drawing.Size(300, 400)
insertPopup.StartPosition = FormStartPosition.CenterScreen
insertPopup.Show()
Dim acceptButton As New Button
Dim cancelButton As New Button
acceptButton.Location = New System.Drawing.Point(100, 325)
acceptButton.Text = "Insert"
acceptButton.Size = New System.Drawing.Size(85, 24)
acceptButton.TabIndex = 1
cancelButton.Location = New System.Drawing.Point(190, 325)
cancelButton.Text = "Cancel"
cancelButton.Size = New System.Drawing.Size(85, 24)
cancelButton.TabIndex = 2
insertPopup.Controls.Add(acceptButton)
insertPopup.Controls.Add(cancelButton)
End Sub
Thanks for your help.
-
Oct 8th, 2012, 09:56 PM
#2
Re: vb.net control button in popup
Woah! Why are you adding controls to this "popup" in code after showing it? It's just a form like any other, so just treat it like any other form. Add the Buttons and their event handlers in the designer, just as you have for the main form containing the grid. There's no reason to do it any differently.
Also, I strongly suggest that you call ShowDialog rather than Show. You should check out this CodeBank thread of mine:
http://www.vbforums.com/showthread.p...gue&highlight=
-
Oct 9th, 2012, 12:56 AM
#3
Thread Starter
Member
Re: vb.net control button in popup
Thanks so much... I just read your CodeBank thread and it really helped.
-
Oct 9th, 2012, 01:39 AM
#4
Thread Starter
Member
Re: vb.net control button in popup
I tried your code but I'm not getting what I want. I suspect it's because my datagridview is pulling data from mySQL, so it's dynamic. Right now, using the following code, I'm getting a complete blank in every textbox in the updatePopup Form.
Private Sub fetchData(ByVal tableName As String)
command = New MySqlCommand
dt = New DataTable
adapter = New MySqlDataAdapter
If (conn.State = ConnectionState.Closed) Then
setConnection()
End If
command.Connection = conn
command.CommandText = "select * from `" & tableName & "`;"
adapter.SelectCommand = command
reader = command.ExecuteReader
dt.Load(reader)
BindingSource1.DataSource = dt
DataGridView1.DataSource = BindingSource1
reader.Close()
conn.Close()
End Sub
Private Sub DataGridView1_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
Dim row As DataRow = DirectCast(Me.BindingSource1.Current, DataRowView).Row
Using dialogue As New insertPopup()
dialogue.ShowDialog()
End Using
End Sub
Update Popup Form
Public Class updatePopup
Public Sub New()
InitializeComponent()
End Sub
Private data As DataRow
Public Sub New(ByVal data As DataRow)
' This call is required by the Windows Form Designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.data = data
TextBox1.Text = data("dataid").ToString()
TextBox2.Text = CStr(data("col1"))
TextBox3.Text = CStr(data("col2"))
TextBox4.Text = CStr(data("col3"))
TextBox5.Text = CStr(data("col4"))
TextBox6.Text = CStr(data("col5"))
TextBox7.Text = CStr(data("col6"))
TextBox8.Text = CStr(data("col7"))
End Sub
Private Sub acceptButton_Click(sender As System.Object, e As System.EventArgs) Handles acceptButton.Click
data("col1") = TextBox2.Text
data("col2") = TextBox3.Text
data("col3") = TextBox4.Text
data("col4") = TextBox5.Text
data("col5") = TextBox6.Text
data("col6") = TextBox7.Text
data("col7") = TextBox8.Text
End Sub
End Class
There are 2 problems with this so far:
1) the selected row's values aren't showing up in the updatePopup form, since the datagridview is pulling the data from mySQL.
2) even if it worked, I need the updatePopup form to work dynamically, so that the column names will be different, depending on what data table the user chose on the main form.
I'm really lost in how to do this, and I'd greatly appreciate your help!!
Thanks so much.
Last edited by echoUser; Oct 9th, 2012 at 01:42 AM.
-
Oct 9th, 2012, 01:55 AM
#5
Re: vb.net control button in popup
First up, please use the Code or VBCode button provided on the advanced editor to wrap your code snippets in formatting tags or else enter the [code] or [highlight=vb.net] tags yourself.
As for the issue, you might try reading your code a bit more carefully. Your first code snippet is creating an instance of a class named 'insertPopup' while the second code snippet is for a class named 'updatePopup'.
As for this need for it to be "dynamic", you can create the TextBoxes on demand if you need to, but that would be done in the dialogue, not the calling form. You can get the DataTable from the DataRow and its Columns will tell you how many TextBoxes to display and what Labels to display beside them. I would suggest using a TableLayoutPanel to handle the layout of the Labels and TextBoxes and the OK and Cancel Buttons are still going to be created in the designer because they never change.
-
Oct 9th, 2012, 01:58 AM
#6
Thread Starter
Member
Re: vb.net control button in popup
Yes, I just noticed that I was using the wrong form name. Now the textboxes are being filled correctly. I'll try your suggestion on using a tableLayoutPanel to handle the stuff in the dialogue dynamically. Will probably come back soon with more questions, but thanks again!
-
Oct 9th, 2012, 02:22 AM
#7
Thread Starter
Member
Re: vb.net control button in popup
How can I get the DataTable from the DataRow? And the # of columns from that table?
Also, when I click the OK button, how can I set each textbox to its new value without specifying the column & textbox, i.e. data("someColName") = TextBox2.Text ?
-
Oct 9th, 2012, 02:47 AM
#8
Re: vb.net control button in popup
Originally Posted by echoUser
How can I get the DataTable from the DataRow? And the # of columns from that table?
Also, when I click the OK button, how can I set each textbox to its new value without specifying the column & textbox, i.e. data("someColName") = TextBox2.Text ?
Have you read the documentation for the DataRow class? If not, why not? How much time do you think Microsoft spent compiling the Help documentation that you're ignoring? It should take no more than a couple of minutes to answer that first question and then not much more to answer the second. Look for yourself first and then ask here for what you can't find. We should be here to help you with problems, no t a substitute for your own endeavours.
-
Oct 9th, 2012, 03:20 AM
#9
Thread Starter
Member
Re: vb.net control button in popup
Okay, here's what I have.
Code:
Public Sub New(ByVal data As DataRow)
InitializeComponent()
Me.data = data
Dim table = data.Table
Dim column = data.Table.Columns
Dim textBox As New TextBox()
textBox.Size = New Size(200, 30)
TableLayoutPanel.ColumnStyles.Add(New ColumnStyle())
For Each column In table.Columns
TableLayoutPanel.RowStyles.Add(New RowStyle())
TableLayoutPanel.Controls.Add(textBox)
Next
Controls.Add(TableLayoutPanel)
End Sub
I got an error saying system.data.datacolumn cannot be casted to system.data.dataColumnCollection.
Could you please point out what fixes I need to make?
Thank you.
-
Oct 9th, 2012, 03:48 AM
#10
Re: vb.net control button in popup
Does this make sense:
Code:
Public Sub New(ByVal data As DataRow)
InitializeComponent()
Me.data = data
Dim table = data.Table
Dim column = data.Table.Columns
Dim textBox As New TextBox()
textBox.Size = New Size(200, 30)
TableLayoutPanel.ColumnStyles.Add(New ColumnStyle())
For Each column In table.Columns
TableLayoutPanel.RowStyles.Add(New RowStyle())
TableLayoutPanel.Controls.Add(textBox)
Next
Controls.Add(TableLayoutPanel)
End Sub
Either 'column' is all the columns or it's one of the columns, not both. Given that the property is named Columns, does it make sense to assign it to a variable named 'column'? Common sense: don't mix singular and plural. Just get rid of that first line because it serves no useful purpose.
Again, why are adding the TableLayoutPanel to the form in code? Adding controls in the designer should ALWAYS be your first choice. Only add controls in code if you don't know about them at design time. The TableLayoutPanel you know about at design time. The Labels and TextBoxes in the TableLayoutPanel you do not know about until run time. Two columns in the TLP and, for each column, add a Label and a TextBox.
-
Oct 9th, 2012, 07:46 AM
#11
Hyperactive Member
Re: vb.net control button in popup
Also I believe your sql statement wont work as you have put quotes round the table name which are not needed:
Code:
command.CommandText = "select * from `" & tableName & "`;"
Should be:
Code:
command.CommandText = "select * from " & tableName & ";"
If your table name includes spaces (which I recommend not doing) then you need brackets round the table name like:
Code:
command.CommandText = "select * from [" & tableName & "];"
HTH
Rico
Using: VB.net & MS SQL
-
Oct 9th, 2012, 09:48 AM
#12
Re: vb.net control button in popup
I noticed that, too. I was surprised that the SQL even executed, but I figured that perhaps MySQL uses a different flavor of SQL.
Another issue with the original code is that you create a dataadapter, set the SelectCommand of that dataadapter, then use a reader to fill the table. There is no need for the dataadapter if you are going to use a reader to fill the table. The two mechanisms are alternative methods for filling a table. You are using parts of both. This won't cause any particular harm, since the dataadapter is created, then destroyed, but why bother?
My usual boring signature: Nothing
-
Oct 9th, 2012, 07:57 PM
#13
Thread Starter
Member
Re: vb.net control button in popup
Hi Rico, I'm using mySQL, and the brackets don't seem to work. I think the syntax for mySQL is single quotes.
Thanks for your advice though!
-
Oct 9th, 2012, 08:05 PM
#14
Re: vb.net control button in popup
Originally Posted by echoUser
Hi Rico, I'm using mySQL, and the brackets don't seem to work. I think the syntax for mySQL is single quotes.
Thanks for your advice though!
They're not single quotes. They are graves. The singular is grave (pronounced "grarv") and it's an accent used in French. It is found on the same key as the tilde (~), in the top, left corner of the keyboard, while a single quote is found on the same key as the double quote.
-
Oct 10th, 2012, 12:44 AM
#15
Thread Starter
Member
Re: vb.net control button in popup
Could you take a look at my for loop and tell me why it's only getting the last column? Thanks.
Code:
Dim i As Integer
Dim dt As DataTable = data.Table
Dim dc As DataColumn
Dim row As DataRow = data
Dim label As New Label
label.Size = New System.Drawing.Size(100, 17)
Dim textbox As New TextBox
textbox.Size = New System.Drawing.Size(100, 17)
tbltest.RowCount = dt.Columns.Count
tbltest.ColumnCount = 2
For i = 0 To dt.Columns.Count - 1
dc = dt.Columns(i)
tbltest.RowStyles.Add(New RowStyle())
label.Text = dc.ColumnName
textbox.Text = row(dc).ToString()
tbltest.Controls.Add(label, 0, i)
tbltest.Controls.Add(textbox, 1, i)
Next
-
Oct 10th, 2012, 01:06 AM
#16
Re: vb.net control button in popup
How many Labels and how many TextBoxes are you creating there?
-
Oct 10th, 2012, 09:41 AM
#17
Re: vb.net control button in popup
And, in case you missed it, the answer to that question is the key to the problem.
My usual boring signature: Nothing
-
Oct 11th, 2012, 12:17 AM
#18
Thread Starter
Member
Re: vb.net control button in popup
Apparently just one each, but since the controls.add functions are inside the loop, shouldn't it create however many columns there are?
-
Oct 11th, 2012, 12:39 AM
#19
Re: vb.net control button in popup
It's not the Add method that creates the controls. To create an object you must invoke a constructor using the New keyword.
Programming objects behave pretty much like real-world objects, which is the whole intention of object-oriented programming. Let's say that you have an egg carton with a dozen cups and you have one egg. If you add that egg to the carton in the first cup, then you add it in the second, then you add it in the third and so on until you add it in the twelfth, what do you have? Do you magically have a dozen eggs? No, you just have one egg in the last cup because that's where you last put it.
The same is happening here. You've only got one Label and one TextBox and you just keep moving them. If you want multiple controls then you have to create multiple controls, which means invoking the constructor multiple times. Currently it's what's inside the loop that is getting executed multiple times, so that should tell you about where to invoke the constructor.
-
Oct 11th, 2012, 02:24 AM
#20
Thread Starter
Member
Re: vb.net control button in popup
Okay, I think I got it. Thanks for your help.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|