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();
}
}
}
}
Related
Hey I got Code Written in Class Form1 And Form2. I want to call the method openkindForm() from Form2. I tried every soloution I found. I got this one at the moment which is not working it gives me a System.NullReferenceException. I do not know why it isnt working. I tried it so long but whatever I do it will not workout somehow. I would be thankfull for an answer.
Kind regards
First Class
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 FBDP00
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
submenupanel.Visible = false;
}
private void funktionenSM_Click(object sender, EventArgs e)
{
switch (submenupanel.Visible)
{
case true:
submenupanel.Visible = false;
break;
case false:
submenupanel.Visible = true;
break;
}
}
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2());
}
public Form activeForm = null;
public void openkindForm(Form childForm)
{
if (activeForm != null)
{
activeForm.Close();
}
activeForm = childForm;
childForm.TopLevel = false;
childForm.FormBorderStyle = FormBorderStyle.None;
childForm.Dock = DockStyle.Fill;
backgroundPanel.Controls.Add(childForm);
childForm.Show();
}
}
}
Class 2
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 static FBDP00.Form1;
namespace FBDP00
{
public partial class Form2 : Form
{
public Form1 testform;
public Form2()
{
InitializeComponent();
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
}
private void funktionenSM_Click(object sender, EventArgs e)
{
}
public void baukontrolleb_Click(object sender, EventArgs e)
{
testform.openkindForm(new Form3());
}
}
}
In Form2 you have defined a variable for Form1 (testform), but you don't set this anywhere. So this is why you get a Null reference error when you try to use it, because it is null!
So when you create your Form2 then you need to set this value. In your case, maybe in the constructor like this.
public Form1 testform;
private Form2(Form1 f1)
{
InitializeComponent();
testform = f1;
}
Then call it like this:
public void neuepruefungSm_Click(object sender, EventArgs e)
{
submenupanel.Visible = false;
openkindForm(new Form2(this));
}
However, looking at your openkindForm method, it seems to me that this really has nothing to do with Form1, and shares no variables, so should not be in this class.
You should either make this static (together with the activeForm variable), or make it a Singleton class instead. But certainly this should be a separate class.
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 have two different forms with one form dedicated to password input and if the password is correct open a dialog box for loading file from pc and if wrong a message box appears stating wrong password.
the problem with this is if the program is started and i clicked the button and enter the correct password and succesfully loaded the file. but if again i press the button to enter the password and i close the popup manually by X on the top, i get access to the dialog box window. i am unable to get how to stop this.
my codes are as follows
form 1:
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;
using System.Windows;
using System.Threading;
using System.Text.RegularExpressions;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
formpopup popup = new formpopup();
popup.ShowDialog();
if (formpopup.j == 1)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}
}
}
}
another form of password is:
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 WindowsFormsApplication2
{
public partial class formpopup : Form
{
public formpopup()
{
InitializeComponent();
}
public static int j = 0;
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
if (a == "1234")
{
j = 1;
textBox1.Text = string.Empty;
this.Close();
}
else
{
j = 0;
textBox1.Text = string.Empty;
this.Close();
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
yes i also tried to used form.Dispose() command but nothing happens.
You solution want redesigning: what on Earth "j" means?
// Make the class name readable, use upper case (FormPopup instead of formpopup):
public partial class FormPopup : Form {
public FormPopup() {
InitializeComponent();
}
//TODO: rename the button as well as the textbox
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == "1234")
DialogResult = System.Windows.Forms.DialogResult.OK;
else
DialogResult = System.Windows.Forms.DialogResult.Cancel;
// In case form was open as non-dialog
Close();
}
}
....
public partial class Form1 : Form {
...
private void button1_Click(object sender, EventArgs e) {
// Wrap IDisposable into using
using (FormPopup dialog = new FormPopup()) {
if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
return; // wrong password
}
// Wrap IDisposable into using
using (OpenFileDialog fileDialog = new OpenFileDialog()) {
if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
//TODO: put relevant code here
}
}
}
}
The problem is that your j is static. Make it non-static, so it would refer to an instance of your form
public int j = 0;
And then you should refer to your form in the way something like that
if (popup.j == 1)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
if (openfiledialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
}
}
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 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