When I try to connect two forms, I successfully connect them, but the problem is that when I open Form2 from Form1 then Form2 opens but Form2 does not show its buttons, text etc.
I used these instructions.
I am using Visual Studio 2012. I believe Visual Studio is doing this mistakenly and I might have to install new visual studio.
Form1.cs
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 Login_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.Show();
}
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}
}
}
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;
namespace Login_Form
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
}
private void button4_Click(object sender, EventArgs e)
{
}
private void button3_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
}
Now remove following code snippet from Form1 class
// Create Form2.
public class Form2 : Form
{
public Form2()
{
Text = "Form2";
}
}
Related
I am having trouble with using serialPort1 in both of my forms. I have opened the serialPort but it just isn't receiving or sending anything. Am I not able to do it this way or is there a simpler way of doing this. Please let me know.
Form 1 Code
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 Arduino_GUI
{
public partial class Form1 : Form
{
public delegate void d1(string indata);
public Form1()
{
InitializeComponent();
serialPort1.Open();
}
private void onButton_Click(object sender, EventArgs e)
{
serialPort1.Write("A"); // on
}
private void offButton_Click(object sender, EventArgs e)
{
serialPort1.Write("a"); // off
}
private void btnOForm2_Click(object sender, EventArgs e)
{
serialPort1.Close();
Form2 f2 = new Form2();
f2.Show();
this.Hide();
}
}
}
Form 2 Code
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 Arduino_GUI
{
public partial class Form2 : Form
{
public delegate void d1(string indata);
public Form2()
{
InitializeComponent();
serialPort1.Open();
}
private void btnMain_Click(object sender, EventArgs e)
{
serialPort1.Close();
Form1 f1 = new Form1();
f1.Show();
this.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
serialPort1.WriteLine("2");
}
private void serialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e) // receiving data
{
String indata = serialPort1.ReadLine();
d1 writeit = new d1(Write2Form);
Invoke(writeit, indata);
}
public void Write2Form(string indata)
{
char firstchar;
string test;
firstchar = indata[0];
switch (firstchar)
{
case 'r':
test = indata.Substring(1);
label1.Text = test;
break;
}
}
private void Form2_FormClosing(object sender, FormClosingEventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
f1.Close();
}
}
}
Arduino Code
String data;
char d1;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
// put your main code here, to run repeatedly:
if(Serial.available()){
data = Serial.readString();
d1 = data.charAt(0);
switch(d1){ // select action based on first character
case '2': // Do when Form2 opens
Serial.println('r');
break;
}
}
}
Thank you for going through this all and I hope you can help me.
:)
I am currently using WinForms Application and it seems that I have ran into a problem. I have a button that when pressed it outputs a link in a RichTextBox. However, the link is highlighted in blue but when I click it does not open in my browser. Here is the code(Note that https://something.com/ represents an actual link.):
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
richTextBox1.Text = "https://something.com/" + textBox1.Text;
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
This question already has answers here:
Send values from one form to another form
(20 answers)
Closed 7 years ago.
I am having a problem adding text from my Form 2, to the List item in Form 1. Could anyone help please, Sorry if it's a bit jumbled, I'm new with coding :) What I am trying to achieve is a to-do list application, in the 2nd form, text should be entered in a text box - when the user clicks enter, this text should be entered into the list item.
Another question is, in this case would I be better using a list item or a list box?
Here is the code:
FORM 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;
namespace ToDoList
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public void button2_Click(object sender, EventArgs e, string tbtext)
{
//Form1 form1 = new Form1();
//form1.listBox1.Items.Add(tbtext);
}
public void button1_Click(object sender, EventArgs e)
{
this.Close();
}
public void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
ListViewItem item = new ListViewItem(textBox1.Text);
Form1 f1 = new Form1();
f1.listView1.Items.Add(item);
}
private void textBox1_TextChanged_1(object sender, EventArgs e)
{
}
}
}
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;
namespace ToDoList
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void filesToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
public void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
}
public void button2_Click(object sender, EventArgs e)
{
}
public void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void F1Button1_Click(object sender, EventArgs e)
{
}
private void tB1_TextChanged(object sender, EventArgs e)
{
}
public void listView1_SelectedIndexChanged_1(object sender, EventArgs e)
{
}
}
}
You create different object Form1, and the object is destroyd after you quit button2_Click. Is it local variable.
You need to create the datasource for your TODO list and propaget it to both forms, for example by method Set DataSource. Then you have one object accesible by
both forms.
I'm working on a WindowsFormProject in C# of which carry the code below:
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 Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
}
}
}
this is code for 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;
namespace WindowsFormsApplication2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.textBox1.Text = "prova";
f1.Refresh();
}
}
}
end this is code for Form2.
the goal is to write a text in the textbox of the form 1, I set the access modifier as public, but does not work
Any solution?
Try this
In Form2:
public Form _parent;
public Form2(Form Parent)
{
InitializeComponent();
this._parent = Parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova";
}
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this);
f.Show();
}
Note that in the "button1_Click" in Form2 you are creating a new instance of "Form1" so Form2 does'nt really "knows" the Form1 that is allready open,
You should pass this object reference (Form1) to Form2,
Similar to the solution of Marcio but instead of passong the inherite class "Form" you should pass "Form1"
In Form2:
public Form1 _parent;
public Form2(Form1 parent) //<-- parent is the reference for the first form ("Form1" object)
{
InitializeComponent();
this._parent = parent;
}
private void button1_Click(object sender, EventArgs e)
{
this._parent.textBox1.Text = "prova"; // you won't get an exception here because _parant is from type Form1 and not Form
}
In Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(this); //<- "this" is the reference for the current "Form1" object
f.Show();
}
I have two form and i want to change backGround of first form from 2nd form. i have already choose a backGround image for form1 and button1 in form2, but nothing happens. thanx in advance (Windows Form)
1st form:
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.ShowDialog();
}
}
}
2nd Form:
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 WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
frm1.BackgroundImage = button1.BackgroundImage;
}
}
}
In your second form, add a private member that will hold a reference to the first form:
private Form _form1 = null;
Then in the constructor for Form2, allow that reference to be passed in:
public Form2(Form form1)
{
InitializeComponent();
_form1 = form1;
}
Now, in that button click handler, you can:
private void button1_Click(Object sender, EventArgs e)
{
_form1.BackgroundImage = button1.BackgroundImage;
}
An alternative approach would be to add a method to Form1 that receives the image to be set as background. Assume the same _form1 reference exists in Form2, you add this to Form1:
public void ChangeBGImage(Image bgImage)
{
this.BackgroundImage = bgImage;
}
And from Form2, you call it:
private void button1_Click(Object sender, EventArgs e)
{
_form1.ChangeBGImage(button1.BackgroundImage);
}
try this,
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
//check if button1 clicked and then change the background
if(frm2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.BackgroundImage = frm2.GetBackImage();
}
}
}
}
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;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Form1 frm1 = new Form1();
//frm1.BackgroundImage = button1.BackgroundImage;
this.DialogResult = System.Windows.Forms.DialogResult.OK;
}
public Image GetBackImage()
{
return this.button1.BackgroundImage;
}
}
}
The problem is that you haven't access to the form1 from form2 to change it. If you want to change something in form1 you shouldn't create new instance of Form1. You should get the instance in the constructor.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.ShowDialog();
}
}
public partial class Form2 : Form
{
Form1 frm1;
public Form2(Form1 frm1)
{
InitializeComponent();
this.frm1 = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
frm1.BackgroundImage = button1.BackgroundImage;
}
}