Available Serial ports are not shown in Visual studio - c#

I am recently working on a project for Serial port communication, I want to show the available serial ports in a comboBox, But while running it is not showing the available list of com ports.
My code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace Serial_application
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
getPortsNames();
}
void getPortsNames()
{
string[] ports = SerialPort.GetPortNames();
comboBox1.Items.AddRange(ports);
}
}
}

There are no old style com ports on most modern computers, they are USB converters. If the USB device driver has not been installed there are no ports listed in the computer. Just solved this problem for myself.

Related

How do I get a Controls.ItemsControl in an Form

I have a Controls.ItemsControl (actually a HelixViewport3D) and would like to add it inside a Forms.UserControl.
I already tried this.Controls.Add(new HelixViewport3D());
Where this is a derivative from UserControl
however this resulted in an error HelixToolkit.Wpf.HelixViewport3D' to 'System.Windows.Forms.Control. Which makes sense given that they don't inherit anything from one another. Is there a sort of wrapper class in these situations?
using HelixToolkit.Wpf;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
public partial class HelixForm : Form
{
public HelixForm()
{
InitializeComponent();
var _3Dcntr = new HelixViewport3D();
this.Controls.Add(_3Dcntr);//doesn't work
//what we worked out so far
var elemHost = new ElementHost();
}
}
I have also noticed that it's supposed to be possible to host a WPF in a winform using ElementHost.Child but adding this variable/function cannot be found in my example code(missing something?), System.Windows.Forms.Integration.ElementHost tells me that System.Windows.Forms.Integration is missing.
Hosting WPF Composite Control in Winform
Mixing WPF in a Winforms app is not recommended but Microsoft provides a ElementHost in WindowsFormsIntegration namespace.
Here is a more indepth demo of them working
Mixing WPF and Winforms
(sorry for the simple answer I have not mixed them before.)

What is the proper namespace for IPHostEntry and DNS?

My program uses IPHostEntry and DNS, i know i have to add System.Object, System.Net.IPHostEntry, System.Net.DNS to my project reference but i can not find it in the list.
After the System.IO.Log the next to follow is System. Management (no System.Object)
After the System.Net is System.Numerics (no System.Net.IPHostEntry or System.Net.DNS)
I am using .NET 4.0
What do i do to add the following reference and make it work?
i have the following codes
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Threading;
using System.Windows.Forms;
public string LocalIPAddress()
{
IPHostEntry host;
string localIP = "";
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily == AddressFamily.InterNetwork)
{
localIP = ip.ToString();
break;
}
}
return localIP;
}
Classes belong to assemblies and exist in namespaces.
You reference assemblies. You specify classes in your code by using their full name (namespace + class name) or you can add certain namespaces in using directives.
So you need to find, from the documentation, where each class belongs and then ensure that you're referencing those assemblies:
IPHostEntry and Dns belong to the System.Net namespace and are in the System assembly.
Object belongs to the System namespace and is in mscorlib.
You almost certainly already have references to these assemblies. But you might want to add using directives at the head of your code file.
Add:
using System.Net;
to your using directives (you already have a using System; for referring to Object)

C# - Event is not detected on Windows XP - USB drive

I´m new to C# -- started three days ago because of an especific need regarding usb drives. Reading here and there I could have the following code to work. What I want is to know when an user inserts a pendrive in the usb port.
The only problem is the that on XP32 (the only XP I tested) the event will never be detected. On Windows 7 it runs perfectly.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
using System.Diagnostics;
using System.Threading;
using System.IO;
using Microsoft.Win32;
using System.Security.Permissions;
namespace X
{
class Program
{
static void Main(string[] args)
{
ManagementEventWatcher watcher = new ManagementEventWatcher();
WqlEventQuery query = new WqlEventQuery("SELECT * FROM Win32_VolumeChangeEvent WHERE EventType = 2");
watcher.Query = query;
watcher.Start();
watcher.WaitForNextEvent();
// DO something if a pen drive (or any storage device) is inserted.
// Works fine on Windows 7
// XP will ignore the event...
}
}
}
Any suggestions will be very welcome!
Regards,
Sergio
Looks like you need XP service pack 3 for it to work (SP2 on x64).

C# Blocking IP by modifying Windows 7 Firewall

I'm starting to build an anti-ddos application in C#, it's going to block IP's by checking how many connections there are, in an amount of time, but I can't find whats needed for NetFwMgrType.
Here's code, I just started:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NETCONLib;
using NATUPNPLib;
using NetFwTypeLib;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
Type NetFwMgrType = Type.GetTypeFromProgID("HNetCfg.FwMgr", false);
INetFwMgr mgr = (INetFwMgr)Activator.CreateInstance(NetFwMgrType);
bool Firewallenabled = mgr.LocalPolicy.CurrentProfile.FirewallEnabled;
public Form1()
{
InitializeComponent();
}
}
}
The errors I get is:
Error 1 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.NetFwMgrType'
Error 2 A field initializer cannot reference the non-static field, method, or property 'WindowsFormsApplication1.Form1.mgr'
(I added references: \Windows\System32\hnetcfg.dll and \Windows\System32\FireWallAPI.dll)
Thanks for answer.
Oh and if you know how to block IPs with Firewall modifying, as well, it would save me a lot of googling:)

Determining labels from another partial class/windows form

I'm bit new at this so excuse me if my question is a bit novice. I'm designing a Windows form application to basically replace an old excel spreadsheet then email system(not that its much of a system). After the forms are filled out the answers are saved in terms of public variables, which is fine since it's a small program. Im having trouble referencing my variable from another windows form. Basically, I would like to have the filled out form close and a new "review" window pop up. I'm just using labels that will be show what the value of the variable is. If it was in the same class it wouldn't be a problem but im using two different forms that are partial classes of the same namespace. A bit of code:
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OPS_PictureLoader
{
public partial class Points_Screen : Form
{
public int DevJobStandardsTotal = 0;
And the label I would like to show a "0"(or whatever the program has added to it)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace OPS_PictureLoader
{
public partial class Review : Form
{
public Review()
{
InitializeComponent();
label14.Text = DevJobStandardsTotal;
Again, thanks and feel free to tell me Im totally wrong :D
You need to pass the instance of the first form to the second form's constructor:
public Review(Points_Screen owner)
{
InitializeComponent();
label14.Text = owner.DevJobStandardsTotal;

Categories

Resources