Create Dynamic Web Control in c# desktop application - c#

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:

Related

The TextBoxes locations get modified before the repaint event is performed

I have a panel and on this panel, I create a number of TextBoxes, one under the other. The number of TextBoxes is greater than the vertical size of the panel can accommodate.
I have two problems:
1. The first TextBox appears at a large vertical distance from the top of the panel, even though its vertical coordinate is 5.
2. I want to automatically scroll to the bottom of the panel so that the last TextBoxes get visible, but I am unable to do that.
The first TextBoxes remain visible.
Here is the code I use:
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 TestPanelScroll
{
public partial class Form1 : Form
{
private const int _verticalSpacing = 1;
public Form1()
{
InitializeComponent();
this.StartPosition = FormStartPosition.CenterScreen;
this.BackColor = Color.BurlyWood;
this.ClientSize = new Size(800, 800);
this.SuspendLayout();
panelLeftForNodeCreationAndNodeMembersEditingMode = new Panel();
panelLeftForNodeCreationAndNodeMembersEditingMode.SuspendLayout();
panelLeftForNodeCreationAndNodeMembersEditingMode.Location = new Point(3, 56);
panelLeftForNodeCreationAndNodeMembersEditingMode.Size = new Size(461, 187);
panelLeftForNodeCreationAndNodeMembersEditingMode.Name = "panelLeftForNodeCreationAndNodeMembersEditingMode";
panelLeftForNodeCreationAndNodeMembersEditingMode.BorderStyle = BorderStyle.Fixed3D;
panelLeftForNodeCreationAndNodeMembersEditingMode.Visible = true;
panelLeftForNodeCreationAndNodeMembersEditingMode.CausesValidation = false;
panelLeftForNodeCreationAndNodeMembersEditingMode.TabStop = false;
panelLeftForNodeCreationAndNodeMembersEditingMode.TabIndex = 0;
panelLeftForNodeCreationAndNodeMembersEditingMode.BackColor = Color.LightGray;
panelLeftForNodeCreationAndNodeMembersEditingMode.SetAutoScrollMargin(0, 40);
panelLeftForNodeCreationAndNodeMembersEditingMode.HorizontalScroll.Maximum = 0;
panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScroll = false;
panelLeftForNodeCreationAndNodeMembersEditingMode.HorizontalScroll.Visible = false;
panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScroll = true;
panelLeftForNodeCreationAndNodeMembersEditingMode.Parent = this;
this.Controls.Add(panelLeftForNodeCreationAndNodeMembersEditingMode);
panelLeftForNodeCreationAndNodeMembersEditingMode.SendToBack();
panelLeftForNodeCreationAndNodeMembersEditingMode.ResumeLayout(false);
panelLeftForNodeCreationAndNodeMembersEditingMode.PerformLayout();
TextBox currentTextBox = null;
int xCoordinate = 30;
for (int i = 1; i <= 37; i++)
{
currentTextBox = new TextBox();
currentTextBox.Name = i.ToString();
currentTextBox.Size = new Size(85, 20);
currentTextBox.Tag = i.ToString();
currentTextBox.TabIndex = i - 1;
currentTextBox.TextAlign = HorizontalAlignment.Center;
currentTextBox.Text = currentTextBox.Name;
currentTextBox.Enabled = true;
currentTextBox.ReadOnly = true;
currentTextBox.CausesValidation = true;
currentTextBox.TabStop = true;
currentTextBox.BackColor = Color.DarkGray;
currentTextBox.Parent = panelLeftForNodeCreationAndNodeMembersEditingMode;
int yCoordinate = yCoordinate = 0 + (i - 1) * _verticalSpacing * 33 + _verticalSpacing * 5;
currentTextBox.Location = new Point(xCoordinate, yCoordinate);
textBox1.AppendText("TextBox " + currentTextBox.Name + " verticalCoordinate = " + yCoordinate.ToString() + Environment.NewLine);
int value = (currentTextBox.Location.Y + currentTextBox.Height) * 2 + _verticalSpacing * 40;
panelLeftForNodeCreationAndNodeMembersEditingMode.AutoScrollPosition = new Point(0, value);
panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Maximum = 300;
int minimum = panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Minimum;
int maximum = panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Maximum;
//panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Value = value;
//panelLeftForNodeCreationAndNodeMembersEditingMode.VerticalScroll.Value = value;
panelLeftForNodeCreationAndNodeMembersEditingMode.SetAutoScrollMargin(0, 40);
panelLeftForNodeCreationAndNodeMembersEditingMode.ScrollControlIntoView(currentTextBox);
panelLeftForNodeCreationAndNodeMembersEditingMode.PerformLayout();
}
this.ResumeLayout();
}
private Panel panelLeftForNodeCreationAndNodeMembersEditingMode;
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
I use Visual Studio 2010.
Any idea would be greatly appreciated.
Thank you in advance.
My intention is to understand what I am doing wrong and how to correct my code and not to find a workaround (a different way to program it).

Windows Forms C# Buttons in TableLayoutPanel Margins being overwritten

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

Overwrite label text in running program

I'm new to C#, I need to overwrite label on windows form. I'm generating number of labels as per value of n using loop.
The issue: if I change the value of lines[i] while running the program, the value does get changed but is not updated on windows form (previous value does not gets replaced by new one).
Can any one guide me how can I do that?
Also I'm refreshing the code every second using timer, which is also working fine
This is the part where I create the label and write the value in it, and it is in loop
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
label.Left = 10;
label.Top = (i + 1) * 25;
this.Controls.Add(label);
here is my complete code id anyone needs to check it:
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.IO;
namespace Admin
{
public partial class Form1 : Form
{
string pathuser = #"//192.168.2.10/Shared-Public/Users.txt";
int check = 0;
public static string usernam;
string[] lines = new String[500];
string[] lines2 = new String[500];
public Form1()
{
InitializeComponent();
}
private void timer_Tick(object sender, EventArgs e)
{
tc = tc + 1;
Console.WriteLine(tc);
int c = 2;
int u = 0;
int us = 0; //for user pass
int z = 0; // for reading values from file
using (StreamReader sr2 = new StreamReader(pathuser))
{
string line;
while ((line = sr2.ReadLine()) != null)
{
lines[us] = line;
us++;
}
}
for (int i = 0; i < us; i++)
{
//Create label
Label label = new Label();
label.Text = String.Format("{0}", lines[i]);
//Position label on screen
label.Left = 10;
label.Top = (i + 1) * 25;
Label label2 = new Label();
label2.Left = 120;
label2.Top = (i + 1) * 25;
string line2;
string path = "//192.168.2.10/Shared-Public/" + lines[i] + DateTime.Now.ToString(" MM-dd-yyyy") + ".txt";
if (!File.Exists(path))
{
lines2[z] = null;
}
else
{
using (StreamReader sr3 = new StreamReader(path))
{
while ((line2 = sr3.ReadLine()) != null)
{
lines2[z] = line2;
z++;
}
}
label2.Text = String.Format("{0}", lines2[0]);
u = z;
z = 0;
}
PictureBox picbox = new PictureBox();
picbox.Location = new Point(240, 25 + (i*25));
picbox.Size = new Size(15, 15);
if (u%2==0)
picbox.BackColor = Color.Green;
else
picbox.BackColor = Color.Red;
u = 0;
this.Controls.Add(label);
this.Controls.Add(label2);
this.Controls.Add(picbox);
}
}
int tc = 0;
private void Form1_Load(object sender, EventArgs e)
{
Timer timer = new Timer();
timer.Interval = (1000);
timer.Tick += new EventHandler(timer_Tick);
timer.Start();
}
}
}
Give your label's Name a Unique id and Search for it and replace the value:
Label label = new Label();
var someid = new Guid().ToString();
label.Name = someid;
label.Text ="aaaa";
label.Left = 10;
this.Controls.Add(label);
foreach(Control item in this.Controls)
{
if (item.Name == someid)
item.Text = "bbb";
}

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)}
}
}

Categories

Resources