Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 2010 > BOOK: Beginning Microsoft Visual Basic 2010
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning Microsoft Visual Basic 2010
This is the forum to discuss the Wrox book Beginning Visual Basic 2010 by Thearon Willis, Bryan Newsome; ISBN: 9780470502228
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning Microsoft Visual Basic 2010 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 14th, 2010, 10:04 PM
Registered User
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Chapter 16 related question

Hi guys, i am a newbie learning using your book.
as i wanted to make few change to a datagrid i was forced to learn how to write the code associated with it.
however as i wanted to add a toolstrip using code i got lost a bit.

the code i wrote results in a warning an exception
the warning is: Warning 1 'Public Sub New()' in designer-generated type 'db.AccountTypes' should call InitializeComponent method.

and the exception is:
object reference not set to an instance of an object

the code:
'Import Data and SqlClient namespaces
Imports System.Data
Imports System.Data.SqlClient
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms


Public Class AccountTypes
Inherits System.Windows.Forms.Form
Private toolStripContainer1 As ToolStripContainer
Private toolStrip1 As ToolStrip

Dim objConnection As New SqlConnection _
("server=127.0.0.1;database=db;User Id=sa;Password=pass;")
'Server=myServerAddress;Database=myDataBase;Truste d_Connection=True;
Dim objDataAdapter As New SqlDataAdapter
Dim objDateSet As New DataSet()

' Dim DataGridView1 As String


Private Sub AccountTypes_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Try

' Set the select command properties..
objDataAdapter.SelectCommand = New SqlCommand()
objDataAdapter.SelectCommand.Connection = objConnection
objDataAdapter.SelectCommand.CommandText = "Select AccountTypeID,AccountType,notes From AccountTypes "
objDataAdapter.SelectCommand.CommandType = CommandType.Text

'open the database connection
objConnection.Open()

'fill the dataset with data
objDataAdapter.Fill(objDateSet, "AccountTypes")

'close the database connection
objConnection.Close()

'set the datagrid properties to bind it to the data
' grdAccountTypes.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeade r
'
grdAccountTypes.AutoGenerateColumns = True
grdAccountTypes.DataSource = objDateSet
grdAccountTypes.DataMember = "AccountTypes"
grdAccountTypes.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeade r

'declare and set the alternating rows style
Dim objAltenateingCellStyle As New DataGridViewCellStyle
objAltenateingCellStyle.BackColor = Color.WhiteSmoke
grdAccountTypes.AlternatingRowsDefaultCellStyle = objAltenateingCellStyle

'change column names and style using the column name
grdAccountTypes.Columns(0).HeaderText = "Accout ID"
grdAccountTypes.Columns(1).HeaderText = "Accout Type"
grdAccountTypes.Columns(2).HeaderText = "Notes"

'clean up
objDataAdapter = Nothing
objConnection = Nothing

Catch es As Exception
MsgBox(es.Message)
End Try

End Sub
Public Sub New()
newsub()
End Sub 'New

<STAThread()> _
Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub 'Main


Private Sub newsub()
toolStripContainer1 = New System.Windows.Forms.ToolStripContainer()
toolStrip1 = New System.Windows.Forms.ToolStrip()
' Add items to the ToolStrip.
toolStrip1.Items.Add("One")
toolStrip1.Items.Add("Two")
toolStrip1.Items.Add("Three")
' Add the ToolStrip to the top panel of the ToolStripContainer.
toolStripContainer1.TopToolStripPanel.Controls.Add (toolStrip1)
' Add the ToolStripContainer to the form.
Controls.Add(toolStripContainer1)
End Sub 'InitializeComponent

Private Sub BtnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClose.Click
Close()
End Sub

Private Sub BindingNavigator1_Refre****************ems(ByVal sender As System.Object, ByVal e As System.EventArgs)

End Sub


End Class
Reply With Quote
  #2 (permalink)  
Old October 16th, 2010, 06:28 AM
Authorized User
Points: 346, Level: 6
Points: 346, Level: 6 Points: 346, Level: 6 Points: 346, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Posts: 61
Thanks: 0
Thanked 13 Times in 13 Posts
Default InitializeComponent

Hi Arijay,

Take what I say with a pinch of salt as I'm just learning Visual Basic on an Open University course, however I think you need to include the InitializeComponent method in the constructor method New() as follows:

Code:
Public Sub New()

InitializeComponent()

newsub()

End Sub
HTH.
Reply With Quote
  #3 (permalink)  
Old October 16th, 2010, 08:06 AM
Authorized User
Points: 319, Level: 6
Points: 319, Level: 6 Points: 319, Level: 6 Points: 319, Level: 6
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: May 2010
Location: MIssouri
Posts: 70
Thanks: 4
Thanked 6 Times in 6 Posts
Send a message via Yahoo to GeneBuchite
Default another try

maybe if you DIM the tooll strip like follows
Code:
Private Sub newsub()
Dim toolStripContainer1 as New System.Windows.Forms.ToolStripContainer()
toolStrip1 = New System.Windows.Forms.ToolStrip()
' Add items to the ToolStrip.
again I am also just starting in vb.net. but I have run acrost this problem many times .
good luck!
Reply With Quote
  #4 (permalink)  
Old October 16th, 2010, 12:03 PM
Registered User
 
Join Date: Oct 2010
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default ok the problem is solved

1. first enable autosize on form load event
me.autozise = true

2. disable scrollbars and dockoption for the grdview
grdAccountTypes.Dock = DockStyle.None
grdAccountTypes.ScrollBars = ScrollBars.None

3. add a sub called grdsize to determine to with of the columns
Me.grdAccountTypes.Width = 0
Me.grdAccountTypes.Height = 0
For Each column As DataGridViewColumn In Me.grdAccountTypes.Columns
Me.grdAccountTypes.Width += column.Width
Next
Me.grdAccountTypes.Width += Me.grdAccountTypes.RowHeadersWidth
For Each Rows As DataGridViewRow In Me.grdAccountTypes.Rows
Me.grdAccountTypes.Height += Rows.Height
Next
Me.grdAccountTypes.Height += Me.grdAccountTypes.ColumnHeadersHeight

4. call the sub when declaring Greedview size
grdAccountTypes.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCellsExceptHeade r
grdSize()
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Chapter 16 Page 576 Question jsymons BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 3 September 25th, 2009 03:20 AM
Chapter 16 Fig 16-11 krsouthern BOOK: Professional SharePoint 2007 Development ISBN: 978-0-470-11756-9 1 July 8th, 2008 12:11 PM
gdi+ related question ashwinikalokhe C# 0 September 10th, 2007 02:43 AM
DATABASE RELATED QUESTION dkvaseeta Access 3 January 13th, 2007 12:17 PM
Chapter 16 Question SomeDude BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 4 October 24th, 2005 06:13 PM



All times are GMT -4. The time now is 08:51 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.