How to loop usercontrol - c#

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

Related

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.

C# What am I doing wrong? Attempting counter up with do while loop but only iterating once

Help, I'm a novice programming student who got assigned a side task at work; This is the pseudo code I wrote for it:
Write a program with 3 buttons:
Exit/Close, Clear/Reset, Count/Calculate
When the user clicks the count button, the number displayed in the label/textbox should increase by 1 and this should continue infinitely.
When the user clicks the clear/Reset button, the number displayed in the label/textbox should be reset to 0.`{
Application.Exit();
}
private void btnCount_Click(object sender, EventArgs e)
{
int Count = 1;
int Numberdisplayed;
{
do Count++;
while (Count >= 1);
Numberdisplayed = Count + 1;
lblNumberdisplayed.Text = Numberdisplayed.ToString();
}
}
private void btnReset_Click(object sender, EventArgs e)
{
int Count = 0;
lblNumberdisplayed.Text = String.Empty;
lblNumberdisplayed.Text = Count.ToString();
}
}
`
When the user clicks the Exit/Close btn, the application should close.
The only language I am familiar with is c# hence my writing it here and my company runs windows for all end users so I figured why not.
private void btnExit_Click(object sender, EventArgs e)
The code I have so far Image
If you aren't bound to use While loop you can try using session or the below:
private int count;
protected void btnCount_Click(object sender, EventArgs e)
{
count = Int32.Parse(lblCount.Text);
count++;
lblCount.Text = count.ToString();
}
Note: Though not tested, you can try the above.
I think the issue is the Count variable is local to the method, thus the do while never sees the reset.

How to print an array in forms (C#)

I have a mathematical problem and I´m trying to solve it, the problem is that you have 81 coins, but one is fake and it´s heavier than the others,you have to find out which one is the fake one by using a scale and doing only 4 comparisons.
I´m trying to make it like a game, when a users decides which coin will be the fake one, and the other player has to find it.
I made an array named monedasf and made all the values 0, so when the users type in the coin that wants to be the fake one, the value changes to 1. I´m trying right now to print the array, but I don´t know if I have to print it in a multiline textbox or where, here´s the code I have untill now.
public partial class Form1 : Form
{
public static int[] monedasf = new int[81];
public Form1()
{
InitializeComponent();
for( int i = 0; i<=80;i++)
{
monedasf[i] = 0;
}
}
private void label1_Click(object sender, EventArgs e)
{
}
private void button1_Click_1(object sender, EventArgs e)
{
int n;
n = Convert.ToInt32(textBox1.Text);
monedasf[n] = 1;
textBox1.Clear();
}
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text = Convert.ToString(monedasf[i]);
}
}
I have only BASIC KNOWLEDGE of programming, that´s why my code might be so primitive :D
Try using something like this:
private void button2_Click(object sender, EventArgs e)
{
for (int i = 0; i <= 80; i++)
textBox2.Text += monedasf[i].ToString() + " ";
}
If you want you can replace " " with any separator you want, like "\n" for newline.
Actually your code would work too, the problem is you were reseting textBox2's text by using assign operator:
textBox2.Text = Convert.ToString(monedasf[i]); // will clear and then print
textBox2.Text += Convert.ToString(monedasf[i]); // will not clear and print
All you need is not to reset previous text inside.
private void button2_Click(object sender, EventArgs e)
{
textBox2.Text = string.Join(", ", monedasf);
}
Use string.Join which is very useful for display.

Populating textBox with value depending on button click count

I am new to coding in C#. Currently I am encountering the problem of populating a textBox depending on the amount of times button1 is clicked. I have been able to populate option one but I am not sure how to get the second option after the second click and so fourth. How would I be able to do that? Also, Would I need to add a loop to start over after the fourth click?
Code
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = "Fruits"; //1st click
MyTextBox.Text = "Vegtables"; //2nd click
MyTextBox.Text = "Grains"; //3rd click
MyTextBox.Text = "Poultry"; //4th click
}
private List<string> messages= new List<string>(){"Fruits", "Vegetables", "Grains", "Poultry"};
private int clickCount = 0;
protected void Button1_Click(object sender, EventArgs e)
{
MyTextBox.Text = messages[clickCount];
clickCount++;
if (clickCount == messages.Count)
clickCount = 0;
}

Counting Clicks C#

Made a simple app which using a timer, counts the number of mouse clicks on a panel for a given duration... simple enough, all working, except it seems to fail to count quickly enough to register all the mouse clicks?
I am literally incrementing a private int value on the click event of the panel, and showing a message box with the results on tick. Any Ideas? Code below...
Matt.
public partial class Form1 : Form
{
int click = 0;
public Form1()
{
InitializeComponent();
}
private void panel1_Click(object sender, EventArgs e)
{
click++;
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
}
private void btnReset_Click(object sender, EventArgs e)
{
timer1.Stop();
txtClicks.Text = "";
txtTime.Text = "";
click = 0;
}
private void btnGo_Click(object sender, EventArgs e)
{
click = 0;
timer1.Interval = int.Parse(txtTime.Text) * 1000;
timer1.Start();
}
private void timer1_Tick(object sender, EventArgs e)
{
timer1.Stop();
MessageBox.Show(txtClicks.Text + " seconds up, No of clicks:" + click.ToString());
}
}
Use the MouseDown Event. That'll handle every time and negate the need to handle both Click and DoubleClick.
except it seems to fail to count quickly enough to register all the mouse clicks?
may be you should handle Mouse DoubleClick event as well as Mouse Click?
I would put money on it that some of the clicks are coming through so fast that...... they count as a double click.
If you add a double click handler, and increment the counter twice while in that handler, does it produce a more accurate result?

Categories

Resources