I am a beginner programmer and I want to develop a small game project for practice, and for so I am making some tests first.
In this test, my program generates a label programmatically, and I want the button to delete it when it is pressed. The problem is, it says that there is no definition for myLabel, so I suppose I should make myLabel a "global" control, but I don't know how. Any ideas? Thanks for the help!
Here's my code so far:
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Label myLabel = new Label();
this.Controls.Add(myLabel);
myLabel.Location = new Point(50, 50);
myLabel.Text = "Yay!";
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Remove(Label.myLabel);
}
}
}
make it a class private field
public partial class Form1 : Form
{
private Label myLabel;
public Form1()
{
InitializeComponent();
myLabel = new Label();
this.Controls.Add(myLabel);
myLabel.Location = new Point(50, 50);
myLabel.Text = "Yay!";
}
private void button1_Click(object sender, EventArgs e)
{
this.Controls.Remove(myLabel);
}
Related
i have a form with a panel in it and when a button is presed a usercontrol showes and the panel hides, now i need to hide a button if the textbox in the form1 contains "admin" in it.
this is the form1 code
public partial class Form1 : Form
{
public string a;
public Form1()
{
InitializeComponent();
a = textBox1.Text;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
}
}
and this is the usercontrol code
public partial class afterlogin : UserControl
{
public afterlogin()
{
InitializeComponent();
Form1 f = new Form1();
if (f.a.Contains("admin"))
{
button1.Hide();
}
}
}
You're creating a new form in the user control, it will not have the same values as the original form you created your user control in.
If you wish to take in values from the form, add a constructor parameter to the "afterlogin" class with text of the textbox, such as:
public afterlogin(string text)
{
InitializeComponent();
if (text.Contains("admin"))
{
button1.Hide();
}
}
and pass the text value to the constructor of the "afterLogin" class:
afterlogin v = new afterlogin(a);
Since Form1 creates the UserControl, just have the Form itself turn on or off the Button?
You can make a method in your UserControl that allows you to change the visibility of the control:
public partial class afterlogin : UserControl
{
public void setButton(bool state)
{
button1.Visible = state;
}
}
Now you can call setButton when you create the UserControl:
private void button1_Click(object sender, EventArgs e)
{
panel1.Controls.Clear();
afterlogin v = new afterlogin();
v.Dock = DockStyle.Fill;
panel1.Controls.Add(v);
v.BringToFront();
v.setButton(!textBox1.Text.Contains("admin"));
}
I have 2 forms, form1 and form2. In form1 I call form2, where I input 2 numbers, one for height and for width of a picturebox. Then I want to pass that data from form2 to form1, where I create picturebox with said size.
Then I want to store height and width to class and then access that info from form1.
Here is my code:
Form1
namespace NPA_projekt
{
public partial class Form1 : Form
{
private Form2 f2 = new Form2();
image img = new image();
public Form1()
{
InitializeComponent();
}
private void newToolStripMenuItem_Click(object sender, EventArgs e)
{
f2.ShowDialog();
}
private void btnTest_Click(object sender, EventArgs e)
{
pbMainArea.Width = img.width;
pbMainArea.Height = img.length;
}
}
}
Form2
namespace NPA_projekt
{
public partial class Form2 : Form
{
image img = new image();
public Form2()
{
InitializeComponent();
}
//reset btn
private void button1_Click(object sender, EventArgs e)
{
nudWidth.Value = 640;
nudLength.Value = 400;
}
//cancel btn
private void button3_Click(object sender, EventArgs e)
{
this.Close();
}
//ok btn
private void btnOK_Click(object sender, EventArgs e)
{
img.width = Convert.ToInt32(nudWidth.Value);
img.length = Convert.ToInt32(nudLength.Value);
this.Close();
}
}
}
Class image
namespace NPA_projekt
{
class image
{
public int width = 0;
public int length = 0;
}
}
Values, that are stored in form2, are set to their original values, when I want to use them in form1. Could someone please elaborate what's happening.
Thank you all!
img is declared twice. Once in Form1 and again in Form2. When you are setting the width and height of img in Form2 you are setting it for the image instance you declared in Form2 not Form1. You need to make the img in Form1 visible to Form2 and perform the operation on that.
So, make img in Form1 public:
public image img {get; set;}
public Form1()
{
InitializeComponent();
img = new image();
}
Then you need to access it in Form2 (one way should be the Parent property of the form):
private void btnOK_Click(object sender, EventArgs e)
{
var form1 = (Form1)this.Parent
form1.img.width = Convert.ToInt32(nudWidth.Value);
form1.img.length = Convert.ToInt32(nudLength.Value);
this.Close();
}
I haven't tested this all out, but the approach is valid. The key to eliminating the confusion is getting rid of the img declaration in Form2 and realizing that you need to get access to Form1 from Form2
I'm trying to make this simple program using Visual Studio C# 2013.
program screenshot: http://i.imgur.com/4QVbaa2.png
The listbox named receiptbox modifier was set to public using the properties panel.
Basically I am using 2 forms, what I want to happen is to show the quantity + the name of the food in the form 1's listbox.
This is the code when you click the food icon on form1:
private void pictureBox1_Click(object sender, EventArgs e)
{
FoodQty form2 = new FoodQty();
form2.Show();
}
It will show form2.
This is the source-code in the form2 and when you click its Ok button:
public partial class FoodQty : Form
{
Form1 mainfrm = new Form1();
Record recordInstance = new Record();
public FoodQty()
{
InitializeComponent();
}
private void btnOk_Click(object sender, EventArgs e)
{
mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array
}
}
Try this :
in form 1 :
private void pictureBox1_Click(object sender, EventArgs e)
{
FoodQty form2 = new FoodQty(this);
form2.Show();
}
in fom2 :
public partial class FoodQty : Form
{
Form1 mainfrm = new Form1();
Record recordInstance = new Record();
public FoodQty( Form1 fr)
{
InitializeComponent();
mainfrm =fr;
}
private void btnOk_Click(object sender, EventArgs e)
{
mainfrm.receiptBox.Items.Add((int)numericUpDown1.Value + recordInstance.foodMenuArray[1]); // converts numupdown to int and appends the string array
}
}
I am new to c#. I have the following in my project in windows forms:
Form1 with button and textbox.
User control with a buttton.
Form2 with button and textBox.
As shown in the screenshot: In form1, I click "Show User Control1" User Control1 pops up. Then in User Control1 I click Show Form2 form2 pops up.
In Form2 I enter values in textBox and when click "Send to textbox in form1" I want this text to be inserted into the textbox in Form1.
My question is: How can I send text from form2 to textbox in form1 via user control1?
I just need to know some steps to follow or some code if it is possible to achieve this.
Please help me. Thank you
Form1:
public partial class Form1 : Form
{
UserControl1 UC1 = new UserControl1();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Controls.Add(UC1); //add a userControl
UC1.Visible = true;
}
}
User Control1:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
Form2:
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// I want to send text to form1 when this button is clicked
}
}
You can do it by trigger event and event handler.
In Form2,
public delegate void SendTextF2(string YourStringFromTextBox);
public partial class Form2 : Form
{
public event SendTextF2 UISendTextHandlerF2;
public Form2(TextBox s)
{/*unchange*/}
private void button1_Click(object sender, EventArgs e)
{
if(UISendTextHandlerF2!=null)
UISendTextHandlerF2(textBox1.Text);
}
}
In UserControl1,
//New
public delegate void SendTextUC(string YourStringInTextBox);
public partial class UserControl1 : UserControl
{
//New
public event SendTextUC UISendTextHandlerUC;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
//Add event handler
frm2.UISendTextHandlerF2 += SendText123;
}
//Event Handler for the event trigger in Form2
void SendText123(string YourStringFromTextBox)
{
//Trigger Event
if(UISendTextHandlerUC!=null)
UISendTextHandlerUC(YourStringFromTextBox);
}
}
In Form1,
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
//Add event handler
UC1.UISendTextHandlerUC += FinallyWeGetTheString;
}
Controls.Add(UC1);
UC1.Visible = true;
}
//New
void FinallyWeGetTheString(string YourStringFromTextBox)
{
textBox1.Text = YouStringFromTextBox;
}
}
Add those line to your code:
public partial class Form1 : Form
{
UserControl1 UC1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (UC1 == null)
{
UC1 = new UserControl1(textBox1);
}
Controls.Add(UC1);
UC1.Visible = true;
}
}
User Control:
public partial class UserControl1 : UserControl
{
TextBox r;
public UserControl1(TextBox r)
{
InitializeComponent();
this.r = r;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(r);
frm2.Show();
}
}
And Form2:
public partial class Form2 : Form
{
TextBox s;
public Form2(TextBox s)
{
InitializeComponent();
this.s = s;
}
private void button1_Click(object sender, EventArgs e)
{
String str = textBox1.Text;
s.Text = str;
}
}
I want to display the current page-name that's on my WebBrowser control on my Label, but when the Document.Title updates, my label disappears instead of showing the current website title. How can i fix this?
public partial class StreamViewer : Form
{
public StreamViewer()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowserViewer.DocumentTitle;
}
Use the DocumentTitleChanged event
public Form1()
{
InitializeComponent();
webBrowserViewer.DocumentTitleChanged += new EventHandler(webBrowserViewer_DocumentTitleChanged);
}
private void webBrowserViewer_DocumentTitleChanged(object sender, EventArgs e)
{
label2.Text = webBrowser1.DocumentTitle;
}
https://msdn.microsoft.com/en-us/library/system.windows.forms.webbrowser.documenttitlechanged(v=vs.110).aspx