I'm trying to create 4 buttons on my form when I click on button1, but the buttons don’t show up. Why not?
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i=0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
}
}
You have only created them, but you also need to add them to your form with: this.Controls.Add(b[i]);
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i=0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
this.Controls.Add(b[i]);
}
}
All you do is create an array of buttons, and assign buttons at indices. Your form knows nothing of these buttons, they could be an array of integers or anything for all that matters at this point. You will need to put them in the form's container:
Controls.Add(b[i]);
Now your form will take ownership of them, managing disposal when the container is disposed.
Try this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace winFormButtons
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Button[] b = new Button[4];
for (int i = 0; i < 4; i++)
{
b[i] = new Button();
b[i].Name = "button" + i;
b[i].Location = new Point(43, 39 + 10 * i);
b[i].Size = new Size(158, 48);
b[i].Click += new EventHandler(OnClick);
this.Controls.Add(b[i]);
}
}
public void OnClick(object sender, EventArgs e)
{
MessageBox.Show("Hello Handler:" + ((Button)sender).Name);
}
}
}
Create a Panel on your form. and add this line in your code
panel1.Controls.Add(b[i])
Related
I'm trying to build a Russian Roulette style program where you click a button and it randomly selects one of 25 images to display on screen but I can't seem to figure out how to call the images using the generator.
It works fine when I select an image manually, as seen in my code below, but anything else seems to return an error.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
using System.Security.Cryptography;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label_click(object sender, EventArgs e)
{
Close();
}
int mouseX = 0, mouseY = 0;
bool mouseDown;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void GOBUTN_Paint(object sender, PaintEventArgs e)
{
}
//Sharing is caring: Communism goes here
private System.Windows.Forms.Timer timtim;
int rand0;
PictureBox Rooskie = new PictureBox();
Label test = new Label();
Random rando = new Random();
List<int> duplicheck = new List<int>();
private void boopthesnoot(object sender, EventArgs e)
{
dingding:
//Hell yeah, random numbers here
rand0 = rando.Next(1, 26);
/*string combowombo = string.Join(", ", duplicheck.ToArray());
test.Text = combowombo;
test.Font = new Font("Calibri", 20);
Controls.Add(test);
test.Location = new Point(0, 200);
test.Height = 1000;
test.Width = 1000;*/
if(duplicheck.Contains(rand0))
{
goto dingding;
}
else
{
GOBUTTON.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
//Fuckin image code goes here my dood
Rooskie.Width = 1160;
Rooskie.Height = 620;
Bitmap image = new Bitmap(WindowsFormsApp1.Properties.Resources._1);
Rooskie.Dock = DockStyle.Fill;
Rooskie.Image = (Image)image;
Controls.Add(Rooskie);
Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;
//Aww shit, it's that timer time
timtim = new System.Windows.Forms.Timer();
timtim.Tick += new EventHandler(clockfinish);
timtim.Interval = 3000;
timtim.Start();
duplicheck.Add(rand0);
if (duplicheck.Count == 25)
{
duplicheck = new List<int>();
}
}
}
private void clockfinish(object sender, EventArgs e)
{
//CEASE THE TIMER AND GIVE ME BACK MY BUTTON
Rooskie.Image = null;
timtim.Stop();
GOBUTTON.Show();
pictureBox1.Show();
pictureBox2.Show();
}
The expected result is when the user presses the button it calls up the image without having to load it from a folder.
I'm trying to exclude old results from a random number generator but I can't seem to get the variables to remember past two. I'm new to C# so there's probably some really simple solution I'm overlooking. It'd be super helpful if you guys could enlighten me.
I've already tried the code shown below, and I'm fairly sure I'm being stupid but I can't figure out why it's not working.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading.Tasks;
using System.IO;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void label_click(object sender, EventArgs e)
{
Close();
}
int mouseX = 0, mouseY = 0;
bool mouseDown;
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown = false;
}
private void GOBUTN_Paint(object sender, PaintEventArgs e)
{
}
//Sharing is caring: Communism goes here
private System.Windows.Forms.Timer timtim;
PictureBox Rooskie = new PictureBox();
int duplicheck = 0;
int duplicheck2 = 0;
int duplicheck3 = 0;
Label test = new Label();
private void boopthesnoot(object sender, EventArgs e)
{
dinging:
//yeah, random numbers here
Random rando = new Random();
int rand0 = rando.Next(1, 25);
test.Text = duplicheck + ", " + duplicheck2 + ", " + duplicheck3;
test.Font = new Font("Calibri", 20);
Controls.Add(test);
test.Location = new Point(0, 200);
test.Height = 1000;
test.Width = 1000;
if(duplicheck != rand0 && duplicheck2 != rand0 && duplicheck3 != rand0)
{
GOBUTTON.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
//image code goes here
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + "resources" + "\\" + rand0 + ".jpg";
Rooskie.Width = 1160;
Rooskie.Height = 620;
Bitmap image = new Bitmap(filepath);
Rooskie.Dock = DockStyle.Fill;
Rooskie.Image = (Image)image;
Controls.Add(Rooskie);
Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;
//Aww, it's that timer time
timtim = new System.Windows.Forms.Timer();
timtim.Tick += new EventHandler(clockfinish);
timtim.Interval = 5000;
timtim.Start();
}
else
{
goto dinging;
}
if (duplicheck != rand0)
{
duplicheck2 = duplicheck;
}
if(duplicheck != rand0)
{
duplicheck2 = duplicheck;
}
if (duplicheck2 != duplicheck)
{
duplicheck3 = duplicheck2;
}
duplicheck = rand0;
}
private void clockfinish(object sender, EventArgs e)
{
//CEASE THE TIMER AND GIVE ME BACK MY BUTTON
Rooskie.Image = null;
timtim.Stop();
GOBUTTON.Show();
pictureBox1.Show();
pictureBox2.Show();
}
The idea is that a random number generator picks an image and displays it. But I don't want the same image to show up multiple times in a row so this is supposed to give it a buffer i.e. If image 2 was displayed on the first button press, image 1 was displayed on the second, and image 3 was displayed on the third, the results should look like: duplicheck = 2 duplicheck2 = 1 duplicheck3 = 3
So with the help of everyone in the comments and a lot of messing around I wound up with this. It works perfectly to prevent any image from being displayed more than once per 25 clicks of the button. Thank you guys so much for the help on this! I don't think I'd have figured this out without you.
//Sharing is caring: Communism goes here
private System.Windows.Forms.Timer timtim;
int rand0;
PictureBox Rooskie = new PictureBox();
Label test = new Label();
Random rando = new Random();
List<int> duplicheck = new List<int>();
private void boopthesnoot(object sender, EventArgs e)
{
dingding:
//yeah, random numbers here
rand0 = rando.Next(1, 26);
/*string combowombo = string.Join(", ", duplicheck.ToArray());
test.Text = combowombo;
test.Font = new Font("Calibri", 20);
Controls.Add(test);
test.Location = new Point(0, 200);
test.Height = 1000;
test.Width = 1000;*/
if(duplicheck.Contains(rand0))
{
goto dingding;
}
else
{
GOBUTTON.Hide();
pictureBox1.Hide();
pictureBox2.Hide();
//image code goes here
Rooskie.Width = 1160;
Rooskie.Height = 620;
Bitmap image = new Bitmap(WindowsFormsApp1.Properties.Resources._1);
Rooskie.Dock = DockStyle.Fill;
Rooskie.Image = (Image)image;
Controls.Add(Rooskie);
Rooskie.SizeMode = PictureBoxSizeMode.CenterImage;
//Aww, it's that timer time
timtim = new System.Windows.Forms.Timer();
timtim.Tick += new EventHandler(clockfinish);
timtim.Interval = 3000;
timtim.Start();
duplicheck.Add(rand0);
if (duplicheck.Count == 25)
{
duplicheck = new List<int>();
}
}
}
so I created my own private void that can create buttons
private void addButtonsToForm()
{
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button();
currentNewButton.Size = new Size(20, 30);
currentNewButton.Location = new Point(20 + 25 * i, 420);
currentNewButton.Text = ((char)(65 + i)).ToString();
currentNewButton.Click += LetterClicked;
letters[i] = currentNewButton;
this.Controls.Add(letters[i]);
}
}
The buttons are alphabets and will be accessed when the user wants to choose a letter ... but the problem is I'm trying to figure out how to go back when the user clicked or selected a button..
Originally I wanted to do was i could just hide all the buttons created and just make the previous button visible but for some reason it only hides the only button that is clicked
//this is under private void LetterClicked(object sender, EventArgs e)
Button selectedLetter = (Button)sender;
selectedLetter.Enabled = false;
i thought of stupid codes like
addbuttonstoform().visible = false; but of course that won't work.. but you might get an idea to where i want to go.... it's a bit confusing to explain... I'm new in c# and i'm creating a guess the word game so help could be great..
Here is a working solution for your problem. See below:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Alphabets
{
public partial class Form1 : Form
{
private Button[] _letters;
public Form1()
{
InitializeComponent();
AddButtonsToForm();
}
private void AddButtonsToForm()
{
_letters = new Button[26];
for (int i = 0; i < 26; i++)
{
Button currentNewButton = new Button
{
Name = "BtnLetter"+ ((char)(65 + i)),
Size = new Size(20, 30),
Location = new Point(20 + 25 * i, 420),
Text = ((char) (65 + i)).ToString()
};
currentNewButton.Click += LetterClicked;
_letters[i] = currentNewButton;
this.Controls.Add(_letters[i]);
}
}
private void LetterClicked(object sender, EventArgs e)
{
var selectedLetter = (Button) sender;
//hide all other buttons
foreach (var letter in _letters)
{
if (letter.Text != selectedLetter.Text)
{
var buttons = this.Controls.Find("BtnLetter" + letter.Text, true);
buttons[0].Enabled = false;
}
}
}
}
}
I'm programming an calendar with C# right now.
If i call my calendar, it creates as much panels as the current month has days. But if I want to increase the current month by one, the panels from the current month are stilt there.
So i have to delete all my panels as soon as I change the month.
But how can i do it in this case ?
Thanks for the help.
Code eplain:
First I call the createPanel method, to create panels for the current month.
Next if I click the MonthAdd method, I want to delete all my created panels.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Globalization;
namespace Einteilungs_Plan
{
public partial class Kalender : Form
{
public DateTime TodayDate { get; set; }
int counting = 0;
public Kalender()
{
InitializeComponent();
//Kalenderwochen initialisieren
monat(counting);
createPanel(true);
}
public string monat(int adding)
{
string monat = DateTime.Now.AddMonths(adding).ToString("MMMM");
tbMonat.Text = monat;
return monat;
}
private void btnAddMonth_Click(object sender, EventArgs e)
{
counting++;
if(counting < 12)
{
monat(counting);
switch (counting)
{
case 0:
int number = 10;
break;
case 1:
break;
default:
break;
}
}
else
{
counting--;
}
}
private void btnRemoveMonth_Click(object sender, EventArgs e)
{
counting--;
if (counting > -1)
{
monat(counting);
}
else
{
counting++;
}
}
public void createPanel(bool remove)
{
var numDays = DateTime.DaysInMonth(DateTime.Today.Year, DateTime.Today.Month);
int locationX = 12;
int locationY = 74;
for (int i = 0; i <= numDays; i++)
{
//Create Panel
Panel test = new Panel();
//Fill Panel
test.Name = "panel" + i;
test.Width = 200;
test.Height = 100;
test.BackColor = Color.White;
test.Location = new System.Drawing.Point(locationX, locationY);
this.Controls.Add(test);
test.Show();
if(i == 6 || i == 13 || i == 20 || i == 28)
{
locationY += 106;
locationX = -194;
}
locationX += 206;
}
}
public void Kalender_Shown(object sender, EventArgs e)
{
}
private void Kalender_Load(object sender, EventArgs e)
{
}
private void btnNeuerEintrag_Click(object sender, EventArgs e)
{
Formular formular = new Formular();
formular.Show();
formular.Focus();
}
private void btnHinzufügen_Click(object sender, EventArgs e)
{
Formular formular = new Formular();
formular.Show();
formular.Focus();
}
}
}
...
for (int i = 0; i <= numDays; i++)
{
//Create Panel
test[i] = new Panel();
}
...
and then
this.Control.Remove(test[i]);
I'm not sure if I undestood you well but first simple solutiona that comes into my mind is that while creating panels you can keep them in some List for example and before generating new month you can call
foreach (var p in panels)
this.Controls.Remove(p);
I am trying to make pictureboxes fall continuously in the form.
Here is the code that I tried.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Rain_dropz
{
public partial class Form1 : Form
{
PictureBox[] RD = new PictureBox[500];
int ndrop = 0;
public Form1()
{
InitializeComponent();
}
public void dropIt()
{
for (int i = 0; i < ndrop; i++)
{
RD[i].Top += 10;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
private void Form1_Load(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
}
}
I think it is better to delete the picture boxes which disappear from the form. How to do that?
You can remove it by removing the picturebox from form controls list.
private void timer1_Tick(object sender, EventArgs e)
{
Random rnd = new Random();
int l = rnd.Next(1,545);
RD[ndrop] = new PictureBox();
RD[ndrop].BackColor = System.Drawing.Color.MediumBlue;
RD[ndrop].Size = new Size(5, 5);
RD[ndrop].Location = new Point(l, 0);
RD[ndrop].LocationChanged += pb_LocationChanged;
this.Controls.Add(RD[ndrop]);
ndrop++;
dropIt();
}
void pb_LocationChanged(object sender, EventArgs e)
{
// FORM_LASTBOUND is the Y-Axis point after which you wanted to remove the picturebox.
if ((sender as PictureBox).Top > FORM_LASTBOUND)
{
this.Controls.Remove(sender as PictureBox);
}
}