# .NET and More > WPF, WCF, WF >  Adding a WPF control in VB.NET Code

## CJay

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

In this code I'm adding a new control instance to a Grid.

Vb .Net Code:
Dim n As New Button
  n.Width = 20
  n.Height = 20
  Grid1.Children.Add(n)

----------


## CJay

Thanks , thats perfect !

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

----------


## bflosabre91

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

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

> 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  :Smilie: 

Thanks!

----------


## bflosabre91

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.


vb Code:
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

> 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

> 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  :Smilie:

----------


## bflosabre91

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.  :Smilie:

----------


## chris128

Hmmm well that does sound like a bit of a challenge  :Smilie: 
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  :Smilie: 
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?

----------


## RRickk

hi every one...please i need help...ive been able to show a new form from day one,,,but i cant seem to add controls to my form...sure adding controls to a wpf border or grid is easy,,,but how do i add controls onto a clean wpf form...my coding nor my mind do not work here,,,please help

dim NewForm as new Forms.Form
dim MyNewGrid as new grid
''''''''
NewForm.Controls.Add(MyNewGrid)

it does not work...it says my grid is not a valid control to be added to the wpf controls... lets be clear, the grid is the VERY FIRST control i add...but it doesnt want to... please help

----------


## szlamany

@RRickk - start a NEW THREAD - ask this question.

No reason to open a thread that has been closed since 2009!!

----------

