Is it possible for you to do this in ASP.NET?
thines01
Postaholic
2,424 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
I would create a class that holds each element and the information that you need about that element. Then I'd create a class that knows how to display that information and generate a tooltip on mouseover. Then I'd put them on a form :)
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
If you know something about WPF you could design a button with label for atom symbol, atomic number etc.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
__avd
Posting Genius (adatapost)
8,648 posts since Oct 2008
Reputation Points: 2,136
Solved Threads: 1,241
Have you ever seen a picture of the noble gas elements like Helium, Neon, Argon, Krypton, Xenon and Radon? they are as visible as Nitrogen and Oxygen etc. It is of course a noble (no pun intended) cause to help out your friend, but show us some of your code a we will try to help you out. :)
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
I would do as Momerat h said. Create a class of one Element, and then create a List which will hold the list of all elements. Class will hold data (properties) specific for each element (Name, Shortname, Number, (and position maybe, because the periodic table is not simetric - for example H is on top left, He is on top right)).
Next, to create a physical table, I would personally choose or labels or buttons. Create one by one, position it (based on the Position property from the element list and place it on the form.
If you need any help, let me know.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
I was bored last night so I created a periodic table form control. Maybe I'll post the code sometime :)
Momerath
Nearly a Senior Poster
3,384 posts since Aug 2010
Reputation Points: 1,232
Solved Threads: 558
@Momerath: Looking foreward for the event! Your code snippets are always great and I learn alot from them! :)
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
I was bored last night so I created a periodic table form control. Maybe I'll post the code sometime :)
Damn... I forgot to make it. I promised, but completely forgot it.
Will do my version. And looking forward to see yours Momerath.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
You all seem to have far too much spare time!
I look forward to all solutions as they will undoubtedly demonstrate some new technique that I can admire (and copy;)).
nick.crane
Nearly a Posting Virtuoso
1,230 posts since Feb 2010
Reputation Points: 375
Solved Threads: 187
:) too much time? hmm, this would be a nice debate to talk about...
No, not too much time, just didnt do anything like it before. But to create a decent Periodical table, its not that simple task. I was thinking of creating a file (*.txt) which will hold of the data, and then just reading it to create the table.
If you want to create the table just by the code, there will be too much of work and too much of the code, because there is nothing "simetrical" about this table.
From the beginning there is a bit more work to create the file (to insert all the data), but when this is done, its only a few minutes of work with the code to make it work.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
This is indeed not a so easy job, consider for example this
To display the table, I would consider a matrix of ints, stored as a resource in the program. I would fill it with the atomic numbers or zero for places in the table that are "empty", like the "place" right of Hydrogen and above Beryllium. To display the periodic table I would for-loop through my matrix and with the help of the atomic number I can find the info I need out of a List with elementinfo.
Or is all this a bad idea?
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Just did one hell of a job. Did the whole text file with all required data for 118 elemetns.
Now lets create an application which will draw the periodical table :)
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
Done it!
Here is the code, jsut copy and paste it into a brand new project (attention, change project name, so you wont copy complete code of mine to yours created project!!)
You dont need to put anything on the form - keep it empty.
Code:
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 Sep05_PeriodicalTable //CHANGE TO YOUR NAMESPACE!!!
{
public partial class Form1 : Form
{
List<ElementData> elementsList;
List<LegendData> legendList;
Button[] elebuttons;
Button[] legButtons;
public Form1()
{
InitializeComponent();
//path
string fileName = "PeriodicalTable.txt";
string path = Environment.CurrentDirectory;
path = Path.Combine(path, fileName);
//creating new list<T>:
elementsList = new List<ElementData>();
legendList = new List<LegendData>();
//reading file line by line and adding data into list<T>:
string type = null;
foreach (string data in ReadingData(path))
{
if (data == "ELEMENT_LIST")
{
type = "elements";
continue;
}
else if (data == "LEGEND")
{
type = "legend";
continue;
}
if (type == "elements")
SeperateElementData(data);
else if (type == "legend")
SeperateLegendData(data);
}
//Creating phisical periodical table from the data:
CreatingElements();
//Creating legends:
CreatingLegends();
}
private void CreatingElements()
{
elebuttons = new Button[elementsList.Count];
//starting position for 1st upper left button:
int x = 20;
int y = 60;
//and size of buttons:
int size = 48;
foreach (ElementData el in elementsList)
{
if (el.LocationX > 0)
x = 20 + size * el.LocationX;
else
x = 20;
if (el.LocationY > 0)
y = 60 + size * el.LocationY;
elebuttons[el.SequenceNumber - 1] = new Button();
string btnText = String.Format("{0}\r\n{1}\r\n{2}\r\n{3}", el.SequenceNumber, el.ShortName, el.FullName, el.AtomicMass);
elebuttons[el.SequenceNumber - 1].Location = new Point(x, y);
elebuttons[el.SequenceNumber - 1].Size = new Size(size, size);
elebuttons[el.SequenceNumber - 1].Font = new Font("Arial", 6, FontStyle.Regular);
elebuttons[el.SequenceNumber - 1].Text = btnText;
elebuttons[el.SequenceNumber - 1].Tag = el.FullName;
elebuttons[el.SequenceNumber - 1].BackColor = GetButtonColor(el.Color);
elebuttons[el.SequenceNumber - 1].Click += new EventHandler(ButtonElements_Click);
this.Controls.Add(elebuttons[el.SequenceNumber - 1]);
}
}
private void CreatingLegends()
{
legButtons = new Button[legendList.Count];
//starting position for 1st upper left button:
int x = 164;
int y = 60;
decimal size = 48;
foreach (LegendData le in legendList)
{
x = Convert.ToInt32(20 + size * le.LocationX);
if (le.LocationY > 0)
y = Convert.ToInt32(60 + size * le.LocationY);
else
y = 60;
legButtons[le.Number - 1] = new Button();
legButtons[le.Number - 1].Text = le.GroupName;
legButtons[le.Number - 1].Size = new Size(120, 25);
legButtons[le.Number - 1].Location = new Point(x, y);
legButtons[le.Number - 1].Font = new Font("Arial", 8, FontStyle.Regular);
legButtons[le.Number - 1].BackColor = GetButtonColor(le.Number);
legButtons[le.Number - 1].Tag = le.GroupName;
legButtons[le.Number - 1].Click += new EventHandler(ButtonLegends_Click);
this.Controls.Add(legButtons[le.Number - 1]);
}
}
private void ButtonElements_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
MessageBox.Show(String.Format("Element {0} was selected.", btn.Tag));
}
private void ButtonLegends_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
}
private void SeperateElementData(string data)
{
string[] array = data.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
//removing tabs
for (int i = 0; i < array.Length; i++)
if (array[i].Contains("\t"))
array[i] = array[i].Replace("\t", "");
ElementData el = new ElementData();
el.SequenceNumber = int.Parse(array[0]);
el.ShortName = array[1];
el.FullName = array[2];
el.AtomicMass = Convert.ToDecimal(array[3]);
el.Brackets = (array[4] == "1") ? true : false;
el.Color = int.Parse(array[5]);
decimal[] xy = SeperateLocation(array[6]);
el.LocationX = (int)xy[0];
el.LocationY = (int)xy[1];
el.Details = array[7];
//addind data of element to list<T>:
elementsList.Add(el);
}
private void SeperateLegendData(string data)
{
string[] array = data.Split(new string[] { "-" }, StringSplitOptions.RemoveEmptyEntries);
//removing tabs
for (int i = 0; i < array.Length; i++)
if (array[i].Contains("\t"))
array[i] = array[i].Replace("\t", "");
LegendData le = new LegendData();
le.Number = int.Parse(array[0]);
le.GroupName = array[1];
decimal[] xy = SeperateLocation(array[2]);
le.LocationX = xy[0];
le.LocationY = xy[1];
legendList.Add(le);
}
private decimal[] SeperateLocation(string str)
{
string[] arr = str.Split('x');
decimal x = decimal.Parse(arr[0]);
decimal y = decimal.Parse(arr[1]);
return new decimal[] { x, y };
}
private Color GetButtonColor(int value)
{
Color backColor = new Color();
switch (value)
{
case 1:
backColor = Color.LimeGreen;
break;
case 2:
backColor = Color.Blue;
break;
case 3:
backColor = Color.Orange;
break;
case 4:
backColor = Color.Yellow;
break;
case 5:
backColor = Color.ForestGreen;
break;
case 6:
backColor = Color.Aquamarine;
break;
case 7:
backColor = Color.GreenYellow;
break;
case 8:
backColor = Color.Red;
break;
case 9:
backColor = Color.Pink;
break;
case 10:
backColor = Color.Magenta;
break;
}
return backColor;
}
private IEnumerable<string> ReadingData(string path)
{
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (!line.StartsWith("##") && line != "")
yield return line;
}
}
}
}
class ElementData
{
public int SequenceNumber { get; set; }
public string ShortName { get; set; }
public string FullName { get; set; }
public decimal AtomicMass { get; set; }
public bool Brackets { get; set; }
public int Color { get; set; }
public int LocationX { get; set; }
public int LocationY { get; set; }
public string Details { get; set; }
}
class LegendData
{
public int Number { get; set; }
public string GroupName { get; set; }
public decimal LocationX { get; set; }
public decimal LocationY { get; set; }
}
}
NEXT:
<em>Download the file</em> which is needed that application will work. You get it <a href="http://www.2shared.com/document/lW_-UeAl/PeriodicalTable.html">here</a> .
<em>Copy the file into the debug folder of current project</em> (ie: ...\MyProject\MyProject\bin\Debug\).
NOTE: If you will not put it there, you have to change the path (its on top of the Form1 class.
There is still work to do, but mostly it works. Details are not included, but it can be.
Hope you like it.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474
Great job Mitja!
Are the buttons in the middle meant to be labels or have you further plans with them?
ONe advise: if you use color(colour?) make it more subtle, pastel, I don't know the exact word. It is hard to read what is printed in a harsh dark blue in the noble gasses column.
ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
Hi, thx mate.
They can be labels, even they are now buttons, and I had in mind do something with them, like coloring just the elements that belong to selected group (all other will be colors differently but the same), so the difference will be visible.
Anyway, there is still a lot of work do to, to implement all the details for each element, but this is mostly in writing text. Then when this is done, only a new form is needed what will show selected element`s details, by reading the appropriate file (or all in one, the appropriate line of text).
And thx for the adivce. I know what you mean (not to use so colorful colors), its hard to read when so-red (and so-green, ...) colors are used. I will consider your advice.
Maybe only the buttons location can be done a bit better, I complicated a bit to much, and this is the only code I dont like... will repair it.
Mitja Bonca
Nearly a Posting Maven
2,485 posts since May 2009
Reputation Points: 641
Solved Threads: 474