منتدى فيجوال بيسك لكل العرب | منتدى المبرمجين العرب

نسخة كاملة : خطاء عند تحويل الكود
أنت حالياً تتصفح نسخة خفيفة من المنتدى . مشاهدة نسخة كاملة مع جميع الأشكال الجمالية .
السلام عليكم ورحمة الله

لدي برنامج سطح مكتب مصمم بلغة سي شارب

اريد تحويلة الي VB.NET

استخدمت مواقع التحويل وظهر خطائيين بالبرنامج :

'ReceiveMsg' is not an event of 'Form1'.1-

2- Function '<anonymous method>' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used

الخطا الاول حاولت بالطريقة دية

Public Event ReceiveMsg As EventHandler


حدث خطا بكود فورم اللواد  بقيمة ReceiveMsg


وظهر الخطا


'Public Event ReceiveMsg(sender As Object, e As System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.


الكود السي شارب

كود :
{
   public partial class Form1 : Form
   {
       public EventHandler ReceiveMsg ;
       DateTime LastReceive;

       public Form1()
       {
           InitializeComponent();
           ReceiveMsg += ReceiveMsg_Handler;
           LastReceive = DateTime.Now;
           IPHostEntry ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());

           IPAddress ipAddress = ipHostInfo.AddressList.Where(x => x.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
             .FirstOrDefault();
           lblip.Text = ipAddress.ToString();

       }
       private void ReceiveMsg_Handler(object sender, EventArgs e)
       {
         
           LastReceive = DateTime.Now;
           Timer1_Tick(null, null);
           if (sender.ToString().Contains("ping"))
               return;
           var Barcode = sender.ToString().Split('-');
           if (Barcode.Length > 1)
           {
               txtbarcode.Invoke((MethodInvoker)delegate
               {
                   // Running on the UI thread
                   txtbarcode.Text = Barcode[0];
                   txtname.Text = Barcode[1].Substring(0, Barcode[1].IndexOf("<EOF>"));
               });
               SendKeys.SendWait(Barcode[0]);
           }
       }
       Thread Serverthread;
       private void Form1_Load(object sender, EventArgs e)
       {
           AsynchronousSocketListener.ReceiveMsg = ReceiveMsg;

            Serverthread = new Thread(new ThreadStart(() => {
               AsynchronousSocketListener.StartListening();
           }));
           Serverthread.Start();
       }


       private void Timer1_Tick(object sender, EventArgs e)
       {
           bool res = DateTime.Now - LastReceive >= TimeSpan.FromSeconds(2);
           lblStatus.Invoke((MethodInvoker)delegate
           {
               lblStatus.Text = res ? "Disconnected": "Connected";
               lblStatus.ForeColor = res ? Color.Red : Color.Green;
           });
               return;
       }

       private void Form1_FormClosed(object sender, FormClosedEventArgs e)
       {
           timer1.Stop();
           Serverthread.Abort();
       }
   }
بعد التحويل وظهور المشاكل


كود :
Public Partial Class Form1
   Inherits Form

   Public ReceiveMsg As EventHandler
   Private LastReceive As DateTime

   Public Sub New()
       InitializeComponent()
      AddHandler ReceiveMsg, AddressOf ReceiveMsg_Handler
       LastReceive = DateTime.Now
       Dim ipHostInfo As IPHostEntry = Dns.GetHostEntry(Dns.GetHostName())
       Dim ipAddress As IPAddress = ipHostInfo.AddressList.Where(Function(x) x.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork).FirstOrDefault()
       lblip.Text = ipAddress.ToString()
   End Sub

   Private Sub ReceiveMsg_Handler(ByVal sender As Object, ByVal e As EventArgs)
       LastReceive = DateTime.Now
       Timer1_Tick(Nothing, Nothing)
       If sender.ToString().Contains("ping") Then Return
       Dim Barcode = sender.ToString().Split("-"c)

       If Barcode.Length > 1 Then
           txtbarcode.Invoke(CType(Function()
                                       txtbarcode.Text = Barcode(0)
                                       txtname.Text = Barcode(1).Substring(0, Barcode(1).IndexOf("<EOF>"))
                                   End Function, MethodInvoker))
           SendKeys.SendWait(Barcode(0))
       End If
   End Sub

   Private Serverthread As Thread

   Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
       AsynchronousSocketListener.ReceiveMsg = ReceiveMsg
       Serverthread = New Thread(New ThreadStart(Function()
                                                     AsynchronousSocketListener.StartListening()
                                                 End Function))
       Serverthread.Start()
   End Sub

   Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As EventArgs)
       Dim res As Boolean = DateTime.Now - LastReceive >= TimeSpan.FromSeconds(2)
       lblStatus.Invoke(CType(Function()
                                  lblStatus.Text = If(res, "Disconnected", "Connected")
                                  lblStatus.ForeColor = If(res, Color.Red, Color.Green)
                              End Function, MethodInvoker))
       Return
   End Sub

   Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As FormClosedEventArgs)
       timer1.[Stop]()
       Serverthread.Abort()
   End Sub
End Class