Displaying combobox items into graph - c#

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

Related

How to display Form Components from List

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();
}
}

issue to fill the positions of a one-dimensional array on c#

I´m trying to fill positions on one dimesional array in C#, using three controls: one button, one textbox and one label.
public partial class Form1 : Form
{
string[] VECT;
int i = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
VECT = new string[4];
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = "";
VECT[i] = Convert.ToString(textBox2.Text);
i++;
label5.Text = Convert.ToString(i);
}
}
When the code is compiled, I start to insert alphanumeric values on the textbox, clicking the button to fill this array, but when loading is done, the array doesn't preserve data on it's positions.
Please, I need your help to do a wrigth loading on this array.
Thank you.

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].

Can't use int array from Form1 in Form2

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

How to loop usercontrol

I have problem displaying usercontrol,I have a usercontrol which has a panel called rowpanel which has textbox and combobox,Now ,when I Click button_1,I want the usercontrol to be displayed on each click,it is like adding a row on each click,I just don`t know how to loop it,I tried using indexing...
CODE
private void button1_Click(object sender, EventArgs e)
{
AddRow add = new AddRow();
show_pnl.Controls.Add(add);
}
AddRow is usercontrol ...this is a windows application,can I get some help please,
The reason is because they are overlapping each other. To fix it increment the top &/or left as shown here:
private const int gap = 20;
private int count = 0;
private void button1_Click(object sender, EventArgs e)
{
var add = new UserControl1();
add.Top = count * (add.Height + gap);
show_pnl.Controls.Add(add);
count++;
}

Categories

Resources