Xamarin IOS: How to display a popup control from button click - c#

This is for iPhone.
I have a button and when it's clicked I want to pop-up another control which covers the whole screen. This screen could have any number of controls. And I can close this screen by clicking on an x in the top right corner or programmatically inside any event on the new screen.
I could probably do this by using a UINavigationController which just brings me to a new screen and has a link back to the previous screen but I would just like to ask if there is another option?
What I am doing is I have a map which shows a users location from a pin. But if the user wants to type in a new location instead of using the pin location then they will click a button, go to a new screen, type in an address and click a "suggested" address from what they type.
Any advice would be appreciated or a link to a code sample would be great

You can't use popover for iPhone, I think that you can use Modal View.
yourButton.TouchUpInside += (object sender, EventArgs e) =>
{
YourController yourController = new YourController();
yourController.ModalPresentationStyle = UIModalPresentationStyle.FormSheet;
this.PresentViewController(yourController, true, null);
};
And to close you only need to dismiss the modal.

If it's an iPad app you can use UIPopoverController.
// myvc is an instance of the view controller you want to display in the popup
UIPopoverController pop = new UIPopoverController(myvc);
// target is another view that the popover will be "anchored" to
pop.PresentFromRect(target.Bounds, target, UIPopoverArrowDirection.Any, true);
If it's an iPhone app you can't use UIPopoverController. If you just want a small input box with a single button, you can use UIAlertView (this works for iPhone and iPad)
UIAlertView alert = new UIAlertView();
alert.Title = "Title";
alert.AddButton("OK");
alert.Message = "Please Enter a Value.";
alert.AlertViewStyle = UIAlertViewStyle.PlainTextInput;
alert.Clicked += (object s, UIButtonEventArgs ev) => {
// handle click event here
// user input will be in alert.GetTextField(0).Text;
};
alert.Show();

Related

Access to other window of application - WhiteStack

I would like to test to Outlook application and send an email to me by using White Stack framework. I implemented code who click on 'New Item' and after that the new window appears. I want to type a my mail to TextBox 'To' but I don't know how get to access to second window 'Untitled - Message (HTML)'. Photo
[TestMethod]
public void mail()
{
var application = Application.Launch(appPatch);
Thread.Sleep(2000);
var window = application.GetWindow(appTitle, InitializeOption.NoCache);
SearchCriteria searchCriteriaNewEmail = SearchCriteria.ByText("New Email");
Button buttonNewEmail = window.Get<Button>(searchCriteriaNewEmail);
buttonNewEmail.Click();
Thread.Sleep(1000);
string windowTitle = "Untitled‬ - Message(HTML)";
var window2 = application.GetWindow(windowTitle, InitializeOption.NoCache);
SearchCriteria searchCriteriaTo = SearchCriteria.ByText("To");
TextBox tbxTo = window2.Get<TextBox>(searchCriteriaTo);
tbxTo.BulkText = "mymail#gmail.com";
Thread.Sleep(2000);
window.Close();
}
The "second window" is in the same process id, so with that you can't simply use the .GetWindow() method.
For MDI applications you should use MdiChild(), or if it is a modal window, use .ModalWindow() on the parent window, or go back to the application then use Window() to get the window.
I would add that getting the window by it's title is not recommended, since that will change as you type the new title of the e-mail.

How to use TestStack.White to click the button under the menu

I tried to use white to click the new project button, I try using the below code, but it can not work, anyone can help me?
public void Notepad()
{
Application app = Application.Launch("C:\\Program Files (x86)\\SourceMonitor\\SourceMonitor.exe");
Window window = app.GetWindow("SourceMonitor", InitializeOption.NoCache);
Button button = window.Get<Button>("File");
button.Click();
app.Kill();
}
I already figured out how to do it, below is the correct code:
public void Notepad()
{
// Arrange
Application app = Application.Launch("C:\\Program Files (x86)\\SourceMonitor\\SourceMonitor.exe");
Window window = app.GetWindow("SourceMonitor", InitializeOption.NoCache);
// Act
MenuBar menuBar = window.MenuBar;
menuBar.MenuItem("File","New Project").Click(); ; //can use any other search criteria as well.
}

How to open multiple windows on a button click?

In my design there are three buttons. I want to open new windows on each button click. I have done upto open a new window. But when I click on the second button it opens in the same popup window. How can I avoid this and open three windows when click on these three buttons?
c# code
protected void btnApprove_Click(object sender, EventArgs e)
{
string ddlVal = ddlComp.SelectedValue.ToString();
if (ddlVal != "--Select The Competition--")
{
Session["ddlVal"] = ddlComp.SelectedValue.ToString();
ScriptManager.RegisterStartupScript(this, typeof(string), "APPROVE_WINDOW", "var Mleft = (screen.width/2)-(760/2);var Mtop = (screen.height/2)-(700/2);window.open( 'approved.aspx', null, 'resizable=yes, status=yes,toolbar=no,scrollbars=yes,menubar=no,location=no,top=\'+Mtop+\', left=\'+Mleft+\'' );", true);
}
else
{
WebMsgBox.Show("Select a competition");
}
}
This is the code I have used for all the three buttons with different page names
You can pass the name parameter as '_blank' instead of null.
Change the below line in your code
window.open( 'approved.aspx', null,
to
window.open( 'approved.aspx', '_blank',
If you're referring to top-level browser windows, you cannot - browsers disable this for obvious reasons (pop-up blockers, etc). You also cannot have more than one JavaScript alert() window open at a time.
Your WebMsgBox class wraps the alert() function. So this not possible.
You will need to change your client-code to instead display multiple elements (e.g. absolutely-positioned <div> boxes with a modal rectangular appearance).

Adding a twin tabPage to tabControl through a user command

I'm a newbie in c# and probably going to ask a very easy question, but I've not been able to find anything on the web to help.
I have a tabControl with a TabPage which is containing a TextBox object; this object, when the event "Text changed" is invoked, will perform the change of the parent tabPage's name.
The textbox where I typed "text changed by me" has a method which is managing changing the name of the tabPage:
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.textBox1.Text != "")
this.tabControl2.SelectedTab.Text = this.textBox1.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
}
Into the current page menu is contained a control to add a new page, which runs this method when the user click on it:
private void addNewPageToolStripMenuItem_Click(object sender, EventArgs e)
{
int numPagine;
string strPagine;
numPagine = this.tabControl2.TabCount;
strPagine = numPagine.ToString();
this.tabControl2.TabPages.Add("new page" + strPagine);
}
...and here is the output, which is expected since I'm just asking to add a new empty tabPage:
So, my question is: how can I make possible that when the user is clicking on "Add new page", rather than creating an empty new tabPage the program is rather creating a page like the first one (i.e. containing a textbox into the same position which has a method to change the text of the parent tabPage that I have just created?
Here is an example.
//..
// create the new page
TabPage tpNew = new TabPage("new page..");
// add it to the tab
this.tabControl2.TabPages.Add(tpNew);
// create one labe with text and location like label1
Label lbl = new Label();
lbl.Text = label1.Text;
lbl.Location = label1.Location;
// create a new textbox..
TextBox tbx = new TextBox();
tbx.Location = textBox1.Location;
tpNew.Controls.Add(lbl);
tpNew.Controls.Add(tbx);
// add code to the new textbox via lambda code:
tbx.TextChanged += ( (sender2, evArgs) =>
{
if (tbx.Text != "")
this.tabControl2.SelectedTab.Text = tbx.Text;
else
this.tabControl2.SelectedTab.Text = "(no name)";
} );
For more complicated layout you may want to consider creating a user control..
You also may want to create the first page with this code; the, of course with real values for text and positions!
For creating a UserControl you go to the project tag and right click Add-UserControl-UserControl and name it, maybe myTagPageUC. Then you can do layout on it like on a form. A rather good example is right here on MSDN
The problem is that is has no connection to the form, meaning you'll have to code all sorts of references to make it work..
I'm not really sure if you may not be better off writing a complete clonePage method instead. It could work like the code above, but would loop over the Controls of the template page and check on the various types to add the right controls..
It really depends on what is more complicated: the Layout or the ties between the pages and the form and its other controls..

How do I remove a specific panel through event handling in my FlowlayoutPanel for my Windows Form Application?

For my C# Windows Form Application, I have created a flowlayoutpanel that contains several panels. Inside the panel, I have a button "Clear" for each and every single panel.
How do I write the event handler for the code for the button "Clear" such that once I have click the button, the panel would sort of be "Removed" from the flowlayoutpanel.
This is a short part of the code of the adding of panels to the flowlayoutpanel.
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanel());
nFlowPanel.Controls.Add(createNotificationPanelImpt());
nFlowPanel.Controls.Add(createNotificationPanelImpt());
and this is the code for the button "Clear"
Button btnClear = new Button
{
Text = "Clear",
Name = "btnClear",
Location = new Point(416, 17)
};
p.Controls.Add(btnClear);
btnClear.Click += new EventHandler(buttonClear_Click);
So what should i write in the following method to have the effect of removing e.g. the second panel that was added in the first part of code I have written?
void buttonClear_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
EDIT
the code for creating my panel is
var p = new Panel
{
BorderStyle = BorderStyle.FixedSingle ,
Size = new Size(506,100),
Name = "notifyPanel"
};
and the code for creating my FlowLayoutPanel is
var nFlowPanel = new FlowLayoutPanel
{
FlowDirection = FlowDirection.TopDown,
WrapContents = false,
AutoScroll = true,
Size = new Size(530, 377),
Location = new Point(13, 145)
};
and the code for my button clear is
void buttonClear_Click(object sender, EventArgs e)
{
var button = (Control)sender;
var panel = button.Parent.Controls["notifyPanel"];
panel.Dispose();
}
however it gives the error
Object reference not set to an instance of an object.
on the panel.Dispose() line.
anyone can help?
The Controls.Remove() method is very dangerous, it doesn't dispose the control. Which will live on, moved to the so-called parking window, using up both Windows and managed resources. After a bit less than 10,000 times doing this it crashes your program when Windows is no longer willing to let you create any more windows.
Call the control's Dispose() method instead. That also automatically removes the control from its container.
void buttonClear_Click(object sender, EventArgs e)
{
var panel = nFlowPanel.Controls["notifyPanel"];
panel.Dispose();
}
You can do it like this:
nFlowPanel.Controls.Remove((sender as Button).Parent);
I will suggest you to use List for this. Before adding Panels in the FlowLayoutpanel, add them in the List. Then just remove the indexed panel from the flowlayoutpanel.
Panel pnlTemp = (panel)list[index];
nFlowPanel.Controls.Remove(pnlTemp);
To get the index of the button you have to add your buttons also to your list and after clicking any button, search the button in the list and get the index of the button where it is saved in the list. If my code is unclear, let me know. but I feel your task is that complex. I am not sure but this link may be of some help.
Hope it helps.

Categories