Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > Mobile > Windows Phone Dev - สอนเขียน App บนโปรแกรม Windows Phone 7 , Windows Phone 8 > Dynamically Create Controls AddHandler (Event Handler) บน Windows Phone



Clound SSD Virtual Server

Dynamically Create Controls AddHandler (Event Handler) บน Windows Phone

Dynamically Create Controls AddHandler (Event Handler) บน Windows Phone บทความและตัวอย่างการสร้าง Controls แบบ Dynamic และการสร้าง Event Handler ให้กับ Controls ที่ถูกสร้างแบบ Dynamic ในขณะที่โปรแกรมกำลังทำงานอยู่ผ่าน Runtime

Dynamically Create Controls AddHandler (Event Handler)


XAML
    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>

        <!--ContentPanel - place additional content here-->
        <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">

        </StackPanel>
        
    </Grid>

XAML Design ของหน้าขอ Application ซึ่งเราจะสร้าง Controls มาไว้ในส่วนของ ContentPanel

VB.NET
    Private Sub MainPage_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded

        '*** Create TextBlock ***'
        Dim textBlock As New TextBlock
        textBlock.Name = "txt1"
        textBlock.Text = "Please Input Your Name ?"
        textBlock.TextAlignment = TextAlignment.Center
        ContentPanel.Children.Add(textBlock)

        '*** Create TextBox ***'
        textBox = New TextBox
        textBox.Name = "txt2"
        textBox.Height = "72"
        textBox.Width = "460"
        textBox.TextAlignment = TextAlignment.Center
        ContentPanel.Children.Add(textBox)

        '*** Create Button ***'
        Dim button As New Button
        button.Name = "btn1"
        button.Content = "Submit"
        button.Height = "72"
        button.Width = "160"
        'button.Margin = New Thickness(44, 63, 0, 0)
        AddHandler button.Click, AddressOf Me.buttonClick
        ContentPanel.Children.Add(button)

    End Sub

    Private Sub buttonClick(ByVal sender As Object, ByVal e As RoutedEventArgs)
        MessageBox.Show("Sawatdee Khun : " + textBox.Text.ToString())
    End Sub









C#
    private void MainPage_Loaded(object sender, System.Windows.RoutedEventArgs e) {
        // *** Create TextBlock ***'
        TextBlock textBlock = new TextBlock();
        textBlock.Name = "txt1";
        textBlock.Text = "Please Input Your Name ?";
        textBlock.TextAlignment = TextAlignment.Center;
        ContentPanel.Children.Add(textBlock);
        // *** Create TextBox ***'
        textBox = new TextBox();
        textBox.Name = "txt2";
        textBox.Height = "72";
        textBox.Width = "460";
        textBox.TextAlignment = TextAlignment.Center;
        ContentPanel.Children.Add(textBox);
        // *** Create Button ***'
        Button button = new Button();
        button.Name = "btn1";
        button.Content = "Submit";
        button.Height = "72";
        button.Width = "160";
        button.Click += new System.EventHandler(this.buttonClick);
        ContentPanel.Children.Add(button);
    }
    
    private void buttonClick(object sender, RoutedEventArgs e) {
        MessageBox.Show(("Sawatdee Khun : " + textBox.Text.ToString()));
    }


Dynamically Create Controls AddHandler (Event Handler)

หน้าจอ Design บน Visual Studio ซึ่งตอนนี้จะยังไม่แสดง Controls ใหม่ ที่ได้ถูกสร้างขึ้น

Screenshot

Dynamically Create Controls AddHandler (Event Handler)

ทดสอบรันผ่าน Emulator ซึ่งจะแสดง Controls ที่ได้ถูกสร้างแบบ Dynamic ผ่าน Runtime

Dynamically Create Controls AddHandler (Event Handler)

ทดสอบการสร้าง Event ที่ผ่าน Dynamic Runtime

เพิ่มเติม ในกรณีที่เป็น Dynamic Control และต้องการ FindControl สามารถใช้คำสั่งได้ง่าย ๆ เช่น
Dim ID As Integer = 123
Dim text As TextBlock = Me.FindName("txtblock" & ID)
text.Text = "My String"


   
Share


ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท


ลองใช้ค้นหาข้อมูล


   


Bookmark.   
       
  By : ThaiCreate.Com Team (บทความเป็นลิขสิทธิ์ของเว็บไทยครีเอทห้ามนำเผยแพร่ ณ เว็บไซต์อื่น ๆ)
  Score Rating :  
  Create/Update Date : 2012-09-02 21:33:54 / 2017-03-25 22:11:43
  Download : Download  Dynamically Create Controls AddHandler (Event Handler) บน Windows Phone
 Sponsored Links / Related

 
การสร้าง ApplicationBar (MenuItem) บน Windows Phone
Rating :

 
ตัวอย่างโปรแกรม Code Samples สำหรับ Windows Phone
Rating :

 
Windows Phone Posting Data to Web Server URL (Website)
Rating :

 
Windows Phone Show List Data from Web Server (Website)
Rating :

 
Windows Phone Search Data from Web Server
Rating :

 
Windows Phone Edit Update data to Web Server
Rating :

 
Windows Phone Delete data in Web Server
Rating :

 
Windows Phone and BackgroundWorker
Rating :


ThaiCreate.Com Forum


Comunity Forum Free Web Script
Jobs Freelance Free Uploads
Free Web Hosting Free Tools

สอน PHP ผ่าน Youtube ฟรี
สอน Android การเขียนโปรแกรม Android
สอน Windows Phone การเขียนโปรแกรม Windows Phone 7 และ 8
สอน iOS การเขียนโปรแกรม iPhone, iPad
สอน Java การเขียนโปรแกรม ภาษา Java
สอน Java GUI การเขียนโปรแกรม ภาษา Java GUI
สอน JSP การเขียนโปรแกรม ภาษา Java
สอน jQuery การเขียนโปรแกรม ภาษา jQuery
สอน .Net การเขียนโปรแกรม ภาษา .Net
Free Tutorial
สอน Google Maps Api
สอน Windows Service
สอน Entity Framework
สอน Android
สอน Java เขียน Java
Java GUI Swing
สอน JSP (Web App)
iOS (iPhone,iPad)
Windows Phone
Windows Azure
Windows Store
Laravel Framework
Yii PHP Framework
สอน jQuery
สอน jQuery กับ Ajax
สอน PHP OOP (Vdo)
Ajax Tutorials
SQL Tutorials
สอน SQL (Part 2)
JavaScript Tutorial
Javascript Tips
VBScript Tutorial
VBScript Validation
Microsoft Access
MySQL Tutorials
-- Stored Procedure
MariaDB Database
SQL Server Tutorial
SQL Server 2005
SQL Server 2008
SQL Server 2012
-- Stored Procedure
Oracle Database
-- Stored Procedure
SVN (Subversion)
แนวทางการทำ SEO
ปรับแต่งเว็บให้โหลดเร็ว


Hit Link
   







Load balance : Server 03
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่