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";
Related
Iam very new to coding so please pardon my lack of knowledge.
I made a program "Music player" that uses webBrowser to play video from youtube but from what i understood from other posts it cant work due to webBrowser being IE7. I dont neccessarily have to use webBrowser but it seemed like the best way.
Is there a way to "update" IE or do i need to use different method?
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;
using System.Text.RegularExpressions;
namespace MusicPlayer_V4._1
{
public partial class Form1 : Form
{
string _ytUrl;
public Form1()
{
InitializeComponent();
}
public string VideoId
{
get
{
var ytMatch = new Regex(#"youtu(?:\.be|be\.com) / (?:.*v(?:.*/|=)|(?:.*/)?)([a-zA-Z0-_]+)").Match(_ytUrl);
return ytMatch.Success ? ytMatch.Groups[1].Value : string.Empty;
}
}
//user input (URL)
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
//play button
private void button1_Click(object sender, EventArgs e)
{
_ytUrl = txtUrl.Text;
webBrowser.Navigate($"http://youtube.com/v/{VideoId}?version=3");
}
//help button
private void button2_Click(object sender, EventArgs e)
{
}
//browser
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
}
}
The way moving forward is to use WebView2 which is basically the same thing as a WebView but based on the chromium-based edge. It will be always up to date on windows 10/11 machines and it works down to Windows 7.
https://developer.microsoft.com/en-us/microsoft-edge/webview2/
I just "moved" from C++ and a Rad Studio to C# and a Visual Studio as i can see more tutorials and help is available on the internet for VC. But.. I have a problem.
I know how to play a music when a form is created (when program starts). But how can i stop music playing using a normal TButton?
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 _01_21_2019
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
// play an intro sound when a form is shown
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
wplayer.URL = "intro.mp3";
wplayer.controls.play();
}
private void button1_Click(object sender, EventArgs e)
{
wplayer.controls.stop(); // Here it is not working - "current context"
}
}
}
Compiler says
Error CS0103 The name 'wplayer' does not exist in the current context"
I tried to move wplayer.controls.stop() just below the play(); and it works. But how to stop music using a button?
Here is the code on pastebin:
https://pastebin.com/v9wDn5mJ
You should instantiate the object outside of the function so it's available for the class instance.
You might also want to look into the mvvm pattern. It's very helpfull when writing WPF and some other applications.
public partial class Form1 : Form
{
WMPLib.WindowsMediaPlayer wplayer = new WMPLib.WindowsMediaPlayer();
public Form1()
{
InitializeComponent();
}
private void Form1_Shown(object sender, EventArgs e)
{
// play an intro sound when a form is shown
wplayer.URL = "intro.mp3";
wplayer.controls.play();
}
private void button1_Click(object sender, EventArgs e)
{
wplayer.controls.stop();
}
}
I have this error and i don't know how to fix it, i can't find whats wrong with my code so i ask help to the ppl that are reading this and know the solution.
The error is at the button voids(exept the start button void). tried to switch between the URLtexbox and the Combobox but somewhere i screwed up, i've tried a lot to fix it but i couldn't find out how to fix it.
Here is my 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;
using System.Web;
namespace ****
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
}
private void startbutton_Click(object sender, EventArgs e)
{
switch (Text)
{
case "full":
{
webBrowser1.Navigate(URLtextbox.Text);
}
break;
case "empety":
{
webBrowser1.Navigate(comboBox1.Text);
}
break;
}
}
}
private void stopbutton_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void forwardbutton_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void backbutton_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
}
Thanks for helping ^.^
The 3 methods (stopbutton_Click, forwardbutton_Click and backbutton_Click) must be defined inside a class.
Or in other words: Move the closing brace of the class below the last method.
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";
}
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;