none
how are you able to click on a certain cell in a grid of textboxs and allow that to chage the backcolor of the other cells which lie on the same x and y axis RRS feed

  • Question

  • Public Class form1
     Class sudoku_textbox
      Inherits TextBox
      Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
       If Char.IsDigit(e.KeyChar) Or e.KeyChar = " " Or e.KeyChar = ControlChars.Back Then
        e.Handled = False
       Else
        e.Handled = True
       End If
       If e.KeyChar = " " Or e.KeyChar = "0" Then
        e.KeyChar = ControlChars.Back
       End If
      End Sub
     End Class
     Dim cell(0 To 8, 0 To 8) As sudoku_textbox
     Dim grid(0 To 8, 0 To 8) As String
     Dim backtracking As Boolean = False
     Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
      label1.Text = "X= " & Control.MousePosition.X
      Label2.Text = "Y= " & Control.MousePosition.Y
     End Sub
     Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyClass.Load
      Dim xxtra As Integer
      Dim yxtra As Integer
      Me.CenterToParent()
      Timer1.Start() 'start timer
      Timer1.Interval = 10 'milisecond
      For x As Integer = 0 To 8
       For y As Integer = 0 To 8
        cell(x, y) = New sudoku_textbox
        cell(x, y).AutoSize = False
        'this took me sooo long to figure out' ' this one line of code allows you to change the width and height without the height being set'
        cell(x, y).Font = New Font("Roboto", 50.0, FontStyle.Regular)
        ' change font and font size of the text inside the cells '
        cell(x, y).Text = ""
        cell(x, y).Width = 90
        cell(x, y).Height = 90
        'change the width and height of the cells'
        cell(x, y).MaxLength = 1
        'allowing a maxium of 1 character inside each cell'
        cell(x, y).TextAlign = HorizontalAlignment.Center
        'allowing the text inside the cells to be centerized'

        xxtra = 0
        yxtra = 0
        If x > 2 Then
         xxtra = 4
        End If
        If x > 5 Then
         xxtra = 8
        End If
        If y > 2 Then
         yxtra = 4
        End If
        If y > 5 Then
         yxtra = 8
        End If
        cell(x, y).Location = New Point(45 + x * 90 + xxtra, 120 + y * 90 + yxtra)
        Me.Controls.Add(cell(x, y))
        AddHandler cell(x, y).TextChanged, AddressOf cell_changed
       Next
      Next
     End Sub

    Thursday, January 24, 2019 7:48 AM

All replies

  • Hello,

    Please edit your question and place the code presented in a code block.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, January 24, 2019 12:06 PM
    Moderator
  • Hi

    Didn't try to check your code due to readability being poor.

    However, something to do with DataGridView and highlighting columns/rows according to cell click.

    Here is a stand alone example that may assist. Give it a try. It just needs a blank default DataGridfView1 on Form1 and copy/replace all Form1 code with this code.

    Image

    Code

    ' Form1 with
    
    Option Strict On
    Option Explicit On
    Public Class Form1
      Dim r As New Random
      Dim BC As Color = Color.Pink
      Dim HC As Color = Color.PaleGreen
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        With DataGridView1
          .DefaultCellStyle.BackColor = BC
          For Each s As String In "ABCDEFGHI"
            .Columns.Add(s, s)
            .Columns(s).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Rows.Add()
          Next
          .ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
          .AllowUserToAddRows = False
          .Font = New Font("Arial", 16, FontStyle.Bold)
        End With
    
        ' some random data just
        ' for this example
        For i As Integer = 0 To 8
          For j As Integer = 0 To 8
            DataGridView1.Rows(i).HeaderCell.Value = (i + 1).ToString
            DataGridView1(i, j).Value = r.Next(1, 10)
          Next
        Next
      End Sub
    
      Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        For i As Integer = 0 To 8
          For j As Integer = 0 To 8
            DataGridView1(i, j).Style.BackColor = BC
            If i = e.ColumnIndex Or j = e.RowIndex Then
              DataGridView1(i, j).Style.BackColor = HC
            End If
          Next
        Next
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, January 24, 2019 3:12 PM
    Thursday, January 24, 2019 3:11 PM
  • Hi,

    try the code:

    Imports System.Data.SqlClient
    Public Class Form1
    
        Class sudoku_textbox
            Inherits TextBox
            Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
                If Char.IsDigit(e.KeyChar) Or e.KeyChar = " " Or e.KeyChar = ControlChars.Back Then
                    e.Handled = False
                Else
                    e.Handled = True
                End If
                If e.KeyChar = " " Or e.KeyChar = "0" Then
                    e.KeyChar = ControlChars.Back
                End If
            End Sub
        End Class
        Dim cell(0 To 8, 0 To 8) As sudoku_textbox
        Dim grid(0 To 8, 0 To 8) As String
        Dim backtracking As Boolean = False
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Label1.Text = "X= " & Me.PointToClient(MousePosition).X
            Label2.Text = "Y= " & Me.PointToClient(MousePosition).Y
        End Sub
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Label2.Text = Label1.PointToScreen(Control.MousePosition).X
            Dim xxtra As Integer
            Dim yxtra As Integer
            Me.CenterToParent()
            Timer1.Start() 'start timer
            Timer1.Interval = 10 'milisecond
            For x As Integer = 0 To 8
                For y As Integer = 0 To 8
                    cell(x, y) = New sudoku_textbox
                    cell(x, y).AutoSize = False
                    'this took me sooo long to figure out' ' this one line of code allows you to change the width and height without the height being set'
                    cell(x, y).Font = New Font("Roboto", 50.0, FontStyle.Regular)
                    ' change font and font size of the text inside the cells '
                    cell(x, y).Text = ""
                    cell(x, y).Width = 90
                    cell(x, y).Height = 90
                    'change the width and height of the cells'
                    cell(x, y).MaxLength = 1
                    'allowing a maxium of 1 character inside each cell'
                    cell(x, y).TextAlign = HorizontalAlignment.Center
                    'allowing the text inside the cells to be centerized'
    
                    xxtra = 0
                    yxtra = 0
                    If x > 2 Then
                        xxtra = 4
                    End If
                    If x > 5 Then
                        xxtra = 8
                    End If
                    If y > 2 Then
                        yxtra = 4
                    End If
                    If y > 5 Then
                        yxtra = 8
                    End If
                    cell(x, y).Location = New Point(45 + x * 90 + xxtra, 120 + y * 90 + yxtra)
                    Me.Controls.Add(cell(x, y))
                    AddHandler cell(x, y).Click, AddressOf cell_Changed
    
                Next
            Next
    
        End Sub
        Public Sub cell_Changed()
            For Each a In Me.Controls
                If a.GetType Is GetType(sudoku_textbox) Then
                    If a.Location.X < Me.PointToClient(MousePosition).X And a.Location.X + a.Width > Me.PointToClient(MousePosition).X Then
                        a.BackColor = Color.Black
                    End If
                    If a.Location.Y < Me.PointToClient(MousePosition).Y And a.Location.Y + a.height > Me.PointToClient(MousePosition).Y Then
                        a.BackColor = Color.Black
                    End If
                End If
            Next
    
        End Sub
    
    End Class

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, January 25, 2019 1:45 AM