Opacity CheckBox(VisualStudio) don't work - c#

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;

Related

Calling a non static void method from another class in c#

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.

DotNetBrowser is a tiny window in my form, why and how do I fix this?

This is my current code: https://hastebin.com/ifejusezat.cs
This is a screenshot of what I see: https://gyazo.com/2ee9b1210649ca8ec61e3fa7e645a286
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
BrowserView browserView = new WinFormsBrowserView();
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("http://www.google.com");
}
private void Form1_Load(object sender, EventArgs e)
{
BrowserView browserView = new WinFormsBrowserView(BrowserFactory.Create());
Control browserWindow = (Control)browserView;
browserWindow.Dock = DockStyle.Fill;
Controls.Add(browserWindow);
}
}
Updated answer: This is my complete Form1.cs (taken directly from the teamdev website), and it works (see image) without needing to resize.
using DotNetBrowser;
using DotNetBrowser.WinForms;
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 WinFormsSampleCSLightweight
{
public partial class Form1 : Form
{
private readonly BrowserView browserView;
public Form1()
{
InitializeComponent();
browserView = new WinFormsBrowserView(BrowserFactory.Create(BrowserType.LIGHTWEIGHT));
Controls.Add((Control)browserView);
browserView.Browser.LoadURL("http://www.google.com");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (browserView != null)
{
browserView.Dispose();
browserView.Browser.Dispose();
}
}
}
}
Original answer:
You can try the UpdateSize method on your browser view.
browserView.UpdateSize(someWidth, someHeight);
Or you can try to size the document that is viewed.
public void WebBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
//For the case when the control's Dock property is DockStyle.Fill
this.Width = WebBrowser.Document.Body.ScrollRectangle.Width + 40; //40 is for border
this.Height = WebBrowser.Document.Body.ScrollRectangle.Height + 40; //40 is for border
//For the case when the control is not docked
WebBrowser.Size = WebBrowser.Document.Body.ScrollRectangle.Size;
}
If your are using DotNetBrowser 1.9 use the control properties to set Dock to DockFill
Control browserControl = (Control)browserView;
browserControl.Dock = DockStyle.Fill;
Controls.Add(browserControl);

Get the new size of panel and transfer to usercontrol after winform becomes fullscreen?

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;
}

C# Change usercontrol labeltext and background color

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";
}

Public function not working properly

I have the following two pieces of code, please take a look at it, I pointed where it is going wrong.
I removed the functions where I call the second window, they do not make sense here.
First, main form, this form calls the second 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.Runtime.InteropServices;
namespace STP_Design
{
public partial class STP2Main : Form
{
public STP2Main()
{
InitializeComponent();
tabControl1.SelectedTab = tabPageDeviceManager;
}
private void pictureBox1_Click(object sender, EventArgs e)
{
MenuForm MDIMF = new MenuForm();
MDIMF.MdiParent = this;
MDIMF.StartPosition = FormStartPosition.Manual;
MDIMF.Location = new Point(3, 50);
MDIMF.Show();
tabControl1.Visible = false;
}
public void set()
{
tabControl1.Visible = true; // This statement executes, but does not result in anything I'd expect. The debugger tells me the visibility is false.
tabControl1.BringToFront();
}
}
}
and second form, which I close and should update the first 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 STP_Design
{
public partial class MenuForm : Form
{
public MenuForm()
{
InitializeComponent();
this.BringToFront();
}
private void button1_Click(object sender, EventArgs e)
{
STP2Main stp = new STP2Main();
stp.set();
this.Close();
}
}
}
You're calling the set method on a new version of the main form, rather than on the version you presumably already have displayed to the user.
What you need to do is get the current main form from the MdiParent property of the menu form, and call the method on that instead.
// In menu form
private void button1_Click(object sender, EventArgs e)
{
var mainForm = this.MdiParent as STP2Main;
if (mainForm != null)
mainForm.set();
this.Close();
}

Categories