Windows Forms C# Buttons in TableLayoutPanel Margins being overwritten - c#

Buttons very close to each other.
Component Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Drawing;
namespace CustomExplorer.Components
{
public partial class RunningApps : TableLayoutPanel
{
public RunningApps()
{
InitializeComponent();
}
public RunningApps(IContainer container)
{
container.Add(this);
InitializeComponent();
this.ColumnStyles.Clear();
this.RowStyles.Clear();
this.RowStyles.Add(new RowStyle(SizeType.Percent, 100));
this.HandleCreated += new EventHandler(this.Form_Load);
}
private void Form_Load(object sender, EventArgs e)
{
// Process list and spawn buttons
List<WindowData> ar = WindowsTasks.GetActiveTasks();
this.ColumnCount = ar.Count;
this.MinimumSize = new Size(50 * ar.Count, 54);
this.Size = new Size(50 * ar.Count, 54);
for (int i = 0; i < ar.Count; i++)
{
this.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100 / this.ColumnCount));
Button button = new Button();
WindowData w = ar[i];
// Setup Button Style
button.Name = string.Format("Button_{0:D}", i);
button.Text = string.Format("Button_{0:D}", i);
button.BackColor = Color.Transparent;
button.FlatStyle = FlatStyle.Flat;
button.FlatAppearance.BorderSize = 0;
// Icon
button.Image = w.icon.ToBitmap();
button.ImageAlign = ContentAlignment.MiddleCenter;
// Sizes
button.MinimumSize = new Size(50, 54);
button.Margin = new Padding(5);
button.Size = new Size(50, 54);
// Utils
this.Controls.Add(button, i, 0);
}
}
}
}
The way buttons are exposed:
The function gives the running process that have "Graphics", add them all to Array ar. Then the for loop adds columns for each of the buttons and them other for loop add the buttons with the image
Tried all that I could found. Margins, Paddings and stuff seems to be only applied at the first Button.
The component is imported to a tablelayout at main Component

Related

Change the color of the Grid Cell in C#

I am writing a program for Occupancy grid mapping in C#. I want to change the color of the grid cell.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows;
namespace gridappl
{
public partial class Form1 : Form
{
private Window mainWindow;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Create the application's main window
mainWindow = new Window();
mainWindow.Title = "Grid Sample";
// Create the Grid
Grid myGrid = new Grid();
myGrid.Width = 600;
myGrid.Height = 600;
//myGrid.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
//myGrid.VerticalAlignment = System.Windows.VerticalAlignment.Top;
myGrid.ShowGridLines = true;
// Define the Columns
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
// Define the Rows
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
// Add the Grid as the Content of the Parent Window Object
mainWindow.Content = myGrid;
mainWindow.Show();
}
}
}
I tried using many commands like the following:
myGrid.Background = Brushes.Azure;
myGrid.Background = System.Drawing.Brushes.Red;
But still, the program throws errors. What should be done to change the grid cell color as per our choice?
for (int i = 0; i < myGrid.Rows.Count; i++)
{
for (int j = 0; j < myGrid.Rows[i].Cells.Count; j++)
{
myGrid.Rows[i].Cells[j].Style.BackColor = /*color you want*/
}
}
Try this to fill backcolor dynamically.
or try this
myGrid.Background= new SolidColorBrush(Colors.Green);
You can try this code:
for (int i = 0; i < myGrid.Rows.Count; i++)
{
myGrid.Rows[i].Cells[/*Cell you want the color*/].Style.BackColor = /*color you want*/
}
The for loop is there in case you want to go through all rows or you can just do
myGrid.Rows[/*specific row number*/].Cells[/*Cell you want the color*/].Style.BackColor = /*color you want*/
Example:
myGrid.Rows[0].Cells[1].Style.BackColor = Color.Red;
foreach (DataGridViewRow row in dgv_List.Rows)
{
if (intcolumncunt == 2)
{
if(Convert.ToInt32(row.Cells[2].Value.ToString()) > Convert.ToInt32(row.Cells[3].Value.ToString()))
{
row.DefaultCellStyle.BackColor = Color.Red;
}
}
}

Create Dynamic Web Control in c# desktop application

I just want to create multiple web control using c# in visual studio. I have written the code for that but it create only once, I think it show the control created at the last in the loop.
Here is the code:
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;
namespace MDMScreenSharing
{
public partial class Form2 : Form
{
private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;
public Form2()
{
InitializeComponent();
this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
this.AutoSize = true;
this.Padding = new Padding(0, 0, 20, 20);
this.StartPosition = FormStartPosition.CenterScreen;
}
private void Form2_Load(object sender, EventArgs e)
{
int inputNumber =5;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for (int i = 1; i <= inputNumber; i++)
{
int j = 1;
String wbname = "br" + i;
Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Width = 200;
gw.Height = 200;
gw.Parent = panel1;
gw.Name = wbname;
gw.Location = new Point(gw.Width, panel1.Bottom + (i * 30));
gw.Navigate("http://192.168.1.162:8080");
geckowebbrouser.Add(gw);
this.Controls.Add(gw);
j = j*gw.Width;
}
}
}
}
This will show the web control created at the last. I think the program should be more dynamic for that. What am I doing wrong?
The form output, that show only once.
with the new addition of code i have code that one of you provided. the code is
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;
namespace MDMScreenSharing
{
public partial class Form2 : Form
{
private List<Skybound.Gecko.GeckoWebBrowser> geckowebbrouser;
public Form2()
{
InitializeComponent();
// this.AutoSizeMode = AutoSizeMode.GrowAndShrink;
// this.AutoSize = true;
//this.Padding = new Padding(0, 0, 20, 20);
// this.StartPosition = FormStartPosition.CenterScreen;
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
this.flowLayoutPanel1.TabIndex = 0;
}
private void Form2_Load(object sender, EventArgs e)
{
int inputNumber =4;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for (int i = 1; i <= inputNumber; i++)
{
String wbname = "br" + i;
Skybound.Gecko.GeckoWebBrowser gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Parent = flowLayoutPanel1;
gw.Width = 200;
gw.Height = 200;
gw.Name = wbname;
gw.Navigate("http://192.168.1.162:8080");
geckowebbrouser.Add(gw);
flowLayoutPanel1.Controls.Add(gw);
}
}
}
}
but the problem in this case only one browser window navigate the page.
like this
as you can see.
I think better approach will be using FlowLoayoutPanel so that you don't have to handle the position of each new control.
Go like this:
Add the FlowLayoutPanel through visual designer from toolbox.
Then on your designer.cs you should automatically have:
//
// flowLayoutPanel1
//
this.flowLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill;
this.flowLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.flowLayoutPanel1.Name = "flowLayoutPanel1";
this.flowLayoutPanel1.Size = new System.Drawing.Size(602, 395);
this.flowLayoutPanel1.TabIndex = 0;
FormLoad event:
private void Form1_Load ( object sender, EventArgs e )
{
int inputNumber = 5;
geckowebbrouser = new List<Skybound.Gecko.GeckoWebBrowser>();
for ( int i = 1; i <= inputNumber; i++ )
{
int j = 1;
String wbname = "br" + i;
var gw = new Skybound.Gecko.GeckoWebBrowser();
gw.Width = 300;
gw.Height = 300;
gw.Name = wbname;
geckowebbrouser.Add(gw);
flowLayoutPanel1.Controls.Add(gw);
gw.Navigate("http://www.google.com");
}
}
Update
The only difference from the WebBrowser implementation is that you need to call the Navigate method after you've added the control to the panel. Check the updated code above.
I have tested with default Skybound.Gecko.GeckoWebBrowser control too, and it's working just fine too:

Moving images from a dynamic list PictureBox

I am initiating me into programming thanks to Stack Overflow.
The game I'm doing in c #, consists of several bees flying around the desktop, which I have to give Click and SCORE is going to increase by a certain time.
To which I did the following:
Create a list PictureBox dynamically (at runtime): OK
Load PictureBox with these GIF images randomly: OK
![Bee Games][1]
In this part I was stuck:
Place these randomly PictureBox (in the bottom of the form).
Make the PictureBox move randomly (more or less marked routes).
each PictureBox Click Event, Change the PictureBox image and hide (visible = false), and increase in SCORE + 1.
I need help please.
My code is as follows:
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;
using System.IO;
namespace MiPrimerJuego
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox> objeto = new List<PictureBox>();
for (int i = 0; i < 10; i++, x += 90)
{
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
var rand = new Random();
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
}
Declare rand and files outside the for-loop and objeto outside the function/void as a Private member variable:
private List<PictureBox> objeto = new List<PictureBox>();
private void button1_Click(object sender, EventArgs e)
{
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
int x = 20;
int y = 600;
var rand = new Random();
for (int i = 0; i < 10; i++)
{
x += 90;
PictureBox pBox = new PictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = new System.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
Sorry, I did not explain properly.
What I do is this:
1 Make the PictureBox move randomly from the bottom of the form (more or less marked routes).
2 In the Click event of each PictureBox, Changing the PictureBox image and hide (visible = false), and increase in SCORE + 1.
My code is as follows:
using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Windows.Forms;
using System.IO;
namespaceMiPrimerJuego
{
publicpartialclassForm1 : Form
{
public Form1()
{
InitializeComponent();
}
privatevoid button1_Click(object sender, EventArgs e)
{
int x = 20;
int y = 600;
List<System.Windows.Forms.PictureBox>objeto = newList<PictureBox>();
for (inti = 0; i< 10; i++, x += 90)
{
PictureBoxpBox = newPictureBox();
pBox.Height = 80;
pBox.Width = 50;
pBox.Location = newSystem.Drawing.Point(x, y);
objeto.Add(pBox);
pBox.SizeMode = PictureBoxSizeMode.StretchImage;
Controls.Add(pBox);
var rand = newRandom();
var files = Directory.GetFiles(Application.StartupPath + #"/Images", "*.gif");
pBox.Image = System.Drawing.Bitmap.FromFile(files[rand.Next(files.Length)]);
}
}
}
}

How to create object in Form1 from another class?

I am trying to write program with C# to dynamicaly create PictureBox'es from another class.
Here what is done:
In class Form1 creating object of another class and call procedure in that class which creates objects(many PictureBox'es) I want
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 Lines_online
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//trying to draw NxM table
//where N is horizontal cell count M is vertical cell count
Table tbl = new Table();
tbl.Tablenm(9, 9);
}
}
}
The class Table code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
namespace Lines_online
{
public class Table : Form1
{
List<PictureBox> cell = new List<PictureBox>();
public void Tablenm(int n, int m)
{
for (int i = 0; i < n*m; i++)
{
PictureBox pict = new PictureBox();
cell.Add(pict);
cell[i].Size = new Size(20, 20);
cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
cell[i].Image = Properties.Resources.lang20x20;
Controls.Add(cell[i]);
}
}
}
}
Then code is executed it does nothing and gives no errors (I think it creates the objects somethere, but definetaly not in Form1 window). If procedure in the class Table moved to Form1 class it works perfectly, but I want to control it from other class to reduce code size in Form1. I tried to create Form1 object in Class Table:
Form1 frm1 = new Form1();
It did not help. What I am doing wrong, or maybe using bad approach to this problam?
Note: in your code you are inheriting table from Form1. This does mean that Table represents a new form, instead of a reference to the current existing form, and therefore it doesn't do anything.
In the Controls property of your original form, you are just adding another form, which definitely isn't the behaviour that you need.
I've tooked your code as a start and adapted it a bit.
I do think that this should work.
Create your class Table, but DON'T inherit from Form1.
Make your class that it looks like the following:
public class Table
{
public List<PictureBox> Render(int n, int m)
{
List<PictureBox> returnList = new List<PictureBox>();
for (int i = 0; i < n*m; i++)
{
PictureBox pict = new PictureBox();
pict.Size = new Size(20, 20);
pict.Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
pict.Image = Properties.Resources.lang20x20;
returnList.Add(pict);
}
return returnList;
}
}
Then, in your main form, you can call your method:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var table = new Table();
IEnumerable<Control> controls = table.Render(10, 10);
foreach (var control in controls)
{ this.Controls.Add(control); }
}
}
I believe that should work, but I haven't tested it. Also note that this code is not optimized. You should have parameters passed to the function in order to make it more generic.
//You need to add picturebox control to form1 control in loop,
//Form1.Control.Add(cell[i]);
//-------------------
public void Tablenm(int n, int m, Form Form1)
{
for (int i = 0; i < n*m; i++)
{
PictureBox pict = new PictureBox();
cell.Add(pict);
cell[i].Size = new Size(20, 20);
cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
cell[i].Image = Properties.Resources.lang20x20;
Form1.Control.Add(cell[i]);
}
}
You can return the List<PictureBox> to Form1
public List<PictureBox> Tablenm(int n, int m)
{
for (int i = 0; i < n*m; i++)
{
PictureBox pict = new PictureBox();
cell.Add(pict);
cell[i].Size = new Size(20, 20);
cell[i].Location = new Point(30 + (i % n) * 19, 30 + (i / m) * 19);
cell[i].Image = Properties.Resources.lang20x20;
}
return cell;
}
Then add it inside you form:
private void Form1_Load(object sender, EventArgs e)
{
//trying to draw NxM table
//where N is horizontal cell count M is vertical cell count
Table tbl = new Table();
var pictureBoxes = tbl.Tablenm(9, 9)
foreach (var pictureBox in pictureBoxes)
{
Controls.Add(picturebox)}
}
}

WPF brownian motion: updating state with a thread

Here's a little Brownian motion demo in WPF:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Threading;
namespace WpfBrownianMotion
{
static class MiscUtils
{
public static double Clamp(this double n, int low, double high)
{
return Math.Min(Math.Max(n, low), high);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Width = 500;
Height = 500;
var canvas = new Canvas();
Content = canvas;
var transform = new TranslateTransform(250, 250);
var circle = new Ellipse()
{
Width = 25,
Height = 25,
Fill = Brushes.PowderBlue,
RenderTransform = transform
};
canvas.Children.Add(circle);
var random = new Random();
var thread =
new Thread(
() =>
{
while (true)
{
Dispatcher.Invoke(
DispatcherPriority.Normal,
(ThreadStart)delegate()
{
transform.X += -1 + random.Next(3);
transform.Y += -1 + random.Next(3);
transform.X = transform.X.Clamp(0, 499);
transform.Y = transform.Y.Clamp(0, 499);
});
}
});
thread.Start();
Closed += (s, e) => thread.Abort();
}
}
}
My question is this. In cases like this where the standard WPF animation facility isn't being used, is the above approach using Thread and Dispatcher the recommended approach to take?
In general, I sometimes have states that need to be updated and rendered and the animation facility isn't a good fit. So I need a way to do the update and rendering in a separate thread. Just wondering if the above approach is the Right Thing.
Clemens in the comments above suggested using DispatcherTimer. Indeed, this does simplify the code quite a bit. Here's a version taking that approach:
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using System.Windows.Threading;
namespace WpfBrownianMotion
{
static class MiscUtils
{
public static double Clamp(this double n, int low, double high)
{
return Math.Min(Math.Max(n, low), high);
}
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Width = 500;
Height = 500;
var canvas = new Canvas();
Content = canvas;
var transform = new TranslateTransform(250, 250);
var circle = new Ellipse()
{
Width = 25,
Height = 25,
Fill = Brushes.PowderBlue,
RenderTransform = transform
};
canvas.Children.Add(circle);
var random = new Random();
var timer = new DispatcherTimer();
timer.Tick += (s, e) =>
{
transform.X += -1 + random.Next(3);
transform.Y += -1 + random.Next(3);
transform.X = transform.X.Clamp(0, 499);
transform.Y = transform.Y.Clamp(0, 499);
};
timer.Start();
}
}
}

Categories

Resources