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)
{
}
}
}
label1 is a private field, which means you cannot access it outside of the class UserControl1.
What you could do is add a public property in the definition of the class UserControl1:
public String LabelText {
get {
return label1.Text;
}
set {
label1.Text = value;
}
}
Then use this property to modify the value of the Text of label1:
private void panel1_Click(object sender, EventArgs e)
{
form2.Show();
userControl1.BackColor = System.Drawing.Color.Red;
userControl1.LabelText = "Text";
}
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.
Is there any way to retrieve the updated size of panel after FormWindowState.Maximized? I can get the size of the panel but it's giving the original size of panel.
Thanks;
By the way this is the code for usercontrol
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 WindowsFormsApplication8
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void UserControl1_Load(object sender, EventArgs e)
{
//this is the way i retrieve the panel size
Form1 f = new Form1();
this.Size = f.Size;
}
}
}
this is forms code, the panel has 4 activated anchors
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 WindowsFormsApplication8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
UserControl1 n = new UserControl1();
panel1.Controls.Add(n);
}
private void Form1_Load(object sender, EventArgs e)
{
WindowState = FormWindowState.Maximized;
}
}
}
If what you want to do is to resize the user control every time the parent Form is resized, you could use the Resize event of the ParentForm:
Main form:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
var uc = new UserControl2();
uc.Location = new Point(0, 0);
this.Controls.Add(uc);
WindowState = FormWindowState.Maximized;
}
}
}
User control:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace ans
{
public partial class UserControl2 : UserControl
{
public UserControl2()
{
InitializeComponent();
}
private void UserControl2_Load(object sender, EventArgs e)
{
this.BackColor = Color.Aqua;
if (this.ParentForm != null)
{
this.Size = this.ParentForm.Size;
this.ParentForm.Resize += ParentForm_Resize;
}
}
private void ParentForm_Resize(object sender, EventArgs e)
{
this.Size = ((Form)sender).Size;
}
}
}
If you want the UserControl you create dynamically at runtime to grow and shrink with the control that it is placed on, then set the Dock property of the UserControl to Fill at runtime:
private void button1_Click(object sender, EventArgs e)
{
MyUserControl userControl = new MyUserControl();
panel1.Controls.Add(userControl);
userControl.Dock = DockStyle.Fill;
}
For my A2 computing project I have decided to program a timetable software piece using C#. I have been trying to program a system in which clicking a panel in Form1 will change the text within label1 which is on UserControl1 which has been placed upon panel 2. At first this seemed like a trivial task but it would seem I was punished for my ignorance. As stated in the title when using the solution I thought would work I was told that label1 is 'inacessible due to its protection level', frankly this has baffled me. Anyway, here's the code. I'm quite new to C# and StackOverflow so please be tolerant of any stupid errors.
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 TileInterFaceTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void panel1_DoubleClick(object sender, EventArgs e)
{
}
private void panel1_Click(object sender, EventArgs e)
{
UserControl1.label1.Text = "Text";
}
}
}
label1 is a private member in UserControl1, so you can't get to access it outside that class.
possible solutions:
1 . in UserControl1 create public property
public string Title
{
get {return label1.Text; }
set {label1.Text = value; }
}
then in your form set that property
private void panel1_Click(object sender, EventArgs e)
{
UserControl1.Title = "Text";
}
2 . the following code should also work
UserControl1.Controls["label1"].Text = "Text";
There's no code problem, the debug doesn't have any problem but when I test, when I checked the checkbox the opacity doesn't change. Nothing happen. I'm using VisualStudio 2013 Express. Here's the code:
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 TP3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int carac = textBox1.Text.Length;
label2.Text = carac.ToString();
}
private void label2_Click(object sender, EventArgs e)
{
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
Form Form1;
Form1 = new Form1();
if(checkBox1.Checked == true)
{
Form1.Opacity = 1;
}
}
}
}
The code doesn't seem to do anything right. You are creating a new instance of Form in the checkBox1_CheckedChanged method (why do you create a new form?), you set the Opacity property on the new form, but you do not show the form any way. You need to call Show() / ShowDialog() on the Form1 to show it.
If you wish to change the opacity of the current form you can do it this way:
this.Opacity = 1;
And a call like without this would work as well:
Opacity = 1;
I have 2 forms with a main window and a second window. The main window (Form1) shall get text from the second window (Form2)
My second window (Form2) can write text from form 2 to form 1. In the class I can choose color for my text but my problem is that when I push the button who shall send the text it just comes the text without the color I choose so its just black text in Form1 when I send example yellow.
I'm not a C# expert since I'm pretty new at this. I'm sure its a pretty simple problem to fix but for me its not so easy.
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.Windows.Forms;
namespace tester
{
public partial class Form1 : Form
{
public string text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 NewForm2 = new Form2(this);
NewForm2.Show();
}
internal void populate()
{
richTextBox1.Text = text;
}
}
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.Windows.Forms;
namespace Tester
{
public partial class Form2 : Form
{
Form1 texting;
public Form2(Form1 iForm)
{
texting = iForm;
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
texting.text = richTextBox1.Text;
texting.populate();
this.Close();
}
private void button2_Click(object sender, EventArgs e)
{
if ( MyColorDialog.ShowDialog() != DialogResult.Cancel)
{
richTextBox1.ForeColor = MyColorDialog.Color;
}
}
}
Just as you are making public string text in your Form1, you could make a public Color rictTextBoxColor property. Then setting it as well, and referring to it in your populate method