Done Button For PickerView ToolBar Not Working - c#

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!

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!

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

How to hide/show button and change size form C#

I have a form with a simple calculator with the size 245 x 359.
I currently have a show/hide button for scientific functions.
I want to be able to click a button and have it show the scientific functions.
With the button size 300 x 400.
In your button click event you can do the following:
this.Size = new Size(300,400);
In the designer, just make these buttons, but then drag it's size to how you want it before the specific button is pressed.
Add a event handler for the button you want, then do this:
bool buttonPressed = false;
private void onChangeSizeButton_pressed(EventArgs e, object o)
{
if (buttonPressed)
{
this.Size = new Size(this.Size.Width, DEFAULT HEIGHT HERE);
buttonPressed = false;
}
else
{
this.Size = new Size(this.Size.Width, NEW HEIGHT HERE);
buttonPressed = true;
}
}
In your button click event, set each scentific function to:
btnMC.Visbile = true;
btnMC.Size = new Size(300, 400);
btnMR.Visbile = true;
btnMR.Size = new Size(300, 400);
ect...
That should do the trick. You might want to considered resizing your form when they click the button as well.
In your example, do not need to hide and show button but only need to change text.
Add Fix button on panel or user control and change the text. If require then user Button.Visible = false and true. Share your sample code for work on it.

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(); };

UIBarButtonItem with Custom View

I try to create a UIBarButtonItem with a custom view for a Toolbar. The view itself is displayed well, but when i click the BarButton, no action occurs. It looks like the touch event is not forwarded from the view to the UIBarButtonItem instance. I have checked the responder chain and i think it looks good. I have also searched the internet and checked the Apple documentation, but can't find any hint for my problem.
Here is my code:
g__objWeatherButton = new UIBarButtonItem[1];
UIView l__objCustomView = g__objWeatherDisplay.InfoBarButton; // Returns a reference to my custom view
UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomView);
l__objButton.Clicked += delegate {this.WeatherButtonEvent();}; // my action handler
l__objButton.Width = 200;
l__objButton.Enabled = true;
g__objWeatherButton[0] = l__objButton;
this.Items = g__objWeatherButton; // "this" is my UIToolbar object
Can someone give me a hint where the problem is? Or a working code sample (in c# please - have found some examples in Objective-C, but apparently overlooked the crucial trick ;-)
No special trick. When you want to add a custom view to a toolbar or navigation bar, you should subscribe (and respond) to that view's events. So instead of using a UIView to hold your image, create a UIButton with UIButtonType.Custom and subscribe to that button's TouchUpInside event.
You then initialize it like you do with the UIView:
UIButton l__objCustomUIButton = UIButton.FromType(UIButtonType.Custom);
//l__objCustomUIButton.SetImage(UIImage.FromFile("your button image"), UIControlState.Normal);
l__objCustomUIButton.TouchUpInside += delegate { this.WeatherButtonEvent(); };
UIBarButtonItem l__objButton = new UIBarButtonItem(l__objCustomUIButton);
Just make sure you declare the button in the class scope.
Although the answer put me in the right direction I still had trouble displaying the button. Only after I added the following the was button display with image.
l__objCustomUIButton.Frame = new System.Drawing.RectangleF(0, 0, 40, 40);
Regards
Paul
There is an easier way and the custom button will now react like a regular toolbar button.
this.SetToolbarItems( new UIBarButtonItem[] {
new UIBarButtonItem(UIBarButtonSystemItem.Refresh, (s,e) => {
Console.WriteLine("Refresh clicked");
})
, new UIBarButtonItem(UIBarButtonSystemItem.FlexibleSpace) { Width = 50 }
, new UIBarButtonItem(UIImage.FromBundle
("wrench_support_white.png"), UIBarButtonItemStyle.Plain, (sender,args) => {
Console.WriteLine("Support clicked");
})
}, false);

Categories