I am playing around in visual studio and getting to know C# better. I am coming from an intermediate background knowledge of Java.
I have produced a very simple windows form application. The user clicks on a button, the button takes them to another screen, the user types into a textbox and presses a button in which that button will display what the user typed in; in the form. This is the code:
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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
Form2 userinputForm;
public Form2 getSetForm2 {
get { return userinputForm; }
set { userinputForm = value; }
}
Form1 homeFormObj;
public Form1 getSetForm1 {
get { return homeFormObj; }
set { homeFormObj = value; }
}
public Form1()
{
InitializeComponent();
getSetForm2 = new Form2();
getSetForm1 = this;
getSetForm2.formOnePublicObj = getSetForm1;
}
internal void displayUserInput(string name)
{
Label l = new Label();
l.Text = name;
panel1.Controls.Add(l);
}
private void button1_Click(object sender, EventArgs e)
{
userinputForm.Show();
}
}
}
Form2.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 WindowsFormsApplication3
{
public partial class Form2 : Form
{
Form1 formOneObj;
public Form1 formOnePublicObj {
get { return formOneObj; }
set { formOneObj = value; }
}
public Form2()
{
InitializeComponent();
}
List<string> userinputs = new List<string>();
private void button1_Click(object sender, EventArgs e)
{
string name = textBox1.Text;
formOnePublicObj.displayUserInput(name);
}
}
}
The error occurs the second time the user presses the button to go to form2. it occurs on the .show() method.
(P.S I coded like this to see how I can pass data from one windows form to another hence the getters and setters on the form objects).
Well, userinputform is never set and so is null. As such I don't understand why it works the first time unless this isn't actually your code pasted in.
It's probably because you're closing the second form, which is destroying it therefore you can't show it again. Each time you click the button in form1 create a new form2:
getSetForm2 = new Form2();
getSetForm1 = this;
getSetForm2.formOnePublicObj = getSetForm1;
Related
So I am new to Programming and I am working In Visual Studios c# Windows application Forms,
and I have a button on the first form That needs to set a Glabel Bool To true when pressed while also opening the second form.
On the second forms startup I need it to tell if that bool is set to true or false, to enable a button(on the second Form) or keep it disabled(if False.
Here Is the First Forms Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project
{
public partial class FirstPage: Form
{
public FirstPage()
{
InitializeComponent();
}
public bool IsManager = false;
public static class Global
{
public static bool IsManager = false;
}
private void Button_Btn_Click(object sender, EventArgs e)
{
this.hide();
Form2 f2 = new form2(IsManager);
f2.ShowDialog();
IsManager = true;
}
}
}
Here is my second Forms Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Project
{
public partial class SecondPage : Form
{
public FirstPage()
{
InitializeComponent();
IsManager = isManager;
}
private bool isManager;
private void setButtonVisibility()
{
if(isManager == true)
{
MessageBox.Show("Button Message Display")
}
{
//Need to check if the Global variable is true or false on page load to set a button to either enabled or disabled
}
}
Hopefully this makes sense If not let me know :) Thank You!
You can acomplish this by passing the variable to the constructor of the second form.
Let's assume you want to send the IsManager value to the second form. Call the second form while passing the value to the constructor:
Form2 f2 = new Form2(IsManager);
In the second form, read the value:
public partial class Form2 : Form
{
private bool IsManager;
public Form2(bool isManager)
{
InitializeComponent();
IsManager = isManager;
}
//Render button or whatever based on IsManager value;
}
EDIT: To check if the button needs to be enabled or not:
public partial class Form2 : Form
{
private bool IsManager;
public Form2(bool isManager)
{
InitializeComponent();
IsManager = isManager;
setButtonVisibility(); //call method setVisibility() -> this is what was missing in your code
}
private void setButtonVisibility()
{
if(IsManager == true)
{
MessageBox.Show("Button Message Display");
//or YourButton.Enable = true; in order to enable it.
}
}
}
In your second form, there are a couple of issues:
Class name and constructor name are not the same;
The parameter for the Form2 constructor is missing;
isManager should be assigned the value of IsManager which should have been passed as a parameter to the constructor.
Well after reading your question I am assuming that you're trying to set a value to the global bool IsManager and want to do some action in the other second form based on this value.
You can achieve this in multiple ways one passes your global bool in the constructor of second form something like this
public partial class SecondPage: Form
{
bool _isManager;
public SecondPage(bool IsManager)
{
InitializeComponent();
_isManager = IsManager; // the problem was isManager as mistakenkly i used small "i"
}
}
and from the first form pass value like
private void Button_Btn_Click(object sender, EventArgs e)
{
this.hide();
IsManager = true;
Form2 f2 = new form2(IsManager);
f2.ShowDialog();
}
My problem is simple. I want to click a panel in Form1 that will cause label1 in a userControl1, which will be placed upon form2 to change to "Text".
Clicking this panel would also change the background color of said userControl1. I receive the error :
'TileInterFaceTest.Usercontrol1.label1' due to its protection level
which frankly baffles me.
Even running the color change code separately it simply doesn't achieve the desired result.
To be clear, I'm quite a novice when it comes to C# and programming. I've been working with Visual Basic until now so the concept of classes, methods and objects are slightly confusing to me.
Here is my 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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
UserControl1 userControl1 = new UserControl1();
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Code for UserControl1:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class UserControl1 : UserControl
{
public String LabelText
{
get
{
return label1.Text;
}
set
{
label1.Text = value;
}
}
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
}
}
}
Generally this error occurs when you try to access label1 which is private to user control by default, if you want to change this behavior go to properties and there is a Modifier property which is private change it to public.
But in the code you have mentioned above there is no such thing, I think the error is thrown from other point of your application.
Second one is you have created the usercontrol and Form2 but you didn't add usercontrol to the Form you can add it by this code
form2.Controls.Add(userControl1);
the goal of the program is to be able to use the get and set methods of variables.
I have this code in a project C#:
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
{
private int c = 0;
public int a { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b = a;
f2.Show();
}
}
}
and in a 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 f1 = new Form1();
MessageBox.Show(Convert.ToString(b));
}
}
}
This code not work because the value of b should be 5, but during execution value 0;
any solution?
You can't expect values of one instance to magically propagate to every other instance.
So doing this:
Form1 f1 = new Form1();
int b = f1.a;
Is always going to be 0. You created a new instance, and nothing has happened to it! If you want to get the existing form's value (where the button was presumably clicked) you need to pass it to Form2 somehow.
You can:
Pass it on the constructor of Form2
Set up a service that holds the data instead
Probably about a million other approaches
private void button1_Click(object sender, EventArgs e)
{
a = 5;
Form2 f2 = new Form2();
f2.b=a;
f2.Show();
}
public partial class Form2 : Form
{
public int b;
public Form2()
{
InitializeComponent();
}
}
You want to pass the data before showing the second form instead of after the fact.
Right so I have a user control called "ModbusMaster" and a form with literally a single button on it..
When I click the button I want to change the text of a label on my control..
However nothing happens..
Here is the 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.Threading.Tasks;
using System.Windows.Forms;
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ModbusMaster mb = new ModbusMaster();
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}
I am calling the method openPort and passing the string "wooo" to it..
here is my control
The text does not get updated :(:(:(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ModbusMaster_2._0
{
public partial class ModbusMaster : UserControl
{
string portName = "COM1"; //default portname
int timeOut = 300; //default timeout for response
SerialPort sp = new SerialPort();
public ModbusMaster()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
portLabel.Text = portName;
}
public void openPort(string port)
{
statusLabel.Text = port;
}
/*
* Properties
*/
public string SerialPort //Set portname
{
get { return portName; }
set { portName = value;}
}
public int TimeOut //Set response timeout
{
get { return timeOut; }
set { timeOut = value; }
}
}
}
I think you must have two instances of ModbusMaster.
One of them is the one you can see on the display, and is NOT being updated.
The other one is the one you create in class Form1 with the line of code:
ModbusMaster mb = new ModbusMaster();
That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).
What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");
[EDIT]
Thinking about it - it's possible that you haven't instantiated another user control at all.
Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.
If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.
You are creating your UserControl but not assigning it to your Form's Control Collection. Try something like this in your Constructor.
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
ModbusMaster mb = new ModbusMaster();
public Form1()
{
InitializeComponent();
this.Controls.Add(mb); //Add your usercontrol to your forms control collection
}
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}
All I am trying to do is to change the state of checkbox on form2, and keep the state after pression OK.
I have form1 which is my main form and it only has one Strip menu. The code for form1 is as follow:
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 test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
dialog.ShowDialog();
}
}
}
The Form2 only has one checkbox and one OK button.
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 test
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void OK_Click(object sender, EventArgs e)
{
if (cbxForm2.Checked == true)
{
cbxForm2.Checked = true;
}
}
}
}
How can I change my code so when I go back on the menu the state of the combo box is as I left it?
You are creating a new Form2 every time:
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
// the 'new' keyword means you are creating an entirely new instance
Form2 dialog = new Form2();
dialog.ShowDialog();
}
This new instance has no idea what any previous instances looked like, so you need to store the state of the CheckBox and assign the value when you open Form2.
public partial class Form1 : Form
{
// backing field to store the state
bool checkBoxChecked;
public Form1()
{
InitializeComponent();
}
private void dialogToolStripMenuItem_Click(object sender, EventArgs e)
{
Form2 dialog = new Form2();
// assign the state
dialog.CheckBoxChecked = this.checkBoxChecked;
dialog.ShowDialog();
// save the state
this.checkBoxChecked = dialog.CheckBoxChecked;
}
}
You also need to add a property on Form2 so you can retrieve the state:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool CheckBoxChecked
{
get { return cbxForm2.Checked; }
set { cbxForm2.Checked = value; }
}
}