I have a project with some classes. I wanted to change one's(classA) design so I created a new project in expression blend 4. I want to use this in order to do this I need to access that project from main project.
I have in that class:
class1 c = new class1();
c.pass = read.Value;
if (c.ShowDialog() == DialogResult.OK)
{
read.Close();
return c.status;
}
I tried this https://stackoverflow.com/a/3556586/2763129 but it doesn't work. Is it even possible?
ClassA
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace momo
{
public partial class ClassA : Form
{
public string pass;
public bool status;
public ClassA()
{
status = false;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text.Equals(pass))
{
status = true;
this.Close();
}
else
{
MessageBox.Show("Errör", MessageBoxButtons.OK, MessageBoxIcon.Error);
status = false;
}
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
my wpf which I try to ipmlement
using System;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows;
using System.Windows.Controls;
//using System.Windows.Forms;
namespace ZGNH
{
public class ayat
{
//public checkpassword()
//{
// status = false;
// this.InitializeComponent();
//}
public string pass;
public bool status;
}
public partial class MainWindow : Window
{
ayat saf = new ayat();
string pass;
bool status;
//public string pass;
//public bool status;
public MainWindow()
{
//InitializeCo
this.InitializeComponent();
// saf.pass = pass;
//saf.status = status;
// Insert code required on object creation below this point.
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
this.Close();
//
}
private void Button1_Click(object sender, System.Windows.RoutedEventArgs e)
{
//pass="c";
//password
if (pboxxx.Password.Equals(pass))
{
status = true;
//PasswordBox s =new PasswordBox();
this.Close();
}
else
{
Window1 s = new Window1();
s.ShowDialog();
//MessageBox.Show("Errör", MessageBoxButtons.OK, MessageBoxIcon.Error);
status = false;
}
}
}
}
edit : I tried adding this to mainwindow.xaml.cs
public void show()
{
Window.Show();
}
but it gives The type or namespace name 'MainWindow' could not be found (are you missing a using directive or an assembly reference?) .Visual Studio 2012\Projects\momolocker\momo\memoform.cs 14 7 Momo
Related
I am developing a software in C #, and I need to list the names of the available serial ports, but when using (PortName ()) the Visual Studio does not recognize the (PortName ()) and the error. What could be wrong? I'm using Visual Studio 2019 and C #.
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;
namespace Control
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ListCOMs()
{
int i;
bool qtde;
i = 0;
qtde = false;
if (cbPorts.Items.Count == mySerialPort.PortName().Length)
{
foreach (string s in mySerialPort.PortName())
{
if (cbPorts.Items[i++].Equals(s) == false)
{
qtde = true;
}
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void timerCOM_Tick(object sender, EventArgs e)
{
ListCOMss();
}
}
}
Thanks
I want to disable my form when another opened, and enabled when closed. But another forms may close itselves and open new forms so I wite a function which gets boolean value and enables and disables forms and keys. When opening new for it works but when closed that form not working.
Here is main form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Win32;
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
public void anotherForm(bool anotherform)
{
if (anotherform)
{
this.Enabled = false;
keysActivated = false;
}
else
{
this.Enabled = true;
keysActivated = true;
}
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
anotherForm(true); // Enters and working.
settings .Show();
}
}
}
}
}
This is my settings form:
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Diagnostics;
using System.Runtime.InteropServices;
namespace Automation_v1_0_0
{
public partial class FormAyarlarMenu : Form
{
public FormSettings()
{
InitializeComponent();
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
FormMain fm = new FormMain();
fm.anotherForm(false); // Enters but not working.
this.Close();
}
}
}
}
you are making this complex while it can be simple
in your mainform do this :
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
using (FormSettings settings = new FormSettings())
{
settings.ShowDialog();
}
}
}
and just drop all other code you dont need that.
Drop the method anotherform, you dont need that.
in your settings form just do this
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
}
}
Main form:
public partial class FormMain : Form
{
bool keysActivated = true;
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (keysActivated)
{
if (e.KeyCode == Keys.F7)
{
this.Hide();
FormSettings settings = new FormSettings(this);
settings.Show();
}
}
}
}
Setting form:
public partial class FormSettings : Form
{
private FormMain formMain;
public FormSettings(FormMain formMain)
{
InitializeComponent();
this.formMain = formMain;
}
private void FormSettings_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Escape || e.KeyCode == Keys.F1)
{
this.Close();
formMain.Show();
}
}
}
Your problem is that you instantiated new FormMain object instead of using existing one! The solution is that you pass the current instance of FormMain to setting form to use it.
I have solved problem. Here is my code:
namespace Automation_v1_0_0
{
public partial class FormMain : Form
{
public FormMain()
{
InitializeComponent();
}
private void FormMain_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.F7)
{
FormSettings settings = new FormSettings();
settings.ShowDialog();
}
}
}
}
I'm trying to make a windows form application in Visual Studio in C# and the purpose is to be able to communicate with an Arduino microcontroller. Right now I have 2 forms (Form1 and Form2) and I need to send and receive data from both windows to the microcontroller. I have defined a class SerialComms.cs in which there I start my serialPort and I have been able to send messages from both forms to the Arduino.
The problem is I don't know how to go about to receiving data from the Arduino as soon as the data is received. If I were to create the serialPort from one of the forms i would be able to create a function for DataReceived that will run anytime data is received but I don't know how to do that when I start my serialPort from a class. Any help?
Here is my code for both forms and my serialComms class:
Form1
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;
using System.IO;
namespace Test_Comms
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
connectToArduino();
}
void connectToArduino()
{
try { SerialComms.SerialPort.Close(); }
catch { }
getAvailablePorts();
for (int i = 0; i < COMcomboBox.Items.Count; i++)
{
string inputMsg = "None";
string commport = COMcomboBox.GetItemText(COMcomboBox.Items[i]);
//Wait for Arduino message sent
//If not receieved go to next port
SerialComms.SerialPort.PortName = commport;
SerialComms.SerialPort.BaudRate = 9600;
SerialComms.SerialPort.Open();
try { SerialComms.SerialPort.Open(); }
catch { }
int counter1 = 0;
while (counter1 < 100)
{
inputMsg = SerialComms.SerialPort.ReadLine();
try { inputMsg = SerialComms.SerialPort.ReadLine(); }
catch { }
counter1++;
if (inputMsg == "Arduino")
{
counter1 = 1000;
}
}
if (inputMsg != "Arduino")
{
try { SerialComms.SerialPort.Close(); }
catch { }
}
else
{
i = COMcomboBox.Items.Count;
SerialComms.SerialPort.WriteLine("Connection Established");
toolStripStatusLabel1.Text = "Connected";
break;
}
}
}
void getAvailablePorts()
{
COMcomboBox.Items.Clear();
String[] ports = SerialPort.GetPortNames();
COMcomboBox.Items.AddRange(ports);
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
}
private void button3_Click(object sender, EventArgs e) //Opens second form
{
Form2 settingsForm = new Form2();
settingsForm.ShowDialog();
}
}
}
Form2
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;
using System.IO;
namespace Test_Comms
{
public partial class Form2 : Form
{
public static string text = "Test text";
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED ON");
}
private void button2_Click(object sender, EventArgs e)
{
SerialComms.SerialPort.WriteLine("LED OFF");
button2.Text = text;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
SerialComms.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO.Ports;
using System.IO;
namespace Test_Comms
{
public static class SerialComms
{
//public static delegate void DataReceivedEventHandler(object sender, ReceivedEventArgs e);
//public static event DataReceivedEventHandler DataReceived;
private static SerialPort _serialPort = new SerialPort();
public static SerialPort SerialPort
{
get { return _serialPort; }
set { _serialPort = value; }
}
}
}
i create a simple message box to get user input and set the result into webbrowser of previous form.
this is my MsgInput source code
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;
public partial class MsgInput : Form
{
private readonly Main mainForm;
public string input_type;
string script;
public MsgInput()
{
this.mainForm = mainForm;
InitializeComponent();
}
private void MsgInput_Load(object sender, EventArgs e)
{
if (input_type == "echo")
{
richTextBox1.Text = "Echo : void echo ( string $arg1 [, string $... ] )";
}
}
private void btnOk_Click(object sender, EventArgs e)
{
if (input_type == "echo")
{
script = mainForm.webBrowser1.DocumentText;
if (chkNewLine.Checked == true)
{
script += "\n";
}
script += "echo " + txtInput.Text;
mainForm.webBrowser1.DocumentText = script;
this.Close();
}
}
}
and i not add anything in first form just set the webbrowser modifiers to public.
when i debug. null return when i try to submit an text
Main Form
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 EasyPHP
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
private void echoToolStripMenuItem_Click(object sender, EventArgs e)
{
var msg = new MsgInput();
msg.input_type = "echo";
msg.Show();
}
private void Main_Load(object sender, EventArgs e)
{
webBrowser1.DocumentText = "<pre>";
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
}
}
I am assuming you are getting a null reference exception. it will be useful if you post the main form code. From what i understood, you should be passing in the main form reference and that's why you are getting a null reference.
In your code, change the constructor as below (ParentForm is whatever is the class name of your parent form)
public MsgInput(ParentForm mainForm)
{
this.mainForm = mainForm;
InitializeComponent();
}
and in the main form
MsgInput frm = new MsgInput(this);
frm.input_type = "echo";
frm.Show(this);
Else please share the full code and we can quickly help
I have some problems to use the MSTSCLib to connect from 1 PC to another one.
It's working with Servers but not with normal Workstations...
private void btn_connect_Click(object sender, EventArgs e)
{
try
{
rdp_control.Server = tbx_servername.Text;
rdp_control.Connect();
tabPage1.Text = "Connected";
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString());
}
}
private void btn_disconnect_Click(object sender, EventArgs e)
{
if (rdp_control.Connected.ToString() == "1")
{
rdp_control.Disconnect();
}
}
Both client and server applications are in the Same Network under the same NAT. Problem is the certificate,... I need to find a way to include the certificate. With the normal Remote Desktop from Windows, you see a MessageBox with the question:"Do you want to use this certificate.... blablabla" But this is not coming up with the RDP function in c#
Any Ideas?
Thanks B.R.
Following code shows a simple RDP client and server.
RDP Server
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 RDPCOMAPILib;
using AxMSTSCLib;
using System.Runtime.InteropServices;
namespace TCP_to_RDP_Converter
{
public partial class Form1 : Form
{
public static RDPSession currentSession = null;
public static void createSession()
{
currentSession = new RDPSession();
}
public static void Connect(RDPSession session)
{
session.OnAttendeeConnected += Incoming;
session.Open();
}
public static void Disconnect(RDPSession session)
{
session.Close();
}
public static string getConnectionString(RDPSession session, String authString,
string group, string password, int clientLimit)
{
IRDPSRAPIInvitation invitation =
session.Invitations.CreateInvitation
(authString, group, password, clientLimit);
return invitation.ConnectionString;
}
private static void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
/// <summary>
/// Handle the form items
/// </summary>
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
createSession();
Connect(currentSession);
textConnectionString.Text = getConnectionString(currentSession,
"Test","Group","",5);
}
private void button2_Click(object sender, EventArgs e)
{
Disconnect(currentSession);
}
}
}
In order to use the RDP communication library you need to add rdpcompapi and Microsoft windows terminal services, form the COM references.
RDP Client
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 RDPCOMAPILib;
using AxRDPCOMAPILib;
namespace Simple_RDP_Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void Connect(string invitation, AxRDPViewer display, string userName, string password)
{
display.Connect(invitation, userName, password);
}
public static void disconnect(AxRDPViewer display)
{
display.Disconnect();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
Connect(textConnectionString.Text, this.axRDPViewer, "", "");
}
catch (Exception)
{
MessageBox.Show("Unable to connect to the Server");
}
}
}
}
you can add reference to the AxRDPCOMAPILib by importing RDP viewer class component to the main form.
Full project can be downloaded from here [Download]:http://sandaruwmp.blogspot.com/2014/05/remote-desktop-application-with-rdp.html
use this....
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 MSTSCLib;
namespace RemoteTool
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Server = textBox1.Text;
MSTerminalServiceControl1.UserName = textBox2.Text;
IMsTscNonScriptable secured = (IMsTscNonScriptable)MSTerminalServiceControl1.GetOcx();
secured.ClearTextPassword = textBox3.Text;
MSTerminalServiceControl1.Connect();
}
private void button2_Click(object sender, EventArgs e)
{
MSTerminalServiceControl1.Disconnect();
}
}
}