I'm just learning how to make universal apps for windows 10. Most of the tutorials show you how to work with XAML and how to assign functions to elements when you click on them but I couldn't find a way to make a new control appear when I click a button.
I'm making a note taking application. I've designed most of the stuff that I need. Now I want whenever I click a button to create a new textblock where the user can write their note.
//Create a new note when clicking the add button
private void newNoteBtn_Click(object sender, RoutedEventArgs e)
{
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
}
This is the code that runs when the button is clicked. When I run the application nothing happens. I think I have to put the new textbox in some kind of Grid or something.
Most of the tutorials are really old or mention windows forms and use some sort of this.Controls.Add(newNote); but Visual studio doesn't give me the Controls.Add option. I've also created a <Grid x:Name="notes"></Grid> which I thought I could use as a placeholder for the notes that are being created but I can't access the Grid element through the code.
Container Controls like Grid have Children property so you should use Childern like this:
TextBox newNote = new TextBox();
newNote.Text = "Enter your text";
notes.Childern.Add(newNote);
When defining
<Grid x:Name="notes"></Grid>
in XAML on the page, you be able to use notes as the identifier to access this Grid from the page's code behind:
notes.Children.Add(newNote);
Related
My Xamarin app creates buttons in code. I can change the color of a button I click, as the button is referenced in its Clicked handler's sender argument, but I want to change the color of buttons that I didn't click. The problem is, how do I find the buttons I want to change?
I thought about using FindByName, but Name doesn't appear to be an attribute.
One way I can think of: loop through all of the buttons until I find the one with the StyleId of the desired button. Is there an easier way than that?
when you create a button in code you need to keep a reference to it, like you would with any C# object you want to reference later
Button MyButton = new Button { ... };
MyButton.BackgroundColor = Color.Purple;
if you need to access it from multiple places in your code, you should declare it as a class level variable to that it has scope throughout your class
If you want to find the button created in code behind without XAML, like #Jason said, use the variable name of the Button you created or set it directly.
Button button = new Button { ClassId = "button1", BackgroundColor = Color.Red };
If you want to access a view with FindByName, you need to create the button with x:Name in XAML.
There is an XAML example:
<Button x:Name="button1" ></Button>
And in your code behind you could do something like:
button1.BackgroundColor = Color.Red;
If you need to find a view in xaml for some reason, do this:
var button = this.FindByName<Button>("button1");
button.BackgroundColor = Color.Red;
I am new in WPF and want to create WPF application like cookbook. I already done this and app work correctly. But I make it in this way:
First screen show buttons, which open new windows to do something. As a result i have 14 different windows. It is ok, but now i want to make it in other way.
I am trying to make one window, which will be showed at start, and change content. I divided window on two grids. First is static and is placed on bottom. It contains buttons, which represents functionality of the program. Second one will be dynamic. There i want to show content of every window. So i want to change content of this panel instead of creating new windows.
I tried to make *.cs files which will create controls in code-behind, functions and data. But my idea is not succesful and i do not know how to do this.
At all, I want to create app, which will work like this:
- if you click button "Add receip" then app will show controls to add name, ingredients and save it at the end.
- if you clik "Show receip" previous content will be replaced by list of ingredients
and etc.
I hope you will understand me.
You can create a Frame instead of second grid. Frame allows you to show pages, and not in seperate windows, in Frame itself. You can navigate the frame into the page like
mainFrame.Source = new Uri("Page1.xaml",UriKind.Relative);
This changes the frame to your page. You can change the source again, if you wanna change the page again.
Note: You can add tags to your buttons like "showReceip" and you can make just one buttonclick event for your buttons. Code will look like this.
mainFrame.Source = new Uri((sender as Button).Tag.ToString() + ".xaml",UriKind.Relative);
That takes the tag of your clicked button, add the string ".xaml" on it and take it on the source part. So, if your tag is "Page1", Source will look like "Page1.xaml" as my solution.
Appreciate the try, I hope you are looking for WPF user controls instead for separate windows. User controls are similar to windows you can create the UI and functionalities in the user control. I would like to recommend you to design the main window like the following:
<Grid>
<Canvas Name="canFunctionalButtons">
<!--Define the buttons inside this canvas
And allocate proper place for this in the UI
-->
</Canvas>
<Canvas Name="canControlContainer">
<!--This is to display the user control
Which can be changed dynamically according to the Button's click
-->
</Canvas>
</Grid>
Then you have to add click event for those buttons, which will add specific user control to the canControlContainer canvas. An example for adding an user control to this canvas is as follows, Let btnAddSomething be a button and btnAddSomething_Click be its click event then you can do something like:
private void btnAddSomething_Click(object sender, RoutedEventArgs e)
{
canControlContainer.Children.Clear(); // will remove previous contols from this canvas
// UC_AddSomething be the user control that you wanted to add here
canControlContainer.Children.Add(new UC_AddSomething());
}
I would like to improve my application's interface. Is it possible to add something like this(refer to image below)?:
When you click the name or move the cursor to the name, a pop up box will appear with user's main info. If possible, can you teach me the appropriate tools to use. I don't mind if it is basic tools, so i can make a little similar to the image.
BTW, i'm using visual studio 2013.
Here is an (absolutely minimal) example of a pop-up Panel that takes care of the showing and hiding:
Panel popPanel = new Panel();
private void linkLabel1_MouseEnter(object sender, EventArgs e)
{
popPanel.Parent = linkLabel1.Parent;
popPanel.Location = new Point(linkLabel1.Left - 20, linkLabel1.Top + 10);
//popPanel.displayData(someDataClassFromSender);
popPanel.Show();
popPanel.MouseLeave += (ss, ee) => { popPanel.Hide(); };
}
For the layout and display of the info on the Panel (or some other control) you should create a class PopUpPanel and give it a method to load the data it shall display..
If it is a web application go check out http://bootsnipp.com/snippets/featured/fancy-navbar-login-sign-in-form
Just change the event from click to hover.
And get youserlf a copy of bootstrap http://getbootstrap.com/
Issue
I have a simple ui, where user enters a value click on the "ok" button, the SubmitDateHandler click handler should then insert a label to the form with the value from the textbox. Then when this is repeated again, Add another label underneath the existing label. how to dynamically add multiple labels in a new line?
my code, at the moment is able to add one label, but not the second one.
private void SubmitDatebtn_Click(object sender, EventArgs e)
{
Label dateLabel = new Label();
dateLabel.Text = this.Controls.Find("Datetxt", true)[0].Text + Environment.NewLine;
this.LatestScoresPanel.Controls.Add(dateLabel);
}
Make sure to use FlowLayOutPanel, you can specify either veritcal or horizontal layout with it. Add the label to that like:
this.flowLayOutPanel.Controls.Add(dataLabel);
Make sure to specify FlowDirection either in code or design time.
flowLayoutPanel.FlowDirection = FlowDirection.TopDown;
I have page with some control like picture. i want to add these control dynamically and when user click add those control add dynamically. how can i implement this? i want to use template control or something like this.
Why don't you take "sample label, persian translate label, textboxes and add voice button" and put them in a usercontrol (Let's give it the name MyUserControl)
then instead of adding that user control directly on the grid of your window, add a stack panel (lets name it MyStackPanel), and add that user control into the stack panel (stack panel must have the orientation set to vertical)
Now when the user clicks on the "Plus" button your code will be:
MyStackPanel.Children.Add(new MyUserControl() { Margin = new Thickness(0, 5, 0, 0) });
This will give you the same controls you asked for with a margin top = 5.
You will only write code once for the user control, and all the added controls (after clicking on the plus button) will have the same code.
Edit:
If you want to delete MyUserControl when clicking the delete button add the following code for for the StackPanel (MyStackPanel) on the main window (look for ButtonBase.Click):
<StackPanel Height="236" HorizontalAlignment="Left" ButtonBase.Click="stackPanel1_Click" Name="stackPanel1" VerticalAlignment="Top" Width="491">
and for the event of the StackPanel:
private void stackPanel1_Click(object sender, RoutedEventArgs e)
{
if (((UserControl1)e.Source).Tag.ToString() == "1")
{
stackPanel1.Children.Remove(((UserControl1)e.Source));
}
else
{
MessageBox.Show("Another button was clicked");
}
}
Now the delete button on the user control must have the following line of code:
this.Tag = 1;
According to me using Implicit Data Templates is much easier then anything else User Control also a Good Choice but Data Templates OR Implicit Data Templates are much better.
Take a Look Data Templates