Pure.Krome
Pure.Krome

Reputation: 87087

Is it possible to programatically add some controls to a WinForm?

i have a pretty simple rectangluar WinForm that uses timers to check the end content of a number of files. Works fine.

Now, the list of files to check is dynamic. Could be 3. could be 30. It dependant upon some value in the database, which i check regularly. That's also fine.

What I wish to do is visual this, on my winform. For each file, have a red circle. when the file is being 'checked', show that circle as green. once finished, red again.

  1. How can i programatically display a row (or rows) of this circle? I know the width of the form, if that helps. Remember, i have no idea how many to display if i drag/drop them onto the designer. I need to be able to do this dynamically :)
  2. How can i switch/flip this circle from red to green and later green to red?
  3. What is the circle? a custom image (eg. png) i make in MSPaint and add it to the project as an embedded resource?

Cheers!

Upvotes: 2

Views: 1788

Answers (5)

Mesh
Mesh

Reputation: 6472

Yes just create the control (new) and add it to the form. see here link for and example or two.

The rest of you question is pretty open - there are many ways to do what you want.

  • dynamically draw a circle( via GDI+) for each item( in fact is each item really a control does it need to accept user input)
  • create a control for each item
  • usercontrol that swaps bitmaps
  • Use a progress bar control instead
  • etc

Upvotes: 1

Christian Hayter
Christian Hayter

Reputation: 31071

  1. Create each control object, set its properties, then add it to the Controls collection of the parent container (e.g. the form). You could either (a) manually work out the coordinates of each new control based on the last one added, or (b) put them inside a FlowLayoutPanel.
  2. You should be able to just change the relevant property value of the control as and when required (depending on how you choose to implement the circle).
  3. You could either use an image resource as you suggest, or draw it yourself using the Graphics object.

Upvotes: 9

Neil N
Neil N

Reputation: 25278

In cases like this I create a "model" control, that has all the properties of the controls you want to add. For example the red dot, with its size and other properties already set. Then set that either out of view or ake it invisible. Then when you need to create your controls dynamically, clone that model control, and just set its location, (and make it visible if need be), then add it to the forms control collection

Upvotes: 1

kgiannakakis
kgiannakakis

Reputation: 104196

Implement a custom control and do something like this:

SuspendLayout();

MyCircleControl circle = new MyCircleControl ();

// Set properties
circle.Location = new Point(0,0);
circle.Color = Color.Red;

Controls.Add(circle );

ResumeLayout(false);

This is actually what the designer is doing. Have a look at the Designer.cs file.

Upvotes: 1

Totty
Totty

Reputation: 916

Since you say that the list of files is held in a database anyway, I would add a datagridview to the form in design mode. This dgv would use the table with the files as its datasource, thus creating rows dynamically. I would also either add a column to the dgv or the table itself to hold a boolean/bit for whether the file is being read. This would then show as a column in the dgv. From there you would just get the dgv row for the file you are reading when it is being read, and change the image for the boolean column to be a green or red circle that you create in your favorite image editor.

Upvotes: 0

Related Questions