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");
}
}
}
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();
}
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;
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);
Scenario -
Program opens a winForm. User enters info, clicks Start button. Action transfers to code in App_Code.Model. When that code finishes, code behind the winForm needs to display updated information. App_Code.Model shouldn't know about the winForm. The winForm in this case has a button btnStart and a textbox tbInput.
But when the event is raised, it is null, so I am doing something wrong. Note, this is not about events raised from winForms userControls, I am aware there is a lot of information online about that.
App_Code.Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EventsTest.App_Code.Model
{
public delegate void TableViewChangeHandler(object sender, HandChangedEventArgs e);
public class HandChangedEventArgs : EventArgs{
public int HandNum { get; set; }
public int PlayerNum { get; set; }
public HandChangedEventArgs(int handNum, int playerNum){
HandNum = handNum;
PlayerNum = playerNum;
}
}
public class Game{
public event TableViewChangeHandler TableViewChanged;
public void PrepareGame(){
int value = -1;
if (TableViewChanged != null)
TableViewChanged(this, new HandChangedEventArgs(value, 0));
else
value = 2;//used to set toggle to catch debugger
}
}
}
code behind 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;
using EventsTest.App_Code.Model;
namespace EventsTest
{
public partial class testForm : Form{
public testForm(){
InitializeComponent();
Game myGame = new Game();
myGame.TableViewChanged += this.HandleTableViewChange;
}
private void btnStart_Click(object sender, EventArgs e) {
Game myGame = new Game();
myGame.PrepareGame();
}
public void HandleTableViewChange(object sender, HandChangedEventArgs e){
this.tbInput.Text = "Raised";
}
}
}
May be I understand. You have two instances of Game class:
Is in ctor of the form and subscribes to the event.
Is in btnStart_Click method which doesn't subscribe to event and call PrepareGame(), so you don't recieve event notification.
MOve your event subaceiption code to button click handler and you done.
Basically what I'm trying to do is I have a string on the main form that pulls its value from a textbox.
I then generate a modal version of a second form and want to have that string (or the main forms textbox1.text value) usable in the second form for processes.
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;
using System.Diagnostics;
using System.IO;
namespace Tool{
public partial class MainForm : Form
{
public string hostname;
public MainForm()
{
InitializeComponent();
textBox1.Text = hostname;
}
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.ShowDialog();
}
}
}
'
Child 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;
using System.Diagnostics;
using System.IO;
namespace Tool
{
public partial class SiteForm : Form
{
public string hostname {get; set; }
public SiteForm()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
label1.Text = this.hostname;
}
}
}
Any suggestions on how I can do this? I know there has to be a simpler way, sorry I'm still a bit of a noob and am trying to teach myself C# as I go.
The result is when I click the label on the child form it is blank, because of this I am able to deduce that the string isn't passing between the two forms correctly.
The simplest way is to pass it in the constructor of the Child form, for example:
private string _hostname = "";
...
public SiteForm(string hostname)
{
_hostname = hostname;
InitializeComponent();
}
Try hooking into your child form's Load event and set the value of its hostname property in an event handler on your main form.
public void btn_test_Click(object sender, EventArgs e)
{
string hostname = textBox1.Text;
SiteForm frmsite = new SiteForm();
frmsite.Load += new EventHandler(frmsite_Load);
frmsite.ShowDialog();
}
public void frmsite_Load(object sender, EventArgs e)
{
SiteForm frmsite = sender as SiteForm;
frmsite.hostname = this.hostname;
}