PDA

Click to See Complete Forum and Search --> : Adding a WPF control in VB.NET Code


CJay
May 4th, 2009, 08:06 AM
Hi, as im new i guess i should start off with a brief introduction, i'm a 19 year old software development student and part time programmer.


I would like to know how i would add WPF control in vb.net code. The WPF control class is different to the visual basic one and therefore; The code to add a vb control does not work ( example below ).



Dim d As New Button
Me.Controls.Add(d)


Basically what would the equivalent code be for a wpf control.

Thanks in advance!

Pac_741
May 4th, 2009, 03:12 PM
In this code I'm adding a new control instance to a Grid.

Dim n As New Button
n.Width = 20
n.Height = 20
Grid1.Children.Add(n)

CJay
May 4th, 2009, 03:50 PM
Thanks , thats perfect !

Surprising how three separate ebooks had no mention of that code at all.

bflosabre91
May 4th, 2009, 03:50 PM
yeah, as Pac has shown, in WPF all containers have children now, instead of controls. So whether your using a grid like he has shown, or any type of panel or any other kind of container, you will use the Children property now, instead of the controls property.

Pac_741
May 5th, 2009, 03:02 PM
There's an exception though, there's no Children class on Me (Window), so you will have to use a panel or any container to add controls from code.

CJay
May 6th, 2009, 01:52 AM
There's an exception though, there's no Children class on Me (Window), so you will have to use a panel or any container to add controls from code.

Yeah i noticed that, now im delving deeper into wpf i'm of two minds whether its a decent improvement.

However i have a another related problem. Once i have created to control. Say i want to add it to a 4x4 grid, how would i accomplish that in vb code? I created to grid in vb code and can add the control to the first cell of the grid ( top left) but cant seem to work out how to position it anywhere else one the grid.

Also, anybody know of any good vb.net specific wpf ebooks / books? because of the nature of the program i'm making, a presentation that generates content at runtime, using lots of xaml is not viable. There seems to be very few ebooks / books covering the vb.net equivalent code to the xaml.
Any suggestiosn would help greatly :)

Thanks!

bflosabre91
May 6th, 2009, 07:50 AM
you set the grid row and columns on the controls a little wierd too. its like this, for example if u wanted to add a textbox called txt1.

Grid.SetRow(txt1,1)
Grid.SetColumn(txt1,1)

that will put it in the 2nd row, 2nd column as the column and row numbers start at 0. you can also set the Grid.SetRowSpan and Grid.SetColumnSpan like this.

chris128
May 6th, 2009, 10:24 AM
Also, anybody know of any good vb.net specific wpf ebooks / books? because of the nature of the program i'm making, a presentation that generates content at runtime, using lots of xaml is not viable. There seems to be very few ebooks / books covering the vb.net equivalent code to the xaml.
Any suggestiosn would help greatly :)

Thanks!

I mean it depends totally on what you are trying to make but just because you want dynamic content that certainly does not mean you have to create all your controls etc at runtime. I was surprised how flexible XAML is at creating dynamic interfaces once I started to get to grips with it. I'm not saying that in your situation you will definitely be able to do most of it in XAML (because I dont know exactly what your situation is) but maybe if you explain the bits your struggling with we can help?

CJay
May 7th, 2009, 02:48 PM
I mean it depends totally on what you are trying to make but just because you want dynamic content that certainly does not mean you have to create all your controls etc at runtime. I was surprised how flexible XAML is at creating dynamic interfaces once I started to get to grips with it. I'm not saying that in your situation you will definitely be able to do most of it in XAML (because I dont know exactly what your situation is) but maybe if you explain the bits your struggling with we can help?

Thanks I'll do my best to explain what i'm trying to do. Might be a bit of a lengthy explanation but better than a short one that just raises more questions!

The business i work for wants a system that displays different information on different screens around the company, specific to each department. The information will be updated on any computer by using a program with a secure login system. The program will allow new data fields and slides to be added and saved etc, this program then writes the data the user has entered to a SQL data base. The next program takes this information the user has entered and generates a presentation based on it.

In the long term the company hopes to develop the system to have 2 way interaction with the presentation and link into the businesses network to provide a more effective way of handling secure information.

So far i have two programs. One that allows a user to login, and enter preconfigured information to the section they have access to with there account. The second program is a slideshow /presentation in wpf that displays this information on a single screen in the canteen room. So far this works well!

The Presentation program is poorly coded, I made it to see how it could be done and within a short time scale, the first program is well made and ready for expansion!

The step I want to take now is to see how i can make the presentation program generate slides from a base class taking data from the sql database. Thats the reason for all these questions on this thread.

I have been considering how to accomplish this. My idea was to create a table in the sql database that stored layout information for each piece of code.

E.g.

Layout Table

ControlName----ControlType-----Control Position-----Control Formatting
__________________________________________________________________
GeneralInfo---------TextBlock----------Top left----------Content box
GeneralTitle----------TextBlock---------Top left---------SubTitle


That's Obliviously just a crude example. The problem I'm having how to store the position of the control. I was considering using a grid. But have been thinking about Using a canvas and XY co-ordinates.

Ideas?

Well Sorry to waffle on but i think that covers some more of it.

Thanks for the help, ill be sure to return the favour to the other members if i can :)

bflosabre91
May 7th, 2009, 03:09 PM
it seems like you are on the right track. I'm not sure how to do dynamic controls in XAML, and chris implied, but creating a grid in code and adding the controls would be how i would do this as well. I would fill a dataset with the controls and basically do a count of the controls. Based on that count, you would determine how many Columns or Rows you would need for the grid and then add the columns and rows to your grid. After that you would just create the controls and increment the Column Number or Row number which i showed u a few posts up, then the layout should be something to how you would like it.

if chris has a better way of doing this in XAML, i would love to hear how to do so. :)

chris128
May 12th, 2009, 07:29 AM
Hmmm well that does sound like a bit of a challenge :)
How about letting your users position their elements in the presentation (not sure how you are doing this currently) and then using System.Windows.Markup.XamlWriter.Save to save the XAML to the SQL database and then in your presentation program you just load that XAML into the necessary place.

I dunno though, maybe i've misunderstood exactly what your doing :)
How does the program know where each element should be (for it to store it in your SQL database like you showed), does the user just get a choice of like "top left", "top right" etc or can they drag/drop whatever they want around the screen?

Also, when you say "presentation" - are we talking about having things animating in and out of the screen or will they just be static slides?