In UWP i would like to give my button a random location on my screen when it is clicked on.
My code looks like this:
private void DeclareHere()
{
Random randomGenerator = new Random();
int iRndX = randomGenerator.Next(1, 640);
int iRndY = randomGenerator.Next(1, 360);
btnRandom.Margin.Left.Equals(iRndX);
btnRandom.Margin.Top.Equals(iRndY);
}
private void btnRandom_Click(object sender, RoutedEventArgs e)
{
DeclareHere();
}
What is it that i'm missing? Would help if you could explain how you accomplished the solution to this.
Equals is just a comparison operator.
ex:
bool isEqual = btnRandom.Margin.Left.Equals(iRndY);
You need to assign margin values
So, you should change this:
btnRandom.Margin.Left.Equals(iRndX);
btnRandom.Margin.Top.Equals(iRndY);
with this:
btnRandom.Margin = new Thickness { Left = iRndX, Top = iRndY};
Related
How to make the letter size change randomly every time you press the button. Here is a code that randomly shows the letter, the color of the letter
public partial class Form1 : Form
{
Color[] co = new Color[4] { Color.Red, Color.Green, Color.Blue, Color.Orange };
int a;
public Form1()
{
InitializeComponent();
}
Random rnd = new Random();
char[] letters = "АБВГДЕЄЖЗИІЇЙКЛМНОПРСТУФХЦЧШЩЬЮЯ".ToArray();
string output;
void generate(int NoOfLetter)
{
output = null;
for (int i = 0; i < NoOfLetter; i++)
{
output += letters[rnd.Next(0, letters.Length)];
}
textBox1.Text = output;
}
private void button1_Click(object sender, EventArgs e)
{
Random rndcol = new Random();
a = rndcol.Next(0, 4);
textBox1.ForeColor = co[a];
generate(1);
}
}
The principle is quite similar to the way you randomly changed the foreground color in your code.
A WinForms TextBox has a property: Font, that you can access to change all font attributes including size.
In order to keep the current font properties except size you can use:
float fontSize = ... // determine randomly or otherwise
textBox1.Font = new Font(textBox1.Font.FontFamily, fontSize);
So I have five text blocks I've added to a list, and each one is supposed to get its own random number between one and 6.
I know I can just do a new int for each text block (int randomNumberOne, randomNumberTwo, etc) but I'm trying to see if I can figure out how to make a list and a for each loop to work.
Is there some way to edit the content of a TextBox in a list as it goes through? If there is, I haven't found any way to do so.
Here's my code so far.
List<TextBlock> randomBoxList = new List<TextBlock>();
public MainWindow()
{
InitializeComponent();
randomBoxList.Add(randomBoxOne);
randomBoxList.Add(randomBoxTwo);
randomBoxList.Add(randomBoxThree);
randomBoxList.Add(randomBoxFour);
randomBoxList.Add(randomBoxFive);
}
Random randomGenerator = new Random();
int randomNumber;
private void randomButton_Click(object sender, RoutedEventArgs e)
{
foreach (TextBlock textBlock in randomBoxList)
{
randomNumber = randomGenerator.Next(1, 7);
//Code to change randomBox content goes here.
}
}
If this is WPF, you should just be able to use textBlock.Text property like so:
public partial class MainWindow : Window
{
List<System.Windows.Controls.TextBlock> randomBoxList = new List<System.Windows.Controls.TextBlock>();
public MainWindow()
{
InitializeComponent();
randomBoxList.Add(randomBoxOne);
randomBoxList.Add(randomBoxTwo);
randomBoxList.Add(randomBoxThree);
randomBoxList.Add(randomBoxFour);
randomBoxList.Add(randomBoxFive);
}
Random randomGenerator = new Random();
int randomNumber;
private void randomButton_Click(object sender, RoutedEventArgs e)
{
foreach (System.Windows.Controls.TextBlock textBlock in randomBoxList)
{
randomNumber = randomGenerator.Next(1, 7);
textBlock.Text = randomNumber.ToString();
}
}
}
My program creates 5 different labels with a cube form and they just drop down. When I press on them, they come invisible. I want to check if all of them are invisible, but don't know how to do so. Tried going through this site, found a solution with bool, but it just doesn't work my way. Also when my labels appear,you can see only 4 of them.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Label [] kubeliai = new Label [5];
int poz = 100;
private void Form1_Load(object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i] = new Label();
Controls.Add(kubeliai[i]);
Random pos = new Random();
kubeliai[i].Top = 50;
kubeliai[i].Left = poz;
poz += pos.Next(50, 200);
kubeliai[i].BackColor = Color.Red;
kubeliai[i].Height = 20;
kubeliai[i].Width = 20;
kubeliai[i].Click += new EventHandler(kubelio_clickas);
}
Timer kritimo_laikrodis = new Timer();
kritimo_laikrodis.Interval = 10;
kritimo_laikrodis.Tick += new EventHandler(laikrodis);
kritimo_laikrodis.Enabled = true;
}
void kubelio_clickas (object sender, EventArgs e)
{
((Label)sender).Visible = false;
}
void laikrodis (object sender, EventArgs e)
{
for (int i = 0; i < kubeliai.Length; i++)
{
kubeliai[i].Top += 1;
if (kubeliai.All.Visible == false) // this is an error
{
kubeliai[i].Visible = true;
kubeliai[i].Top = 50;
Random pos = new Random();
poz += pos.Next(50, 200);
}
}
}
Using Linq you can check if all are invisible in this way
var areAllInvisible = kubeliai.All(l => l.Visible == false);
if (areAllInvisible)
{
// do something
}
when my labels appear you can see only 4 of them.
That's because the way you are picking random numbers is picking the same numbers each time and you are therefore placing your labels on top of each other. Read the first paragraph of the Random() documentation:
Different Random objects that are created in close succession by a call to the default constructor will have identical default seed values and, therefore, will produce identical sets of random numbers. This problem can be avoided by using a single Random object to generate all random numbers.
Use new Random() once in your class definition like this:
Label [] kubeliai = new Label [5];
Random pos = new Random();
And remove it everywhere else in your program.
I'm trying to change randomly the location of a button when the mouse is hover it. To do that, I'm using the following source code :
private int modifX()
{
int rdmx;
int x_max = this.Width;
Random rdm = new Random();
rdmx = rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
The problem is that my button's position is always on a straight line like that
How can I fix it? I tried to use bt_win.Location.X = modifX(); on the mouseEnter event But it seems that I can't handle Location.X or Location.Y
I don't really get what I'm doing wrong, anyone got an idea and could explain me what I'm doing wrong?
You need to use the same instance of the Random class.
When you create two instance of the Random class closely, they can share the same seed. So the same number will be generated.
private Random _rdm = new Random();
private int modifX()
{
int x_max = this.Width;
int rdmx = _rdm.Next(0, x_max);
return rdmx;
}
private int ModifY(){
// same with y_max = this.Height;
}
private void bt_win_MouseEnter(object sender, EventArgs e)
{
bt_win.Location = new Point(modifX(), modifY());
}
I am kinda stuck and I need help.
My goal is to make a little "game". It should have 3 pictureboxes and it should randomly display/change numbers (pictures) 1 to 6 every 0,5sec. When I hit STOP, it should stop the numbers and I should get points (score) based on nubmers. 3 same numbers = +10 points, 2 same numbers = +5 points, no same numbers = -5 points. Then it should display highest score achieved (Max Score).
http://i.imgur.com/kubQBST.png
Please, give me some tips what to do.
Thanks a lot, regards Peter
You can use a Timer with random something like this :
Random rnd1 = new Random(Environment.TickCount);
Image[] Images = new Image[6];
int[] CurrentStatus = new int [3];
Images[0] = Image.FromFile("FileNameFornumber1");
Images[1] = Image.FromFile("FileNameFornumber2");
Images[2] = Image.FromFile("FileNameFornumber3");
Images[3] = Image.FromFile("FileNameFornumber4");
Images[4] = Image.FromFile("FileNameFornumber5");
Images[5] = Image.FromFile("FileNameFornumber6");
//change numbers every tick
private Timer_TickHandler(object sender, EventArgs e)
{
this.CurrentState[0] = rnd1.Next(1, 6);
this.CurrentState[1] = rnd1.Next(1, 6);
this.CurrentState[2] = rnd1.Next(1, 6);
this.PictureBox1.Image = Images[this.CurrentStatus[0]-1];
this.PictureBox2.Image = Images[this.CurrentStatus[1]-1];
this.PictureBox3.Image = Images[this.CurrentStatus[2]-1];
}
you have to write an event for the stop button to deactivate the timer and calculate the score based on the CurrentStatus Array and I think the calculation alg must be like this :
int score = this.CurrentStatus.Sum();
if (this.CurrentStatus[0] == this.CurrentStatus[1] && this.CurrentStatus[1] == this.CurrentStatus[2])
score +=10;
else
{
for (int i=0; i<3; i++)
{
for (int j=i+1; j<3; j++)
{
if (this.CurrentStatus[i] == this.CurrentStatus[j])
{
score+=5;
break;
}
}
}
}
for start make random picture on button press.
How add images into resources
For example
public partial class Form1 : Form
{
List<Bitmap> picturesList = new List<Bitmap>(); //Array of pictures
Random random = new Random();
public Form1()
{
InitializeComponent();
//Load all pictures from resources into array
picturesList.Add(Properties.Resources.pic1);
picturesList.Add(Properties.Resources.pic2);
picturesList.Add(Properties.Resources.pic3);
//Set random image into picture box
RandomChangeImage();
}
public void RandomChangeImage()
{
//Generate random number. (random index between 0 - array.count )
int randomIndex = random.Next(0, picturesList.Count);
//Set random image from array
YourPictureBoxName.Image = picturesList[randomIndex];
}
}
Now you can use RandomChangeImage(); in your code. Aflter you done you can continue with timer.
Now add Timer from toolbox into designer.
Double click on timer.
It generated this
private void timer1_Tick(object sender, EventArgs e)
{
//Call random change image
RandomChangeImage();
}
in you buttonstart_Click call timer1.Start(); for example
private void btnStart_Click(object sender, EventArgs e)
{
timer1.Start();
}
if you want change tick in timer you can use
timer1.Interval = 500; for 0.5s. where you want.
if you want stop timer use
timer1.Stop();