C++ .NET code translation for Visual Basic .NET

José Carlos 886 Reputation points
2023-09-02T00:22:55.4266667+00:00

Hi friends.

I'm passing a C++ Winforms program to Visual Basic .NET. I still don't know about Linq commands. Here is the code in C++ Winforms.

Thanks a lot for the help.

int numjogo, x = 0;
totaljogosjogados = 0;
tempremio = 0;
for each(Control^ ctrl in panel25->Controls)
{
   CheckBox^ c = dynamic_cast<CheckBox^>(ctrl);
   if (c && c->Checked)//se tiver marcado o checkbox
   {
     numjogo = Convert::ToInt32(c->Text); 
     TextBox^ tb = (TextBox^)Controls->Find("textBox" + numjogo, true)[0]; 
     tb->Text = Convert::ToString(Subverificaacertos(numjogo)); 
     if (Convert::ToInt32(tb->Text) >= 11)
     {
        tb->BackColor = Color::Blue;
        tb->ForeColor = Color::Yellow;
        jogosjogados[x, 0] = 1;
        jogosjogados[x, 1] = numjogo;
     }
   }
}
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,043 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,689 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,714 questions
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-09-02T11:55:55.1933333+00:00

    Hi

    There are several things that would prevent a conversion.

    1. There are unidentified variables withing the code
    2. There are unidentified functions/subs invoked within the code.

    However, based on partial understanding of the code, maybe this will help.

    Start a new test Project and in the Designer, put a Panel25 containing some CheckBoxes named numerically, outside the Panel, on the Form, put a Textbox named 'TextBoxN' where N corresponds to a CheckBox and finally a Button. (see image below)

    Run the code and Check or Uncheck some CheckBoxes and click Button and watch if corresponding TextBox(s) follow desired effect.

    This code has NO EXCEPTION CHECKING.

    111

    ' default Form1 with:
    ' Panel25 containing some number of CheckBoxes
    ' with numeric names 
    
    ' Corresponding number of TextBoxes
    ' Button1 to do operation
    Option Strict On
    Option Explicit On
    
    Public Class Form1
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
    		' set all TextBoxes to default
    		For Each c As Control In Me.Controls
    			Dim tb As TextBox = TryCast(c, TextBox)
    			If Not tb Is Nothing Then
    				With tb
    					.Text = String.Empty
    					.BackColor = Color.White
    					.ForeColor = Color.Black
    				End With
    			End If
    		Next
    
    		' search for Checked CheckBoxes and
    		' set corresponding TextBox properties
    		For Each c As Control In Panel25.Controls
    			If c.GetType = GetType(CheckBox) Then
    				If DirectCast(c, CheckBox).Checked Then
    					Dim n As Integer = 0
    					If Integer.TryParse(c.Text, n) Then
    						Dim tbn As String = "TextBox" & n.ToString
    						Dim cont() As Control = Me.Controls.Find(tbn, False)
    						Dim tb As TextBox = DirectCast(cont(0), TextBox)
    						With tb
    							.Text = n.ToString
    							.BackColor = Color.Blue
    							.ForeColor = Color.Yellow
    						End With
    					End If
    
    				End If
    			End If
    		Next
    
    	End Sub
    End Class
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.