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)
{
}
}
Related
as title, i have a button that load an image in a picturebox, then when i click on it i want it to display the coordinates in two textbox, only proble...it display the coordinates when i click outside of the picturebox and not when i click in.
i already tried the solution in this site but it doesn't seems to work so far.
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 WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
protected override void OnMouseClick(MouseEventArgs e)
{
base.OnMouseClick(e);
textBox1.Text = e.X.ToString();
textBox2.Text = e.Y.ToString();
}
private void BtnBrowse_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "All jpg files (*.jpg)|*.jpg";
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
pictureBox1.ImageLocation = openFileDialog1.FileName;
}
}
}
}
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 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 the following form. The first button opens a text file and displays the file in the rich text box in the form. The second button opens another window. What I want is for that window to be pre-populated with the data that is in that text file...
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 HomeInventory2
{
public partial class Form2 : Form
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, System.EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openFileDialog1.FileName, RichTextBoxStreamType.RichText);
}
}
private void button2_Click(object sender, EventArgs e)
{
Application.Run(new Form1());
}
}
}
The form that needs to be populated
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 HomeInventory2.Domain;
using HomeInventory2.Business;
namespace HomeInventory2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void submitButton_Click(object sender, EventArgs e)
{
CreateInventory create = new CreateInventory();
create.ItemAmount = textBoxAmount.Text;
create.ItemCategory = textBoxCategories.Text;
create.ItemProperties = textBoxValue.Text;
create.ItemValue = textBoxValue.Text;
InventoryMngr invtryMngr = new InventoryMngr();
invtryMngr.Create(create);
}
}
}
Create a new constructor which takes a string overload. When you open the new Form, pass in the text date and fill the textbox.
//in the new form that opens up
public Form1(string prepopulated)
{
InitializeComponent();
myRichTextbox.Text = prepopulated;
}
And call it from your click event like this:
//in the first form
private void button2_Click(object sender, EventArgs e)
{
Application.Run(new Form1(richTextBox1.Text));
}
If your content is more complicated than a simple text file, you can use RichTextBox.Document instead and pass that instead of the string. Change the overload to
Form1(FlowDocument prepopulated)
and call it like this
Application.Run(new Form1(richTextBox1.Document));
I want to transfer my code to a WinForms.
I know how to create buttons, text, etc... and how to operate them.
The problem is that I made my code in a windows application, and I want to transform it to WinForms application. I don't know how to copy and paste the whole code, cause in the WinForms application there is no main method..
Here is my code that I am trying to transfer to WinForms:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
TcpClient connection = new TcpClient("127.0.0.1", 5000);
StreamReader sr = new StreamReader(connection.GetStream());
StreamWriter sw = new StreamWriter(connection.GetStream());
string name2 = "";
while (true)
{
Console.WriteLine("Enter your name and press submit");
name2=Console.ReadLine();
if (name2 != "")
{
sw.WriteLine(name2);
break;
}
}
Console.WriteLine("Loop is over");
Thread t2 = new Thread(Reader);
t2.IsBackground = true;
t2.Start(connection);
while (true)
{
sw.WriteLine(Console.ReadLine());
sw.Flush();
}
}
public static void Reader(object o)
{
TcpClient con = o as TcpClient;
if (con == null)
return;
StreamReader sr = new StreamReader(con.GetStream());
while (true)
{
for (int i = 0; i < 5; i++)
{
Console.WriteLine();
}
Console.WriteLine(sr.ReadLine());
Console.WriteLine();
}
}
}
}
Here is the Form that I have created:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
In windows form application there is main method but it is in program.cs file and its starts your default form after you program starts execution.
You can add the the textbox in form and on button event get the text of the textbox.
String username = textboxusername.Text;
Similarly you can do all the things you want.
The code that you have written before was for console application and that is not the same for the windows form application.
You have to code differently for this.