Navigating Dynamically created Pages - c#

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

Related

Removing controls from a windows form if another menu strip option is clicked

I have a menu strip in place that when clicked upon, adds controls and shows them on the windows application I am making. However, when I try to click on another option and attempt to hide the previous controls shown, it does not hide itself but merely stays on the screen and the datagridview is shown on top of it. I tried the Hide() method but that does not appear to be working.
Here is my code (the AddControls method I made, where it is called, and the HideAllControls method I also made)
Add Controls -
private void AddControls()
{
// begin household head controls
Label householdHeadLbl = new Label()
{
Name = "lbl_householdHead",
Text = "Household Head"
};
householdHeadLbl.Font = new Font(householdHeadLbl.Font.FontFamily, 12);
householdHeadLbl.Location = new Point(86, 75);
householdHeadLbl.Size = new Size(130, 24);
////////////////////////////////////////////
TextBox houseHoldHeadTextBox = new TextBox()
{
Name = "txtBox_householdHead"
};
houseHoldHeadTextBox.Font = new Font(houseHoldHeadTextBox.Font.FontFamily, 12);
houseHoldHeadTextBox.Location = new Point(220, 72);
houseHoldHeadTextBox.Size = new Size(154, 24);
// add the controls
// household head controls
Controls.Add(householdHeadLbl);
Controls.Add(houseHoldHeadTextBox);
}
(There are more controls but I went past the 30000 character limit)
Menu Strip Insert Member Click -
private void MenuInsertMember_Click(object sender, EventArgs e)
{
AddControls();
}
HideAllControls -
private void HideAllControls(Control ctrl)
{
foreach (Control c in Controls)
{
if (c is TextBox || c is Label)
{
c.Hide();
}
else
{
break;
}
}
}
Menu Strip View Click -
private void MenuViewMembers_Click(object sender, EventArgs e)
{
// hide any controls left that may be left over from another option
HideAllControls(this);
}
I included a screenshot to help show/explain what I am encountering.
Insert screenshot - http://imgur.com/zGBY3b4
View screenshot - http://imgur.com/yecBbiw
Any help would be appreciated.
Thanks!

Xamarin creating elements dynamically

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'

Done Button For PickerView ToolBar Not Working

Done button added to pickerview toolbar,
But on click done button click event is not working.
public override void ViewDidLoad(){
myPickerView = new UIPickerView (RectangleF.Empty){
AutoresizingMask = UIViewAutoresizing.FlexibleWidth,
ShowSelectionIndicator = true,
Model = model,
Hidden = true,
BackgroundColor = UIColor.Clear
};
toolbar = new UIToolbar();
toolbar.BarStyle = UIBarStyle.Default;
toolbar.Translucent = true;
toolbar.SizeToFit();
// Create a 'done' button for the toolbar and add it to the toolbar
UIBarButtonItem doneButton = new UIBarButtonItem("Done", UIBarButtonItemStyle.Done,
(s, e) => {
Console.WriteLine ("Calling Done!");
txt_RegistrationType.Text = selectedType;
txt_Email.ResignFirstResponder();
txt_UserName.ResignFirstResponder();
txt_Password.ResignFirstResponder();
txt_PhoneNumber.ResignFirstResponder();
txt_ConformPassword.ResignFirstResponder();
});
toolbar.SetItems(new UIBarButtonItem[]{doneButton}, true);
model.PickerChanged += (sender, e) => {
Console.WriteLine("selected vlaue {0}",e.SelectedValue);
txt_RegistrationType.Text = e.SelectedValue;
};
this.myPickerView.AddSubview (toolbar);
myPickerView.Frame = PickerFrameWithSize (myPickerView.SizeThatFits (SizeF.Empty));
View.AddSubview (myPickerView);
myPickerView.AddSubview (toolbar);
}
On click selected items
its shows pickerView "PickerView.Hidden = false" which appears picker view and toolbar with done button. When click the done button on toolbar its click event is not working.
Please let me know for getting an event on click done button.
Firstly, I want to point out that you're adding your "toolbar" to your current "UIPickerView" twice. Once with:
this.myPickerView.AddSubview (toolbar);
and another time with:
myPickerView.AddSubview (toolbar);
Secondly, why the toolbar? It only adds complexity and dependencies to your project.
What I suggest, is scrap the whole block of code, and make two separate controls on this view:
A button
A UIPickerView
public override void ViewDidLoad()
{
UIButton button = new UIButton (new RectangleF (5, 5, frame_width, 30));
button.TouchUpInside += Method;
UIPickerView pickerView = new UIPickerView (new RectangleF (5, 45, frame_width, 180));
pickerView.Model = model
this.View.AddSubviews (new UIView[]{button, pickerView});
}
void Method (object sender, eventargs e)
{
Console.WriteLine ("Done");
/*If you want to make the pickerView disappear,
you can add a pickerView.Hide() here, and then call a method
to redraw the controls on the current view.*/
}
I hope this helps. Good luck!

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.

Multi-tab application (C#)

I'm creating a multi-tabbed .NET application that allows the user to dynamically add and remove tabs at runtime. When a new tab is added, a control is added to it (as a child), in which the contents can be edited (eg. a text box). The user can perform tasks on the currently visible text box using a toolbar/menu bar.
To better explain this, look at the picture below to see an example of what I want to accomplish. It's just a mock-up, so it doesn't actually work that way, but it shows what I want to get done. Essentially, like a multi-tabbed Notepad.
View the image here: http://picasion.com/pic15/324b466729e42a74b9632c1473355d3b.gif
Is this possible in .NET? I'm pretty sure it is, I'm just looking for a way that it can be implemented.
You could use a simple extension method:
public static void PasteIntoCurrentTab(this TabControl tabControl)
{
if (tabControl.SelectedTab == null)
{
// Could throw here.
return;
}
if (tabControl.SelectedTab.Controls.Count == 0)
{
// Could throw here.
return;
}
RichTextBox textBox = tabControl.SelectedTab.Controls[0] as RichTextBox;
if (textBox == null)
{
// Could throw here.
return;
}
textBox.Paste();
}
Usage:
myTabControl.PasteIntoCurrentTab();
I suggest you keep some "current state" variables updated so you always have a pointer to the selected Tab Page, and its child control (in the case of a tabbed-notepad emulation discussed here : a TextBox). My preference would be to keep track of the TabPage<>TextBox connections using a Dictionary to avoid having to cast the TextBoxes if they are accessed using the TabPage.Controls route : the following code assumes you have a TabControl named 'tabControl1 on a Form :
Dictionary<TabPage, TextBox> dct_TabPageToTextBox;
int tabCnt = 1;
TabPage currentTabPage;
TextBox currentTextBox;
So, as you create each new TabPage at run-time you call something like this :
private void AddNewTabPage()
{
if (dct_TabPageToTextBox == null) dct_TabPageToTextBox = new Dictionary<TabPage, TextBox>();
currentTabPage = new TabPage("Page " + tabCnt.ToString());
tabControl1.TabPages.Add(currentTabPage);
currentTextBox = new TextBox();
dct_TabPageToTextBox.Add(currentTabPage, currentTextBox);
currentTabPage.Controls.Add(currentTextBox);
currentTextBox.Dock = DockStyle.Fill;
currentTextBox.Text = "sample text for page " + tabCnt.ToString();
tabControl1.SelectedTab = currentTabPage;
tabCnt++;
}
As the end-user changes the selected TabPage you can simply update your current state variables like this :
private void tabControl1_SelectedIndexChanged(object sender, EventArgs e)
{
currentTabPage = tabControl1.SelectedTab;
currentTextBox = dct_TabPageToTextBox[currentTabPage];
MessageBox.Show("text in current Tab Page is : " + currentTextBox.Text);
}
So now have the code that is invoked by your menu choices applied only to the currentTextBox.
best, Bill
I tried this for fun ... I made a form with a ToolStripContainer, and a ToolStrip inside it, with the standard buttons (which includes the paste button). I renamed the paste button to pasteButton, and hooking everything up you get:
public Form2()
{
InitializeComponent();
TabControl tc = new TabControl();
toolStripContainer1.ContentPanel.Controls.Add(tc);
tc.Dock = DockStyle.Fill;
TextBox selectedTextBox = null;
pasteButton.Click += (_, __) => selectedTextBox.Paste(Clipboard.GetText(TextDataFormat.Text));
int pages = 0;
newTabButton.Click += (_,__) => {
TextBox tb = new TextBox { Multiline = true, Dock = DockStyle.Fill, ScrollBars = ScrollBars.Vertical };
TabPage tp = new TabPage("Page " + (++pages).ToString());
tc.Selected += (o, e) => selectedTextBox = e.TabPage == tp ? tb: selectedTextBox;
tp.Controls.Add(tb);
tc.TabPages.Add(tp);
tc.SelectedTab = tp;
selectedTextBox = tb;
};
}

Categories