Adding a WPF control in VB.NET Code-VBForums
Results 1 to 11 of 11

Thread: Adding a WPF control in VB.NET Code

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    7

    Adding a WPF control in VB.NET Code

    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 ).


    Code:
    Dim d As New Button
    Me.Controls.Add(d)
    Basically what would the equivalent code be for a wpf control.

    Thanks in advance!

  2. #2
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Adding a WPF control in VB.NET Code

    In this code I'm adding a new control instance to a Grid.
    Vb .Net Code:
    1. Dim n As New Button
    2.   n.Width = 20
    3.   n.Height = 20
    4.   Grid1.Children.Add(n)
    C# and WPF developer
    My Website:
    http://singlebits.com/

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    7

    Re: Adding a WPF control in VB.NET Code

    Thanks , thats perfect !

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

  4. #4
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: Adding a WPF control in VB.NET Code

    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.

  5. #5
    Hyperactive Member Pac_741's Avatar
    Join Date
    Jun 2007
    Location
    Mexico
    Posts
    298

    Re: Adding a WPF control in VB.NET Code

    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.
    C# and WPF developer
    My Website:
    http://singlebits.com/

  6. #6

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    7

    Re: Adding a WPF control in VB.NET Code

    Quote Originally Posted by Pac_741 View Post
    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!

  7. #7
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: Adding a WPF control in VB.NET Code

    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:
    1. Grid.SetRow(txt1,1)
    2. 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.

  8. #8
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Adding a WPF control in VB.NET Code

    Quote Originally Posted by CJay View Post
    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  9. #9

    Thread Starter
    New Member
    Join Date
    May 2009
    Posts
    7

    Re: Adding a WPF control in VB.NET Code

    Quote Originally Posted by chris128 View Post
    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

  10. #10
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: Adding a WPF control in VB.NET Code

    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.

  11. #11
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,595

    Re: Adding a WPF control in VB.NET Code

    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?
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.