Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Constructor must be declared as a Sub, not as a Function

Status
Not open for further replies.

JFoushee

Programmer
Oct 23, 2000
200
US
Hi... I use a lot of DataGridViewTextBoxCells. I'm trying to build a "DataGridViewTextBoxCell" class to simplify the following code:

Code:
Dim dgvRow As New DataGridViewRow
dgvCell = New DataGridViewTextBoxCell()
With dgvCell
    .Value = "hi" : .Style.BackColor = dgvRowBackcolor : dgvRow.Cells.Add(dgvCell)
End With

to the following code:

Code:
Dim dgvRow As New DataGridViewRow
dgvRow.Cells.Add(New myDGVTextbox("hi", dgvRowBackcolor))

Is this a possible action?
I try to build a class object with a "Public Function New" declaration, and get the message "Constructor must be declared as a Sub, not a Function"
 
That is because new can only be a Sub and not a Function.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Thanks for validating the error message. :)

Now, is there a way to perform the action I described?
I want to be able to simplify to the following statement:
Code:
dgvRow.Cells.Add(New myDGVTextbox("hi", dgvRowBackcolor))
The first parameter in the example above is cell-text, and the second parameter is cell-style.
 
Re-reading the post, it might be saying, "this is not possible." I can't tell.
 
Re-reading the post, it might be saying, "this is not possible regardless using 'New' or some other method." I can't tell.
 
this:
Dim dgvRow As New DataGridViewRow
dgvRow.Cells.Add(New myDGVTextbox("hi", dgvRowBackcolor))

has nothing to do with this:
I try to build a class object with a "Public Function New" declaration, and get the message "Constructor must be declared as a Sub, not a Function"
So change it to a sub and the message will go away. I thought you were asking if there was any way to do it as a function and there isn't.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
As to your other question which I assume is you want to change the back color of a cell? No clue. I've seen it done for a full row before, but I don't know if it can be done for a cell. I don't use DataGrids usually so I have no clue on that.

-I hate Microsoft!
-Forever and always forward.
-My kingdom for a edit button!
 
Code:
        With Me.DataGridView1.Rows(1).Cells(1)
            .Style.BackColor = Color.Beige
        End With

Zameer Abdulla
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top