how to add new button by to grid by click on another button?
I am learning C# Silverlight. I need to add to learn how to add to grid new button after clicking to another button. Here is my code:
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel; using System.Runtime.InteropServices; namespace SilverlightApplication1 { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); Button btn = new Button(); btn.Width = 100; btn.Height = 25; btn.Content = "mytext"; rootCanvas.Children.Add(btn); btn.MouseLeftButtonDown += new MouseButtonEventHandler(btn_MouseLeftButtonDown); } void btn_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Button btn1 = new Button(); btn1.Width = 100; btn1.Height = 35; rootCanvas.Children.Add(btn1); } } }
The problem that btn1 do not appear on the GRID.
And second question. If I will declare the btn1 in another class, how I should call it from function in this class?
I tried next code, it's look like left mouse event do not work. And I can't understand why.
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using System.ComponentModel; using System.Runtime.InteropServices; namespace SilverlightApplication1 { public partial class MainPage : UserControl { Button btn1 = new Button(); Button btn2 = new Button(); public MainPage() { InitializeComponent(); btn1.Content = "hello"; btn1.Height = 30; btn1.Width = 70; btn1.MouseLeftButtonDown += new MouseButtonEventHandler(btn1_MouseLeftButtonDown); btn1.MouseEnter += new MouseEventHandler(btn1_MouseEnter); btn1.MouseRightButtonDown += new MouseButtonEventHandler(btn1_MouseRightButtonDown); btn1.MouseLeftButtonUp += new MouseButtonEventHandler(btn1_MouseLeftButtonUp); rootCanvas.Children.Add(btn1); } void btn1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e) { MessageBox.Show("Left Btn UP"); } void btn1_MouseRightButtonDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("Right Button Down"); } void btn1_MouseEnter(object sender, MouseEventArgs e) { } void btn1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { MessageBox.Show("Left Btn Down"); } } }
XAML:
<UserControl x:Class="SilverlightApplication1.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" x:Name="test" Width="1220" Height="960" > <Grid x:Name="rootCanvas" Width="640" Height="480" Background="Gray"> </Grid> </UserControl>
Answers & Comments...
No comments:
Post a Comment
Send us your comment related to the topic mentioned on the blog