How do I make a variable accessible from every method of class? - c#

I have an int named "mode". I want to make every function be able to access it.
Here is my code.
namespace WindowsFormsApplication1
{
public partial class Form5 : Form
{
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
int mode = 4;
}
}
}

Just make it a property of the 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 WindowsFormsApplication1
{
public partial class Form5 : Form
{
public int mode {get; set;}
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
mode = 4;
}
}
}

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 Form5 : Form
{
public int mode;
public Form5()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int wow = mode - 1;
}
private void Form5_Load(object sender, EventArgs e)
{
mode = 4;
}
}
}
However, I'd be surprised if there wasn't a SO page about this. Also I'd recommend looking at MSDN and other programming c#.net resources.

Related

Autoplay video on windows form

i am new to .net i am having trouble playing videos automatically. I would be showing different textboxes here but i want the video to autplay without any buttons
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 WMPLib;
namespace ThinkQDisplay
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
AxWindowsMediaPlayer1.URL = "C:\Users\Ramrod\Documents\Visual Studio 2012\Projects\ThinkQDisplay\ThinkQDisplay\sample.avi";
}
}
}
It keeps telling it is an unrecognized escape sequence. Also I would like to have a separate form (form2). Where I can choose what to play here on form 1. Is it also possible to have it looped?
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = #"C:\Users\Ramrod\Documents\Visual Studio 2012\Projects\ThinkQDisplay\ThinkQDisplay\sampler.avi";
}
private void Form1_Load(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.autoStart = true;
}
}
}

c# Metro framework - Previous and Next button to access another panels

I am developing this project "Profiling System" using MetroFramework and I've got this problem. If I click the next button I want my system to show the other panels where it has MetroTextboxes, MetroComboboxes, MetroCheckboxes and other tools of MetroFramework, But the panels does not appear.
I also want to try the UserControl just to slide it on the form but I don't know how. Please help.
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MetroFramework.Forms;
namespace TEST.cs
{
public partial class Form1 : MetroForm
{
List<Panel> listPanel = new List<Panel>();
int index;
public Form1()
{
InitializeComponent();
}
private void btnBack_Click(object sender, EventArgs e)
{
if (index > 0)
listPanel[--index].BringToFront();
}
private void btnNext_Click(object sender, EventArgs e)
{
if (index < listPanel.Count - 1)
listPanel[++index].BringToFront();
}
private void Form1_Load(object sender, EventArgs e)
{
listPanel.Add(metroPanel1);
listPanel.Add(metroPanel2);
listPanel.Add(metroPanel3);
listPanel[index].BringToFront();
}
}
}

Set textbox of a usercontrol from another usercontrol

I have usercontrol ctrlinserimento with a textbox and I have another usercontrol ctrlricerca with a botton that should set textbox.text in ctrlinserimento to a string(let's say for example 'Hello').
How can I do?
ctrlinserimento
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebApplication3
{
public partial class ctrlinserimento : System.Web.UI.UserControl
{
public string textbox1
{
set { TextBox1.Text = value; }
get { return TextBox1.Text; }
}
}
}
ctrlricerca
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxGridView;
namespace WebApplication3
{
public partial class ctrlricerca: System.Web.UI.UserControl
{
public ctrlinserimento i
{
set; get;
}
protected void Button1_Click(object sender, EventArgs e)
{
i.textbox1 = "Hello";
}
}
}
}
I have NullReferenceException on
i.textbox1 = "Hello";
There are lot of way to do it i.e. page level, User control level. Check following solution that fit for you.
In ctrlricerca user control add property for ctrlinserimento and page level assign instance of ctrlinserimento to property of ctrlricerca
public partial class ctrlricerca: System.Web.UI.UserControl
{
private string nome;
public ctrlinserimento { set; }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
ctrlinserimento.textbox.text="Hello";
}
}
}
}

Calling other form method

I have 2 form, Form 1 and 2. Form 1 will be used to initialize Form 2. After Form 2 being initialize, Form 2 will be calling Form 1 method but it just won't work. I had been searching for hours but still no idea how it works. Please help me out.
Form 1
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 parentChildTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 x = new Form2();
x.Show();
}
public static void callMe(){
MessageBox.Show("HAHAHA");
}
}
}
Form 2
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 parentChildTest
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
/*How to call callMe()?????*/
}
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1.Callme();
}
}
private void button1_Click(object sender, EventArgs e)
{
Form1.callMe();
}

How Do I Use DisplayLabel.Text In Visual C#?

I'm trying to make certain text appear in the label when a button is clicked, but I always get the error `The name DisplayLabel does not exist in the current context. This 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;
namespace Chapter_2_HW
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void sinisterButton_Click(object sender, EventArgs e)
{
MessageBox.Show("Left");
}
private void mediumButton_Click(object sender, EventArgs e)
{
}
private void dexterButton_Click(object sender, EventArgs e)
{
}
private void instructionLabel_Click(object sender, EventArgs e)
{
}
private void translateLabel_Click(object sender, EventArgs e)
{
DisplayLabel.Text = "Goodbye";
}
}
}

Categories

Resources