I have been finding some issues with form1 and form2, I want the result of pressing a button in form1 to be the change of the text in a button in form2, so mainly my problem is how to access this button in form2 from form1,I want to assign the result of int q into the text of button 2 shown in the picture
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
this.Hide();
// int a=1;
Random R = new Random();
int start = R.Next(10, 999);
if(start>99)
{
int x = start-1;
int y = x%100;
int z = start/y;
int w = z+1;
int q = start/w;
}
else
{
int y = start-1;
int z = y/2;
int w = start/z;
int q = 1;
}
}
enter image description here
Create a public property in Form2 class
public class Form2 : Form
{
public string Button1Text
{
set { this.Button1.Text = Value; }
}
....
}
Now in form1 click code set it
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.Show();
.... your calculations
f2.Button1Text = theResultOfYourCalculation.ToString()
Of course this could also be done making the property Modifiers of your buttons Public through the Forms designer. Giving access to the internal controls of your form (and all of their properties) is a bad idea and in the long term leads to a very bad designed application
Related
this question might be really stupid but here it is anyway. What I want my programm to do: When I press a button I want to add a DatePicker Component to a List and then display all the Components in the Main Form. However when I press the button it only adds the components but doesnt show them in the Form Window. No Errors are thrown. What do I have to do to display the DatePicker Components in the Main Form?
//class containing the List of Components
class Eintrag
{
static public List<DateTimePicker> Anfangszeit = new List<DateTimePicker>();
}
//Main Form Class
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++)
{
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50*i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
Eintrag.Anfangszeit[i].Show();
}
}
}
John Wu is right, you have to add the Controls to the Form via Controls.Add()
private void button1_Click(object sender, EventArgs e)
{
Eintrag.Anfangszeit.Add(new DateTimePicker());
for (int i = 0; i < Eintrag.Anfangszeit.Count; i++) {
Eintrag.Anfangszeit[i].Location = new System.Drawing.Point(30, 50 + 50 * i);
Eintrag.Anfangszeit[i].Size = new System.Drawing.Size(200, 20);
Eintrag.Anfangszeit[i].Visible = true;
this.Controls.Add(Eintrag.Anfangszeit[i]);
Eintrag.Anfangszeit[i].Show();
}
}
My code paints a circle on Form 2 when coordinates of circles are entered in textboxes in form 1 and button is clicked. The problem is, every time the coordinates are entered in Form 1, A new Form 2 is opening rather than old one getting updated.
Code on 1st Form
private void button1_Click(object sender, EventArgs e)
{
int r1, r2;
setValue = textBox1.Text;
setValue1 = textBox2.Text;
Int32.TryParse(setValue, out r1);
Int32.TryParse(setValue1, out r2);
Form2 f2 = new Form2();
//// f2.Show();
// f2.addcoordinate(r1,r2);
// f2.Update();
Graphics g2;
g2 = f2.CreateGraphics();
Class1 add = new Class1();
add.addcoordinate(r1,r2);
}
Code in Class1
public void addcoordinate(int r1, int r2)
{
// MessageBox.Show(r1.ToString());
Form2 f2 = new Form2();
f2.addcoordinate(r1, r2);
f2.Show();
}
Code on Form2
private List<Point> circleCoordinates = new List<Point>();
public Form1()
{
InitializeComponent();
}
public void addcoordinate(int r1, int r2)
{
this.circleCoordinates.Add(new Point(r1, r2));
}
protected override void OnPaint(PaintEventArgs e)
{
// linedrawing goes here
foreach (Point point in this.circleCoordinates)
{
e.Graphics.DrawEllipse(Pens.Black, new Rectangle(point, new
Size(10, 10)));
}
base.OnPaint(e);
}
Please suggest.
In Form1 you defined f2 as follows:
Form2 f2 = new Form2();
Every time this line of code is run, it creates a new instance of the object. That's why you see a new form each time you click on your button.
Define the Form2 object inside the Form1 class and out of all the private methods by moving the above mentioned line of code outside the methods in the class. Then use the specific instance of Form2 that you have declared (f2 in this case), in the code inside your methods. This way, you are working on the same instance of the class and you are not creating new instances of the object Form2 every time you click on button1.
So basically im trying to make polygons with a name entered from Form2, called Apgabala_nosaukums (it's in my language, sorry for that). I have been trying to debug this, first 2 times the name entered from Form2 did get read and i was able to see that the name was added to the Polygon. But now it is not getting in the fromVisibleChanged anymore, ending in that the polygon is not getting name. Meaning that I cannot get the bool to true, so I could add 4 points and make a square or rectangle area out of them. Any ideas? Basically the btnAdd_Click function is not working properly, rest is working fine. Any ideas?
Form1 (Main form):
namespace GMapTest
{
public partial class Form1 : Form
{
GMapOverlay polygons = new GMapOverlay("polygons");
List<PointLatLng> points = new List<PointLatLng>();
double lat;
double lng;
int clicks = 0;
bool add = false;
string nosaukums;
public Form1()
{
InitializeComponent();
}
private void gMapControl1_Load(object sender, EventArgs e)
{
gmap.MapProvider = GoogleMapProvider.Instance;
GMaps.Instance.Mode = AccessMode.ServerOnly;
gmap.SetPositionByKeywords("Riga, Latvia");
gmap.ShowCenter = false;
gmap.Overlays.Add(polygons);
}
private void gmap_MouseDown(object sender, MouseEventArgs e)
{
if (add == true)
{
if (e.Button == MouseButtons.Left)
{
lat = gmap.FromLocalToLatLng(e.X, e.Y).Lat;
lng = gmap.FromLocalToLatLng(e.X, e.Y).Lng;
clicks += 1;
points.Add(new PointLatLng(lat, lng));
}
if (clicks == 4)
{
GMapPolygon polygon = new GMapPolygon(points, nosaukums);
polygons.Polygons.Add(polygon);
clicks = 0;
points.Clear();
add = false;
}
}
}
private void btnAdd_Click(object sender, EventArgs e)
{
Apgabala_nosaukums addName = new Apgabala_nosaukums();
addName.ShowDialog();
addName.VisibleChanged += formVisibleChanged;
if (nosaukums != null)
{
this.add = true;
}
}
private void formVisibleChanged(object sender, EventArgs e)
{
Apgabala_nosaukums frm = (Apgabala_nosaukums)sender;
if (!frm.Visible)
{
this.nosaukums = (frm.ReturnText);
frm.Dispose();
}
}
}
}
Form2 (Apgabala_nosaukums):
namespace GMapTest
{
public partial class Apgabala_nosaukums : Form
{
public string ReturnText { get; set; }
public Apgabala_nosaukums()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
this.ReturnText = this.txtName.Text;
this.Visible = false;
}
}
}
The problem is in your btnAdd_Click function. When you call ShowDialog your other form is shown and the next line, addName.VisibleChanged += formVisibleChanged; isn't called until you close the new form. ShowDialog shows the form modally, you can't interact with the parent until you close the new form.
There are a couple ways you could fix this.
1) Subscribe to the VisibleChanged event before you show the form,
addName.VisibleChanged += formVisibleChanged;
addName.ShowDialog();
2) Call addName.Show() instead of addName.ShowDialog(). This shows the form in a non-modal way. The event will get subscribed to because execution continues in btnAdd_Click before the new form is closed. But, the parent form will be interactable, not sure if this is desired or not.
3) You could also get rid of the VisibleChanged event stuff and instead do ShowDialog and read the property after. This is what I'd recommend from seeing the code.
private void btnAdd_Click(object sender, EventArgs e)
{
Apgabala_nosaukums addName = new Apgabala_nosaukums();
addName.ShowDialog();
this.nosaukums = addName.ReturnText;
addName.Dispose();
}
I am trying to pull a label text into another form inside the same solution to use in an if statement. However, it seems as though it's not pulling the data from the field. I am trying to change the color of the label background based on the label text in form 1. Any help is greatly appreciated.
IN FORM 1:
public void button1_Click(object sender, EventArgs e)
{
form1 view = new form();
view.Show();
view.label1 = label1.Text.ToString();
}
IN FORM 2:
public string label1 { get; set; }
public void Display()
{
if (label1 == "1")
{
for (int i = 0; i < nWinnings.Length; i++)
{
Label label = new Label();
label.BackColor = Color.Red;
...
}
}
else
{
for (int i = 0; i < nWinnings.Length; i++)
{
Label label = new Label();
label.BackColor = Color.Blue;
...
}
}
}
There is more to the label but the label is working fine minus the color change.
This is incorrect:
Label label = new Label();
You cannot create a new instance of your Label... it has absolutely no connection to the original Label instance in the first Form, and changing any property on it will not affect the original one either.
You'll need to pass a reference to the entire Label:
// Form 1
public void button1_Click(object sender, EventArgs e)
{
form1 view = new form();
view.label1 = label1;
view.Show();
}
// Form 2
public Label label1 { get; set; }
public void Display()
{
if (label1.Text == "1")
{
for (int i = 0; i < nWinnings.Length; i++)
{
label1.BackColor = Color.Red;
// ... etc, etc
I'd limit how much passing around of references to controls you do to other Forms. In my experience, code starts to get quite muddy when you do that too much.
If what I understood is correct, you have 2 forms. FORM1 & FORM2.
You have a label control LABEL1 in FORM1. You read this text and pass it to FORM2.
In FORM2, you have another label control LABEL2, whose background color you want to change.
You can declare a string variable in FORM2.
Add a new constructor to FORM2 to accept a string argument, and set this value to the string variable.
In FORM2 OnLoad, you can then check the value of your string variable and
then,
LABEL2.BackColor = whateverColor in the if-else loop.
something like this in FORM1
FORM2 newForm = new FORM2(LABEL1.Text);
newForm.Show();
and in FORM2
private label1String = String.Empty();
public FORM2(string arg)
{
...Default Initialization Code...
label1String = arg;
}
private void ChangeLabel2Color()
{
if(label1String == "1")
{
LABEL2.BackColor = whateverColorYouNeed;
}
else
{
...WHATEVER YOU NEED TO DO...
}
}
I wrote the code directly, so there might be syntax errors.
I have an int array in Form1 that I need to use in Form2. But when I'm trying to use the array values in Form2, it gives me zeros.
private void button1_Click(object sender, EventArgs e)
{
frm1 = new Form1();
for (int i = 0; i < 26; i++)
{
label1.Text += frm1.theCode[i];
}
}
But when I try the same thing in Form1, it works great!
private void button5_Click(object sender, EventArgs e)
{
frm2 = new Form2();
for (int i = 0; i < 26; i++) frm2.label1.Text += theCode[i]+ " ";
frm2.Show();
}
But I still need to use the array in Form2, not Form1
in Form1 you must declare int array as static field and public in order to access it from another form.
So this is how you declare theCode in Form1
public static int[] theCode; // should be public and static
And this is how you use the array in Form2
private void button1_Click(object sender, EventArgs e)
{
// no need to create new instance of Form1.
for (int i = 0; i < 26; i++)
{
label1.Text += Form1.theCode[i]; // use the static field
}
}