Question to discuss: Placing a few pictures inside a groupbox (Similar to plotting graphs)

Hi Experts,

I'm supposed to do a program similar to plotting out a graph. Given the x, y coordinates inside a string (e.g. The string looks somehoe like "1 2 3 4 5 6" and I have to use 2 different strings of datas (for x, y) to place different points inside a groupbox. Can anyone provide me with any ideas how I can accomplish this?

Guan Zhao

[374 byte] By [guanzhao] at [2008-1-10]
# 1
Bring Up My Post
guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 2

Hi, Guan

You can subscribe the Paint event of the groupbox and paint these images there.

Code Snippet

private void groupBox1_Paint(object sender, PaintEventArgs e)
{
Point[] points = new Point[10]; //save the positions to this array.

for (int i = 0; i < points.Length; i++)
{
points[i].X = points[i].Y = i * 10;
}

Image img = Properties.Resources.Image1;


for (int i = 0; i < points.Length; i++)
{
e.Graphics.DrawImage(img, new Point(i * 10, i * 10));
}
}

Using multiple pictureboxes is anther way.

Code Snippet

this.SuspendLayout();

Point[] points = new Point[10];

for (int i = 0; i < points.Length; i++)
{
points[i].X = points[i].Y = i * 10;
}

PictureBox[] pics = new PictureBox[points.Length];
for (int i = 0; i < pics.Length; i++)
{
pics[i] = new PictureBox();
pics[i].Parent = groupBox1;
pics[i].Size = new Size(8, 8);
pics[i].Location = points[i];

pics[i].Image = ........;
pics[i].BackColor = Color.Green;
}

this.ResumeLayout();

Best Regards

Chunsheng Tang

ChunShengTang-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 3
Your question is difficult to interpret. Taking a guess:

string xcoor = "1 2 3 4 5 6";
string ycoor = "0 4 2 6 8 6";
string[] xpnts = xcoor.Split(' ');
string[] ypnts = ycoor.Split(' ');
List<Point> points = new List<Point>();
for (int ix = 0; ix < xpnts.Length; ++ix) {
Point p = new Point(int.Parse(xpnts[ix]), int.Parse(ypnts[ix]));
points.Add(p);
}
// Now plot <points>
//...

nobugz at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 4

Hi nobugz,

Sorry to confuse you. It is also hard for me to describe the situation to you people clearly. I will try to elaborate abit more. Meanwhile, I shall test out these programs you good people provided me.

Thanks

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 5

Have I misunderstood you? Didn't you mean to paint an array of pictures inside a groupbox? The code snippets should be able to show you the idea.
ChunShengTang-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 6

Hi ChunSheng,

Yes, the idea is to create a plot of points inside a groupbox. But I have a few questions to ask you before I can start trying out your codes.

A groupbox's coordinates works on the inverse cartesian plane, which means coordinates (0, 0) is at the top left corner right? Is it possible to change the (0, 0) position to the bottom left corner like how we used to plot using a cartesian plane?

Secondly,

graphic.FillRectangle(redBrush, x, y, 10, 10);

For the above rectangle plot, can x, and y be a float value (10.13, 10.99)? I've tried to use float value but I got a build error.

Can you help me with this?

Thanks

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 7

Hi, Guan

This will change the coordinate to the one commonly used in mathematics:

Code Snippet

//Changes the origin to left bottom
e.Graphics.TranslateTransform(0, groupBox1.Height);
//Flip by x axis
e.Graphics.ScaleTransform(1, -1);


Use this for your second question:
Code Snippet

e.Graphics.FillRectangle(Brushes.Blue, 10.1F, 10.1F, 100, 100);


The x or y is float values so don't forget adding F at the end.

Best Regards
Chunsheng Tang
ChunShengTang-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 8

Hi ChunSheng,

Your last post was really a life saviour! Thanks a lot!

I will try to post up my question regarding the plotting as soon as possible.

Once again, thank you.

Warmest Regards

Guan Zhao

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 9
Code Snippet

e.Graphics.FillRectangle(Brushes.Blue, 10.1F, 10.1F, 100, 100);

What if the values I want to pass in is stored in a declared variable (e.g. x, y)? I can't possibly put a F behind right?

Thanks

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 10

Hi,

The varible should be of float type or others that can be implicitly converted to float type such as int.

Code Snippet

float x = 10.1F;
int y = 10;
e.Graphics.FillRectangle(brush, x, y, 100,100);


ChunShengTang-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 11

Hi ChunSheng,

Here is where the complication starts. The float value I'm supposed to retrieve comes from an external data source, which means I'm unable to put a "F" behind every data. The external datais then stored in a variable 'x' and passed into the graphics's argument.

Is there any other way to overcome this problem?

Thanks

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 12

What is the external data source? If it is of string type, use this to get the float value:
Code Snippet

float x = float.Parse("100.1"); //Or use float.TryParse(...)


If it is of double type, you can explicitly convert it to float:
Code Snippet

float x = (float) 100.1;


There may be data loss when converting double to float depending on the range of the values and whether you need to scale them.
ChunShengTang-MSFT at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 13

Hi ChunSheng,

The external data source is Microsoft Excel. I've solved the problem of getting the data from Excel, but haven't been able to solve this.

By the way, the data in the cells are float values (e.e 112.1264434).

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...
# 14

Hi ChunSheng,

The external data source is Microsoft Excel. I've solved the problem of getting the data from Excel, but haven't been able to solve this.

By the way, the data in the cells are float values (e.e 112.1264434).

Linking up Excel and Excel automation

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2108375&SiteID=1

ADO.NET

http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=2134656&SiteID=1

Currently I'm trying out ADO.NET codes. But should you require any help to understand my plight, you can refer to my threads as of above.

Thanks

guanzhao at 2007-10-3 > top of Msdn Tech,Visual Studio Express Editions,Visual C# 2005 Express Edition...