m beginner in c# i got periodic table project from here


my prob is this is totally code based and hard to editable for me since m newbie .... windows form is empty but output comes in proper form ,.......... i want to drag that output form into form1.cs for easy handling. :( i want to add mouse over event click event new form with description

please try this project u will understand what m asking for... if its not possible please make me understand why ??

Recommended Answers

All 2 Replies

As for a beginner, I would suggest you to take an easier project to learn from. The given project has variety things, that may be too complex and too confusing for a newbie. Start from "Hello world" and then build up things to it bit by bit .. that would make your education easy and comfortable ;)

Like Antenka said, maybe this is too complex for you for now, because everything is created at runtime, which that's why you don't see anything in the design view, but if you really want to continue to play around with this code, I add some lines to meet your request, so you might be able to fix it at your needs.
Add this code:
1) In the CreatingElements() Methods

//After this line int size = 48;
//Create Panel To Hold Descriptions
FlowLayoutPanel pnl = new FlowLayoutPanel();
pnl.Location = new Point(x + (size * 2), y + size);
pnl.Size = new Size(size * 10, size * 2);
pnl.BackColor = System.Drawing.Color.LightGray;
pnl.BorderStyle = BorderStyle.FixedSingle;
pnl.Name = "PanelDescription";
//Create Labels 
foreach (var property in typeof(ElementData).GetProperties())
   var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), false)
   if (attribute != null)
      Label lbl = new Label();
      lbl.Name = property.Name;
//Add Panel To The Form

Then for the same method

//after this line: elebuttons[el.SequenceNumber - 1].Click += new EventHandler(ButtonElements_Click);
elebuttons[el.SequenceNumber - 1].Name = el.SequenceNumber.ToString(); //This is to better retrieve the element later
elebuttons[el.SequenceNumber - 1].MouseHover += new EventHandler(Button_MouseHover); //adding the mouse over event at runtime
elebuttons[el.SequenceNumber - 1].MouseLeave += new EventHandler(Button_MouseLeave); //adding the mouse leave event at runtime

Now lets create the Event Handler for the mouse over and leave

void Button_MouseLeave(object sender, EventArgs e)
   //Code when mouse leave
void Button_MouseHover(object sender, EventArgs e)
   //Creating an instance of the button that was hover
   Button btn = (Button)sender;
   //Creating an instance of the element that the button represent
   ElementData element = (from x in elementsList
         where x.SequenceNumber == Convert.ToInt32(btn.Name)
         select x).SingleOrDefault();
   //Creating an instance of the panel that you are going to add the details
   FlowLayoutPanel pnl =  (FlowLayoutPanel)ActiveForm.Controls.Find("PanelDescription",true)[0];
   //Going thru each property of the ElementData Class to find out which one have a DisplayName property
   foreach (var property in typeof(ElementData).GetProperties())
      var attribute = property.GetCustomAttributes(typeof(DisplayNameAttribute), false)
      //if that property has the attribute displayname then render that label
      if (attribute != null)
         //creating an instance of the previous label created that match the same property
         Label lbl = (Label)pnl.Controls.Find(property.Name, false)[0];
         lbl.Text = attribute.DisplayName + ": " + property.GetValue(element, null).ToString();

And this is the most important part, the ElementData Class looks like this

class ElementData
   public int SequenceNumber { get; set; }
   [DisplayName("Short Name")]
   public string ShortName { get; set; }
   [DisplayName("Full Name")]
   public string FullName { get; set; }
   [DisplayName("Atomic Mass")]
   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; }

so if you want to include one of the property of the ElementData, just add the attribute DisplayName to it.

This is just the way I did it demostrate how you can simulate the hover function and display some data. so play around, and I hope you learn something from it.

All the credit to Mitja Bonca nice work :)

commented: Clean and nice .. but I guess it's just adding more confusion for an OP *giggle* +7
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.