I am trying to set images to a grid I gave created, I am a noob, so please don't get mad at my coding :). I was wondering if there is a way to say set image to grid row 1, grid row 2, etc. I am trying to make a whack a mole game.
private void PopulateGrid()
{
double NumofImages = TUtils.GetIniInt(Moleini, "NumPictures", "pictures", 8);
int ImageSize = TUtils.GetIniInt(Moleini, "ImageSize", "imageSize", 50);
int ImageBorderSize = TUtils.GetIniInt(Moleini, "ImageBorder", "imageBorder", 2);
double NumberOfColumns = TUtils.GetIniInt(Moleini, "NumRowsColumns", "columnNum", 4);
// More Columns than Rows \\
if (NumberOfColumns > NumofImages)
{
MessageBox.Show("There is something wrong with the .ini file.");
Window1.Close();
}
// Math - Get Necessary Variables \\
int ColumnSize = (ImageSize + (4 * ImageBorderSize));
int RowSize = (ImageSize + (4 * ImageBorderSize));
int NumberofRows = (int)Math.Ceiling(NumofImages / NumberOfColumns);
int MainWindowWidth = (TUtils.ToInt(NumberOfColumns.ToString(), 4) * ColumnSize) + 15;
int MainWindowHeight = (NumberofRows * RowSize) + 35;
// Set Window Size \\
Window1.Width = MainWindowWidth;
Window1.Height = MainWindowHeight;
// Create Grid \\
Window1.Content = grid_Main;
grid_Main.Height = MainWindowHeight;
grid_Main.Width = MainWindowWidth;
// Grid Properties \\
for (int i = 0; i < NumberofRows; i++)
{
ColumnDefinition newColumn = new ColumnDefinition();
newColumn.Width = new GridLength(ColumnSize, GridUnitType.Pixel);
grid_Main.ColumnDefinitions.Add(newColumn);
}
for (int i = 0; i < NumberofRows; i++)
{
RowDefinition Row = new RowDefinition();
Row.Height = new GridLength(RowSize, GridUnitType.Pixel);
grid_Main.RowDefinitions.Add(Row);
}
// Fill Grid \\
int RowCount = 0;
int ColumnCount = 0;
for (int i = 0; i < NumofImages; i++)
{
grid_Main.Children.Add(grid_Main);
if (RowCount < NumberofRows)
{
if (ColumnCount < NumberOfColumns)
{
Console.WriteLine("ColumnCount: " + ColumnCount.ToString());
Grid.SetRow(grid_Main, ColumnCount);
Grid.SetColumn(grid_Main, ColumnCount);
ColumnCount++;
}
else
{
RowCount++;
ColumnCount = 0;
Grid.SetRow(grid_Main, ColumnCount);
Grid.SetColumn(grid_Main, ColumnCount);
ColumnCount++;
Console.WriteLine("RowCount: " + RowCount.ToString());
}
}
else
{
break;
}
}
}
...
private Image CreateImage(int ImageNum)
{
// Gets/Sets Necessary Variables \\
double ImageHeight = ImageSize * 0.7;
// Initialize Image \\
System.Windows.Controls.Image newImage = new Image();
// Image Properties \\
newImage.Width = ImageSize;
newImage.Height = ImageHeight;
// Define Name and Content \\
newImage.Name = "Image_" + ImageNum;
String ImageFunction = TUtils.GetIniString(Moleini, "Image" + ImageNum, "PictureFile", Root + "mole2.png");
if (File.Exists(ImageFunction))
{
newImage.Source = new BitmapImage(new Uri(ImageFunction));
}
else
{
MessageBox.Show("Cannot find " + ImageFunction + ".", "Please fix the ini file");
}
return newImage;
}
Related
I'm creating a new Picturebox via code, but now my TextBoxes don't give me any values. I think they went out of focus, or their controls aren't working anymore, here's the code so far:
private void button1_Click(object sender, EventArgs e)
{
int ErrorCode = 0;
string NewName = NewPointName.Text;
int X, Y;
Application.DoEvents();
if (NewPointName.Text == "")
ErrorCode = 1;
else
for (int i = 0; i < Names + 1; i++)
{
if (PointName[i] == NewName)
ErrorCode = 2;
}
if (ErrorCode > 0)
MessageBox.Show("Error " + ErrorCode);
else
{
if (Convert.ToInt32(NewPointXBox.Text) > 60)
X = 60;
else if (Convert.ToInt32(NewPointXBox.Text) < -60)
X = -60;
else if (NewPointXBox.Text == "")
X = 0;
else
X = Convert.ToInt32(NewPointXBox.Text);
if (Convert.ToInt32(NewPointYBox.Text) > 60)
Y = 60;
else if (Convert.ToInt32(NewPointYBox.Text) < -60)
Y = -60;
else if (NewPointYBox.Text == "")
Y = 0;
else
Y = Convert.ToInt32(NewPointYBox.Text);
Punkt.GiveName(NewName, Names);
Punkt.GiveCoordinates(X, Y, Names);
PointName[Names] = NewName;
NewPointName.Text = "";
NewPointXBox.Text = "";
NewPointYBox.Text = "";
Application.DoEvents();
UpdatePoint();
CreatePoint(X, Y, NewName, Names);
Names++;
ErrorCode = 0;
NewName = "";
}
}
public void CreatePoint(int X, int Y, string name, int i)
{
int StartPointX = 450, StartPointY = 450, Factor = 7;
if (RadioG6060.Checked)
{
StartPointX = 454;
StartPointY = 449;
Factor = 7;
}
Dot[i] = new PictureBox();
this.Controls.Add(Dot[i]);
Dot[i].Name = "PB_" + name;
Dot[i].Size = new Size(10, 10);
Dot[i].Image = Image.FromFile("../Dot.png");
Dot[i].Anchor = AnchorStyles.Left;
Dot[i].Location = new Point(StartPointX, StartPointY);
Dot[i].Visible = true;
InitializeComponent();
Dot[i].BringToFront();
Dot[i].Location = new Point(Dot[i].Location.X + (X * Factor), Dot[i].Location.Y - (Y * Factor));
Application.DoEvents();
}
I think it's the this.controls.Add(Dot[i]) that throws it off, because now I can't access the text in my NewPointName textbox.
How can I focus the program back on the form or do generally anything that could activate the boxes again?
I just solved the problem, I just had to leave out the InitializeComponent(), then it worked. Turns out that if you do that, the already existing components get blocked and can't be changed in any options of themselves anymore.
I have the following grid in the xaml file:
<Grid x:Name="gridPMP" HorizontalAlignment="Left" Height="285" Margin="23,116,0,-330" Grid.Row="1" VerticalAlignment="Top" Width="1238"/>
That grid is filled programmatically with this code .cs:
public void loadPMPTable()
{
gridPMP.Children.Clear();
gridPMP.RowDefinitions.Clear();
gridPMP.ColumnDefinitions.Clear();
MetodosAux aux = new MetodosAux();
String s = problemName.Content.ToString();
String listaPath = "./DataSaved/" + s + "/ListaDeMateriales.txt";
String arbolPath = "./DataSaved/" + s + "/ArbolDeMateriales";
String RIPath = "./DataSaved/" + s + "/RegistroInventarios.txt";
int cols = NumValue + 1;
int rows = aux.numeroLineasFichero(listaPath) + 1;
FileStream fs = new FileStream(listaPath, FileMode.Open, FileAccess.Read);
System.IO.StreamReader file = new System.IO.StreamReader(fs);
String linea;
for (int x = 0; x < cols; x++)
{
ColumnDefinition CD = new ColumnDefinition();
if (x==0)
{
CD.Width = new System.Windows.GridLength(120);
}
else
{
CD.Width = new System.Windows.GridLength(30);
}
//CD.Width = GridLength.Auto;
gridPMP.ColumnDefinitions.Add(CD);
}
for (int y = 0; y < rows; y++)
{
RowDefinition r = new RowDefinition();
//r.Height = GridLength.Auto;
r.Height= new System.Windows.GridLength(30);
gridPMP.RowDefinitions.Add(r);
}
for (int x = 0; x < cols; x++)
{
for (int y = 0; y < rows; y++)
{
if ((y == 0) && (x == 0)) //y=row index, x=column index
{
TextBox t = new TextBox();
t.Width = 170;
t.IsReadOnly = true;
t.Text = "Elemento/Día";
t.FontWeight = FontWeights.UltraBold;
Grid.SetColumn(t, x);
Grid.SetRow(t, y);
gridPMP.Children.Add(t);
}
else if ((y == 0) && (x >= 1))
{
TextBox t = new TextBox();
t.Width = 170;
t.IsReadOnly = true;
t.Text = x.ToString();
t.FontWeight = FontWeights.UltraBold;
Grid.SetColumn(t, x);
Grid.SetRow(t, y);
gridPMP.Children.Add(t);
}
else if (x == 0)
{
TextBox t = new TextBox();
t.Width = 170;
t.IsReadOnly = true;
linea = file.ReadLine();
t.Text = linea;
t.FontWeight = FontWeights.DemiBold;
Grid.SetColumn(t, x);
Grid.SetRow(t, y);
gridPMP.Children.Add(t);
}
else
{
TextBox tb = new TextBox();
tb.PreviewTextInput += textBoxValidator;
tb.Width = 170;
tb.Text = "0";
Grid.SetColumn(tb, x);
Grid.SetRow(tb, y);
gridPMP.Children.Add(tb);
}
}
}
file.Close();
fs.Close();
}//end loadPMPTable()
The number of columns and rows change depending on the structure of some files. So when there are more than 8 rows the grid doesnt display the data because there isnt enough space. That's why I want to make the grid scrollable. How can I do this programmatically or from the xaml file? I tried it from the xaml file but I didnt get it to work.
You can put it inside a ScrollViewer:
<ScrollViewer>
<Grid x:Name="gridPMP" HorizontalAlignment="Left" Height="285" Grid.Row="1" VerticalAlignment="Top"/>
</ScrollViewer>
I have been trying to create a chess strategy application. I seem to be having issues with trying to get the label1 control to populate during run time. I am actually pretty new to the idea of dynamically creating events like 'mouse enter, mouse leave' How do I get the label to show the coordinates in the mouse enter event
int currentXposition, currentYposition;
const string positionLabel = "Current Position: ";
private void Test_Load(object sender, EventArgs a)
{
var temp=Color.Transparent; //Used to store the old color name of the panels before mouse events
var colorName = Color.Red; //Color used to highlight panel when mouse over
int numBlocks = 8; //Used to hold the number of blocks per row
int blockSize=70;
//Initialize new array of Panels new
string[,] Position = new string[8, 8];
Panel[,] chessBoardPanels = new Panel[numBlocks, numBlocks];
string Alphabet = "A,B,C,D,E,F,G,H";
string Numbers ="1,2,3,4,5,6,7,8";
string[] alphaStrings = Numbers.Split(',');
string[] numStrings=Numbers.Split(',');
// b = sub[0];
int FirstValue, SecondValue;
//Store Position Values
for (int firstValue = 0; firstValue < 8; ++firstValue)
{
FirstValue = Alphabet[firstValue];
for (int SecValue = 0; SecValue < 8; ++SecValue)
{
SecondValue = Numbers[SecValue];
Position[firstValue, SecValue] = alphaStrings[firstValue] + numStrings[SecValue];
}
}
//Loop to create panels
for (int iRow = 0; iRow < numBlocks; iRow++)
for (int iColumn = 0; iColumn < numBlocks; iColumn++)
{
Panel p = new Panel();
//set size
p.Size = new Size(blockSize, blockSize);
//set back colour
p.BackColor = (iRow + (iColumn % 2)) % 2 == 0 ? Color.Black : Color.White;
//set location
p.Location = new Point(blockSize *iRow+15, blockSize * iColumn+15);
chessBoardPanels[iRow, iColumn] = p;
chessBoardPanels[iRow,iColumn].MouseEnter += (s,e) =>
{
currentXposition = iRow;
currentYposition = iColumn;
var oldColor = (s as Panel).BackColor;
(s as Panel).BackColor = colorName;
temp = oldColor;
label1.Text = Position[iRow, iColumn];
};
chessBoardPanels[iRow, iColumn].MouseLeave += (s, e) =>
{
(s as Panel).BackColor = temp;
};
groupBox1.Controls.Add(p);
}
}
Try this.. It was not populating because of a out of range.. iRow always = 8...
Add this class to your project.
public class ChessSquare
{
public string Letter { get; set; }
public int Number { get; set; }
public Color Color { get; set; }
public string Position
{
get { return string.Format("{0}{1}", Letter, Number); }
}
public ChessSquare()
{
}
public ChessSquare(string letter, int number)
{
Letter = letter;
Number = number;
}
}
Replace the FormLoad method with this:
int blockSize = 20;
Panel[,] chessBoardPanels = new Panel[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
ChessSquare sq = new ChessSquare(((char)(65+i)).ToString(), j);
sq.Color = (i + (j % 2)) % 2 == 0 ? Color.AliceBlue : Color.White;
Panel p = new Panel()
{ Size = new Size(blockSize, blockSize),
BackColor = sq.Color,
Tag = sq,
Location = new Point(blockSize * i + 15, blockSize * j+15),
};
p.MouseEnter+=new EventHandler(squareMouseEnter);
p.MouseLeave += new EventHandler(squareMouseLeave);
chessBoardPanels[i, j] = p;
groupBox1.Controls.Add(p);
}
}
And add those two methods to your code:
void squareMouseEnter(object sender, EventArgs e)
{
Panel p = (Panel)sender;
ChessSquare sq = (ChessSquare)p.Tag;
p.BackColor = Color.Aqua;
label1.Text = string.Format("Current position: {0}", sq.Position);
}
void squareMouseLeave(object sender, EventArgs e)
{
Panel p = (Panel) sender;
ChessSquare sq = (ChessSquare)p.Tag;
p.BackColor = sq.Color;
}
I there are several ways of doing it... This one is pretty straight forward.
I want to make a sudoku field of labels, for which I have the code.
Now I want thick lines between some labels so you can see which block of labels are together.
This code makes the labels:
const int spacing = 30; //ruimte tussen kotjes
int aantal = (int)nudColsPerBlock.Value * (int)nudRowsPerBlock.Value; //totaal aantal kotjes per rij/kolom
Label[][] SudokuRaster = new Label[aantal][];
for (int x = 0; x < aantal; x++)
{
SudokuRaster[x] = new Label[aantal];
for (int y = 0; y < aantal; y++)
{
SudokuRaster[x][y] = new Label();
SudokuRaster[x][y].BorderStyle = BorderStyle.FixedSingle;
SudokuRaster[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
SudokuRaster[x][y].Name = "Sudoku" + x.ToString() + "," + y.ToString();
SudokuRaster[x][y].Size = new Size(spacing, spacing);
SudokuRaster[x][y].TabIndex = 0;
SudokuRaster[x][y].MouseClick += new System.Windows.Forms.MouseEventHandler(this.MouseClick);
}
this.Controls.AddRange(SudokuRaster[x]);
}
I need to draw a rectangle, with a number inside, in a C# console app and using extended ASCII. How do I go about it?
This is for a demo.
public class ConsoleRectangle
{
private int hWidth;
private int hHeight;
private Point hLocation;
private ConsoleColor hBorderColor;
public ConsoleRectangle(int width, int height, Point location, ConsoleColor borderColor)
{
hWidth = width;
hHeight = height;
hLocation = location;
hBorderColor = borderColor;
}
public Point Location
{
get { return hLocation; }
set { hLocation = value; }
}
public int Width
{
get { return hWidth; }
set { hWidth = value; }
}
public int Height
{
get { return hHeight; }
set { hHeight = value; }
}
public ConsoleColor BorderColor
{
get { return hBorderColor; }
set { hBorderColor = value; }
}
public void Draw()
{
string s = "╔";
string space = "";
string temp = "";
for (int i = 0; i < Width; i++)
{
space += " ";
s += "═";
}
for (int j = 0; j < Location.X ; j++)
temp += " ";
s += "╗" + "\n";
for (int i = 0; i < Height; i++)
s += temp + "║" + space + "║" + "\n";
s += temp + "╚";
for (int i = 0; i < Width; i++)
s += "═";
s += "╝" + "\n";
Console.ForegroundColor = BorderColor;
Console.CursorTop = hLocation.Y;
Console.CursorLeft = hLocation.X;
Console.Write(s);
Console.ResetColor();
}
}
This is an extension method to String, which will draw a console box around a given string. Multi-line support included.
i.e.
string tmp = "some value"; Console.Write(tmp.DrawInConsoleBox());
public static string DrawInConsoleBox(this string s)
{
string ulCorner = "╔";
string llCorner = "╚";
string urCorner = "╗";
string lrCorner = "╝";
string vertical = "║";
string horizontal = "═";
string[] lines = s.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);
int longest = 0;
foreach(string line in lines)
{
if (line.Length > longest)
longest = line.Length;
}
int width = longest + 2; // 1 space on each side
string h = string.Empty;
for (int i = 0; i < width; i++)
h += horizontal;
// box top
StringBuilder sb = new StringBuilder();
sb.AppendLine(ulCorner + h + urCorner);
// box contents
foreach (string line in lines)
{
double dblSpaces = (((double)width - (double)line.Length) / (double)2);
int iSpaces = Convert.ToInt32(dblSpaces);
if (dblSpaces > iSpaces) // not an even amount of chars
{
iSpaces += 1; // round up to next whole number
}
string beginSpacing = "";
string endSpacing = "";
for (int i = 0; i < iSpaces; i++)
{
beginSpacing += " ";
if (! (iSpaces > dblSpaces && i == iSpaces - 1)) // if there is an extra space somewhere, it should be in the beginning
{
endSpacing += " ";
}
}
// add the text line to the box
sb.AppendLine(vertical + beginSpacing + line + endSpacing + vertical);
}
// box bottom
sb.AppendLine(llCorner + h + lrCorner);
// the finished box
return sb.ToString();
}
Output like this
Like this?
This worked for me:
Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine("┌─┐");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
[EDIT]
Answer to the sub-question in the comment:
Console.OutputEncoding = Encoding.GetEncoding(866);
Console.WriteLine(" ┌─┐");
Console.WriteLine(" │1│");
Console.WriteLine("┌─┼─┘");
Console.WriteLine("│1│");
Console.WriteLine("└─┘");
You can use CsConsoleFormat† to draw with ASCII border symbols in console.
Drawing a number within a rectangle with "double" lines:
ConsoleRenderer.RenderDocument(
new Document()
.AddChildren(
new Border {
Stroke = LineThickness.Wide,
Align = HorizontalAlignment.Left
}
.AddChildren(1337)
)
);
You can change Stroke = LineThickness.Wide line to change the style of lines. LineThickness.Single would produce thin single lines, new LineThickness(LineWidth.Single, LineWidth.Wide) would produce single vertical and double horizontal lines.
Here's what it looks like:
You can also use ConsoleBuffer class to draw lines explicitly (argument names added for clarity):
using static System.ConsoleColor;
var buffer = new ConsoleBuffer(width: 6);
buffer.DrawHorizontalLine(x: 0, y: 0, width: 6, color: White);
buffer.DrawHorizontalLine(x: 0, y: 2, width: 6, color: White);
buffer.DrawVerticalLine(x: 0, y: 0, height: 3, color: White);
buffer.DrawVerticalLine(x: 5, y: 0, height: 3, color: White);
buffer.DrawString(x: 1, y: 1, color: White, text: "1337");
new ConsoleRenderTarget().Render(buffer);
† CsConsoleFormat was developed by me.
Problem with above code is extra spaces, if you draw multiple rectangles, it causes mess.
here is a code which draw rectangles recursively without extra spaces.
public class AsciDrawing
{
public static void TestMain() {
var w = Console.WindowWidth;
var h = Console.WindowHeight;
RecursiveDraw(16, 8, new Point(w/2-8, h/2-4), ConsoleColor.Black);
Console.CursorTop = h;
Console.CursorLeft = 0;
}
public static void RecursiveDraw(int Width, int Height, Point Location, ConsoleColor BorderColor) {
if(Width < 4 || Height < 2) return;
Draw(Width, Height, Location, BorderColor); //Commnet this draw and and Uncomment Draw bellow to see the difference.
Thread.Sleep(500);
//Comment or Uncomment to see how many recursive calls you want to make
//The best is to comment all execpt 1 and then uncomment 1 by 1
RecursiveDraw(Width/2, Height/2, new Point(Location.X-Width/4, Location.Y-Height/4), ConsoleColor.Green); //Left Top
RecursiveDraw(Width / 2, Height / 2, new Point(Location.X + 3* Width / 4, Location.Y + 3* Height / 4), ConsoleColor.Red); //Right Bottom
RecursiveDraw(Width / 2, Height / 2, new Point(Location.X + 3* Width / 4, Location.Y - Height / 4), ConsoleColor.Blue); //Right Top
RecursiveDraw(Width / 2, Height / 2, new Point(Location.X - Width / 4, Location.Y + 3* Height / 4), ConsoleColor.DarkMagenta); // Left Bottom
//Draw(Width, Height, Location, BorderColor);
}
public static void Draw(int Width, int Height, Point Location, ConsoleColor BorderColor)
{
Console.ForegroundColor = BorderColor;
string s = "╔";
string temp = "";
for (int i = 0; i < Width; i++)
s += "═";
s += "╗" + "\n";
Console.CursorTop = Location.Y;
Console.CursorLeft = Location.X;
Console.Write(s);
for (int i = 0; i < Height; i++) {
Console.CursorTop = Location.Y + 1 + i;
Console.CursorLeft = Location.X;
Console.WriteLine("║");
Console.CursorTop = Location.Y + 1 + i;
Console.CursorLeft = Location.X + Width+1;
Console.WriteLine("║");
}
s = temp + "╚";
for (int i = 0; i < Width; i++)
s += "═";
s += "╝" + "\n";
Console.CursorTop = Location.Y+Height;
Console.CursorLeft = Location.X;
Console.Write(s);
Console.ResetColor();
}
}
public record Point(int X, int Y);