Can't use int array from Form1 in Form2 - c#

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

Related

I am writing a c# windows form code

I am writing a c# windows form code to
get the number from button1 and button2 and add them together in a text box but The compiler argues on the convert.toint32(textbox3.text) statement
and also it increases the value of the two variable and three variable how can I keep it constant but increase the value of textbox
and I need a solution?
int Three = 0;
int Two = 0;
//int one = 0;
int sum = 0;
// int sum = 0;
//int dec = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
//Three += 3;
//textBox3.Text = sum.ToString();
Three += 3;
sum = Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text = sum.ToString();
}
private void button2_Click(object sender, EventArgs e)
{
Two += 2;
sum = Two + Convert.ToInt32(textBox3.Text) + Three;
textBox3.Text =Convert.ToInt32(textBox3.Text) + Two.ToString();
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString();
}
`
Change
sum = Convert.ToInt32(textBox3.Text) + Three;
To
sum = Convert.ToInt32(textBox3.Text == "" ? "0" : textBox3.Text) + 3;
Also, remove
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = 0.ToString(); // this
}
because it doesn't make any sense.
Your variables belong to class and they are accessible for initialization in constructor. This can be done in many ways but you need to check if textboxes have values then try to convert it and add it.
private int Two;
private int Three;
private int sum;
public Form1()
{
this.Two = 0;
this.Three = 0;
this.sum = 0;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// MessageBox.Show("Enter the teams` name");
}
private void button1_Click(object sender, EventArgs e)
{
this.Three += 3;
sum = textBox3.Text != String.Empty ? Convert.ToInt32(textBox3.Text) : 0;
textBox3.Text = Convert.ToString(sum + this.Three);
}
... same for number Two
private void textBox3_TextChanged(object sender, EventArgs e)
{
textBox3.Text = "0";
}

accessing different elements in form2 from form1 and vice versa

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

Displaying combobox items into graph

I'm trying to show my combobox items (Numbers) from Form1 into a graph in Form2, but I can't get the value's to show in the graph. After multiple tries this is where I'm at right now, but I just can't figure out how to make it work.
Code in Form2:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
for (int i = 0; i < frm1.comboBox1.Items.Count; i++)
{
int Value = Convert.ToInt16(frm1.comboBox1.Items);
chart1.Series["Saved Results"].Points.AddXY(0, Value);
}
}
When I click on button1 nothing happens :(. Can you help me out? Thanks!
You are converting the item collection to an Int16, which is likely nothing. Use the indexer on the items collection.
private void button1_Click(object sender, EventArgs e)
{
Form1 frm1 = new Form1();
for (int i = 0; i < frm1.comboBox1.Items.Count; i++)
{
int Value = Convert.ToInt16(frm1.comboBox1.Items[i]);
chart1.Series["Saved Results"].Points.AddXY(0, Value);
}
}

Using Dynamic Textboxes in C# Program

I am trying to make number of TextBoxes according to number in TextBox1 and I want to use each TextBox value in my program. Their name becames txtbx0, txtbx1... but when I want to use in my program, it gives error "The name 'txtbx1' does not exist in the current context". How can I use them in my program?
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int y = Convert.ToInt32(textBox1.Text);
TextBox[] txtbx = new TextBox[y];
for (int i = 0; i < y; i++)
{
txtbx[i]= new TextBox();
txtbx[i].Location = new Point(20, i * 50);
txtbx[i].Size = new Size(100,50);
txtbx[i].Name = "txtbx"+i.ToString();
txtbx[i].Text = txtbx[i].Name;
flowLayoutPanel1.Controls.Add(txtbx[i]);
}
}
The way you have it right now, the textboxes could be accessed by using your array txtbx[whateverNumber]. In order to make them accessible outside of the method you posted, you'll need to make your txtbx array a class member instead of a method-scoped variable.
Something like:
class Form1 : Form
{
TextBox[] txtbx;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int y = Convert.ToInt32(textBox1.Text);
txtbx = new TextBox[y]; // Now this references the class member
for (int i = 0; i < y; i++)
... etc.
}
}
Accessing them individually by name is not really feasible because you'd have to have class member variables for each of them, but you don't know up front how many to make. The array method like you're doing is much better. You can just access them in other methods using txtbx[0] through txtbx[numBoxes - 1].

2D Array Buttons Display X and Y

I have a small program that hold 4 button in a 2D Array what I want to do is display its 'X' and 'Y' coordinates of the Array in a message box (when clicked)
I have tried a number of ways some don't work and some work but I cant get it to show the 'X' and 'Y' values
The image below shows what I have so far:
And This is the code i have come up with:
namespace _2DArray
{
public partial class Form1 : Form
{
private Button[,] b;
public Form1()
{
InitializeComponent();
b = new Button[2, 2];
b = new Button[,] { {button1,button2 },
{button3, button4}};
}
private void Form1_Load(object sender, EventArgs e)
{
foreach (Button bt in b)
{
bt.Click += new System.EventHandler(this.ClickedButton);
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s);
}
}
}
Here is the answer to your question if i read it right. You are trying to get the X and Y coordinates of the button right?
Here is the code for a button click:
private void button2_Click(object sender, EventArgs e)
{
MessageBox.Show(button1.Location.ToString());
}
try assigning some sort of pointer like give name of the button to keep track of it coordinates
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
b[i, j].Click += new System.EventHandler(this.ClickedButton);
b[i, j].Name =i+" "+j;
}
}
}
private void ClickedButton(object sender, EventArgs e)
{
Button s = (Button)sender;
MessageBox.Show("you have clicked button:" + s.Name);
}
Use this code
private void Form1_Load(object sender, EventArgs e) {
for (int x = 0; x < 2; x++) {
for (int y = 0; x < 2; y++) {
b[x, y].Tag = new Point(x, y);
b[x, y].Click += new System.EventHandler(this.ClickedButton);
}
}
}
private void ClickedButton(object sender, EventArgs e) {
Button s = (Button) sender;
MessageBox.Show("you have clicked button:" + s.Tag.ToString());
}
then clicking on button1 will show the message "you have clicked button:{X = 0, Y = 0}" etc
Tag is a property that each control has, it's description is "User-defined data associated with the object" so you can set it to whatever object you like.
I know this is probably a bit late for the op but hopefully it will help someone else.

Categories

Resources