Click DataGridView cell, change its background image
Hello,
I'm trying to make a visual map editor, and figured that a DataGridView would be the best way to go. I would like to make it so that you can click one of many buttons to set which new image the grid cell will be set to when you click it.
I've set up pretty much everything except that I can't figure out how to set the background image of the cell that I clicked.
Image:
http://www.dreamvalleygame.com/uploa...pGenerator.png
--
TL;DR:
How do I change the background image of the individual cell I click in a DataGridView?
--
The grid was created programmatically.
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set SelectedEntity
SelectedEntity = Black
'Just number
Dim n As Integer = 0
'Add 80 columns
For n = 1 To 80
Dim newCol = New DataGridViewImageColumn
'Set default image to a wall
newCol.Image = Black
'Stretch the image to fit so it looks nice
newCol.ImageLayout = DataGridViewImageCellLayout.Stretch
'Set the width to look nice and pretty
newCol.Width = 20
'Set the number of the column for easier reading
newCol.HeaderText = n.ToString
'Add the column
DataGridView1.Columns.Add(newCol)
Next
'Add 60 rows
DataGridView1.Rows.Add(60)
End Sub
Re: Click DataGridView cell, change its background image
try this. i used 4 color images that i added to my resources + 3 radiobuttons for changing SelectedEntity:
vb Code:
Public Class Form1
Dim SelectedEntity As Bitmap
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Set SelectedEntity
SelectedEntity = My.Resources.black
'Just number
Dim n As Integer = 0
'Add 80 columns
For n = 1 To 80
Dim newCol = New DataGridViewImageColumn
'Set default image to a wall
newCol.Image = SelectedEntity
'Stretch the image to fit so it looks nice
newCol.ImageLayout = DataGridViewImageCellLayout.Stretch
'Set the width to look nice and pretty
newCol.Width = 20
'Set the number of the column for easier reading
newCol.HeaderText = n.ToString
'Add the column
DataGridView1.Columns.Add(newCol)
Next
'Add 60 rows
DataGridView1.Rows.Add(60)
End Sub
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged
SelectedEntity = My.Resources.red
End Sub
Private Sub RadioButton2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton2.CheckedChanged
SelectedEntity = My.Resources.green
End Sub
Private Sub RadioButton3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged
SelectedEntity = My.Resources.blue
End Sub
End Class
Re: Click DataGridView cell, change its background image
Code:
DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
That's all I needed and it worked! Thank you :)
So I'm assuming that I can use e as the selected row and column for whenever I want to calculate the value and save to a text document?
Re: Click DataGridView cell, change its background image
it depends on the particular eventArgs of the handler sub you're using.
for example:
Code:
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
DataGridView1(e.ColumnIndex, e.RowIndex).Value = SelectedEntity
End Sub
the DataGridView1_CellClick event has DataGridViewCellEventArgs which do contain ColumnIndex + RowIndex