Xamarin creating elements dynamically - c#

I'm working on a project where I have to create an search menu o find specific files, but i need do to create a dinamic search menu because every section has diferents fields of search, and my question is: How can I create EditTexts,Spinners,etc through code on the activity?

//create "reference to your layout"
LinearLayout yourFormLayout = FindViewById<LinearLayout(Resource.Id.Linear_MS);
//create container for parameters
var parameters = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MatchParent, ViewGroup.LayoutParams.WrapContent);
//add some parameters
param.SetMargins(20, 20, 20, 10);
//create new element
Button button = new Button(this);
button.Text = "click me";
button.SetBackgroundColor(Android.Graphics.Color.Black);
button.SetTextColor(Android.Graphics.Color.White);
button.LayoutParameters = parameters;
//add some events to your element
button.Click += (sender, e) => DoStuff();
//Add the button
yourFormLayout.AddView(button);
AddView adds element as a child to nearest parent, so if you want some more sofisticated location you'll have to read about for example 'GetDeclaredField'

Related

Add Xamarin Forms BottomBarPage OnClick() listener

I am using the ThriveGmbH.BottomNavigationBar.XF Nuget package to add a bottom tab bar to my application
BottomBarPage bottomBar = new BottomBarPage
{
};
var tab1 = new MainPage();
var tab2 = new ReceivePage(null);
var tab3 = new SendPage(false);
var tab4 = new SendPage(false);
var tab5 = new InfoPage(null);
bottomBar.Children.Add(tab1);
bottomBar.Children.Add(tab2);
bottomBar.Children.Add(tab3);
bottomBar.Children.Add(tab4);
bottomBar.Children.Add(tab5);
How do I add a listener to this BottomBarPage which checks which of the tabs is currently selected, so that I can add the code below to this listener.
if (bottomBar.SelectedItem == bottomBar.Children[3])
{
//do something
}
Use Android.Support.Design.Widget.TabLayout to create a tabLayout object and create Tab Item elements nested inside a Tab Layout element in your XML.
You can use a TabSelected event on this object like so:
tabLayout.TabSelected += OnTabSelected
Then you can write your OnTabSelected code.

Navigating Dynamically created Pages

I'm having a little issue navigating to dynamically created pages (with C# & XAML (visual studio 2015 edition).
I can create the pages (I assume), but when I navigate to them it gives me the original page.
I guess it might be to do with the GetType of test/test2 which is pointing to the Dynamic_Page_1 page (this page has nothing on it), but how do I go about forcing it to use the new instance which I'm modifying?
// Dynamic Page One
Dynamic_Page_1 test = new Dynamic_Page_1();
Button b = new Button { Content = "DYNAMIC TEST ONE", FontSize = 22, FontWeight = FontWeights.Bold };
test.Stack().Children.Add(b);
// Dynamic Page Two
Dynamic_Page_1 test2 = new Dynamic_Page_1();
TextBlock t1 = new TextBlock {Text = "DYNAMIC TEST TWO" };
test2.Stack().Children.Add(t1);
// Dynamically create buttons which link to both Dynamically created pages
Button b3 = new Button { Content = "GO TO TEST ONE", };
Button b4 = new Button { Content = "GO TO TEST TWO", };
b3.Click += (s, e) => {
Frame.Navigate(test.GetType());
};
b4.Click += (s, e) =>
{
Frame.Navigate(test2.GetType());
};
stackPanel1.Children.Add(b3);
stackPanel1.Children.Add(b4);
I fixed this in the end by passing through the stackpanel.
Frame.Navigate(typeof(Dynamic_Page_1), stackPanel1);
Then modifying the navigateTo part to look for it.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
StackPanel stack = (StackPanel) e.Parameter;
// Do something with stackpanel.
}

Close dynamically created form with dynamic button

I'm trying to close a dynamically created form with a dynamic button (this is the simplest of my jobs, I am also adding other buttons to do other jobs but I figured this is a good place to start).
As of now I can create the form, button and the click event for that button, but I don't know what to add within the click event function to close the host of that button. I am guessing I can somehow access the buttons parent through the click function? Or maybe pass the form control as an argument in the function? Any help is appreciated!
//Create form
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += new EventHandler(saveButton_buttonClick);
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
And my click event function looks pretty much normal:
void saveButton_buttonClick(object sender, EventArgs e)
{
}
Unfortunately I don't know what to add for the function to work! Thanks in advance for any help someone can give me! I feel like this should be a straight-forward problem to solve but I haven't been able to figure it out...
While it's certainly possible to do this with a named function, it's generally simpler to just use an anonymous function in cases like this:
Snapshot snapshot = new Snapshot();
snapshot.StartPosition = FormStartPosition.CenterParent;
//Create save button
Button saveButton = new Button();
saveButton.Text = "Save Screenshot";
saveButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 130);
saveButton.Click += (_,args)=>
{
SaveSnapshot();
};
//Create exit button
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Location = new Point(snapshot.Width - 100, snapshot.Height - 100);
exitButton.Click += (_,args)=>
{
snapshot.Close();
};
//Add all the controls and open the form
snapshot.Controls.Add(saveButton);
snapshot.Controls.Add(exitButton);
snapshot.ShowDialog();
A simple way is to use a lambda method:
Button exitButton = new Button();
exitButton.Text = "Exit";
exitButton.Click += (s, e) => { shapshot.Close(); };

How to change the location of a textbox

I have a problem, im making me own custom SharePoint webpart.
everything is going well, but the problem is that i can't figure out how to change the location of the textboxes and labels.
anyone knows how i can change the locations?
I am trying to accomplish it in C#.
problem SOLVED.
With the help of component ids. set position of that particular component.
"How to change the location of the textboxes and labels"
In this example i'm using a Button (Action performed on Button Click) and i am also adding how to Generate a TextBox and a Label (When you press this Button).
Just because this is usually a common process within setting locations to a control.
private void button1_Click(object sender, EventArgs e)
{
// Settings to generate a New TextBox
TextBox txt = new TextBox(); // Create the Variable for TextBox
txt.Name = "MyTextBoxID"; // Identify your new TextBox
// Create Variables to Define "X" and "Y" Locations
var txtLocX = txt.Location.X;
var txtLocY = txt.Location.Y;
//Set your TextBox Location Here
txtLocX = 103;
txtLocY = 74;
// This adds a new TextBox
this.Controls.Add(txt);
// Now do the same for Labels
// Settings to generate a New Label
Label lbl = new Label(); // Create the Variable for Label
lbl.Name = "MyNewLabelID"; // Identify your new Label
// Create Variables to Define "X" and "Y" Locations
var lblLocX = lbl.Location.X;
var lblLoxY = lbl.Location.Y;
//Set your Label Location Here
lblLocX = 34;
lblLoxY = 77;
// Adds a new Label
this.Controls.Add(lbl);
}
}
Note: This is just an example and will not work after postback.
I hope this answers to your and everyone's question.

Adding items to PopupMenu

I am trying to find how I can add items to devExpress PopupMenu. I have tried the following:
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
listBoxMenu.ItemLinks.Add(manager.Items["Remove item"]);
listBoxMenu.ItemLinks.Add(manager.Items["Clear items"]);
As shown here http://documentation.devexpress.com/#WindowsForms/CustomDocument5472 (at the bottom), but it gives me an error saying the item is not initialized.
What is the proper way to add items? I can't find it anywhere.
Edit, here is how I did it:
//Creates the popup menu to be used for the keywords listbox
manager = new BarManager();
listBoxMenu = new PopupMenu(manager);
item = new BarButtonItem(manager, "Copy");
item2 = new BarButtonItem(manager, "Clear Item");
item3 = new BarButtonItem(manager, "Clear All Items");
listBoxMenu.ItemLinks.Add(item);
listBoxMenu.ItemLinks.Add(item2);
listBoxMenu.ItemLinks.Add(item3);
//Adds the seperator on the second item
item2.Links[0].BeginGroup = true;
manager.ItemClick += manager_ItemClick;
Check this code snippet and implement using the same way.
//create popup and manage objects
private DevExpress.XtraBars.BarManager barManager1;
private DevExpress.XtraBars.PopupMenu buttonContextMenu;
DevExpress.XtraBars.BarButtonItem menuButtonExport = new DevExpress.XtraBars.BarButtonItem();
DevExpress.XtraBars.BarButtonItem menuButtonSave = new DevExpress.XtraBars.BarButtonItem();
public TestForm8()
{
InitializeComponent();
barManager1 = new BarManager();
this.barManager1.Form = this;
buttonContextMenu = new DevExpress.XtraBars.PopupMenu(barManager1);
this.buttonContextMenu.Name = "subViewContextMenu";
menuButtonExport.Caption = "E&xport";
menuButtonExport.Id = 1;
menuButtonExport.Name = "menuButtonExport";
menuButtonExport.ItemClick += new ItemClickEventHandler(menuButtonExport_ItemClick);
menuButtonSave.Caption = "S&ave";
menuButtonSave.Id = 2;
menuButtonSave.Name = "menuButtonSave";
menuButtonSave.ItemClick += new ItemClickEventHandler(menuButtonSave_ItemClick);
//add items to barmanager
this.barManager1.Items.AddRange(new DevExpress.XtraBars.BarItem[] {
menuButtonExport,
menuButtonSave
});
//create links between bar items and popup
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonExport"]);
buttonContextMenu.ItemLinks.Add(barManager1.Items["menuButtonSave"]);
//finally set the context menu to the control or use the showpopup method on right click of control
barManager1.SetPopupContextMenu(btnInsert, buttonContextMenu);
}
Ref by step to include popup:
How to: Create a popup menu
How to: Add items to a container bar item (menu)
Populating Popup Menus
BarManager.SetPopupContextMenu Method
Your manager is empty:
manager = new BarManager();
The example you linked to is using a BarManager that was already created: barManager1, which I assume was created in the designer and populated with items.
From their BarManager help page:
After a BarManager has been added to a form/user control, you can create bars and bar commands using context menus right on the form, using the bar manager's Customization Window or its Designer. Please see the Toolbars Customization section, to learn more.

Categories