I'm observing that when the Popup control loses focus it returns focus back to the object that initiated the Popup. So basically this sequence of events.
Textbox GotFocus fires and I open the popup
When finished with the Popup user clicks anywhere outside Popup to activate LostFocus
LostFocus activates, but immediately after GotFocus for the object in step 1 (Textbox) executes - relaunching the popup.
My question, is there a way to reset focus to whatever the default is when you first start an app? I want to prevent it from returning focus back to the control that launched the popup.
EDIT: I removed LostFocus event because I found it wasn't necessary in order to remove the popup from view when user taps away.
Popup p;
public MainPage()
{
this.InitializeComponent();
this.NavigationCacheMode = NavigationCacheMode.Required;
p = new Popup();
p.IsLightDismissEnabled = true;
//p.LostFocus += p_LostFocus;
}
private void m_GotFocus(object sender, RoutedEventArgs e)
{
if (p.IsOpen != true)
{
// Create some content to show in the popup. Typically you would
// create a user control.
Border border = new Border();
border.BorderBrush = new SolidColorBrush(Colors.Black);
border.BorderThickness = new Thickness(0);
StackPanel panel1 = new StackPanel();
panel1.FlowDirection = Windows.UI.Xaml.FlowDirection.LeftToRight;
panel1.Background = new SolidColorBrush(Color.FromArgb(255, 30, 30, 30));
Button button1 = new Button();
button1.Content = ".";
button1.Margin = new Thickness(5.0);
button1.BorderThickness = new Thickness(0);
button1.Background = new SolidColorBrush(Color.FromArgb(255, 90, 90, 90));
button1.Click += new RoutedEventHandler(Feedback_Click);
TextBlock textblock1 = new TextBlock();
textblock1.Text = "";
textblock1.TextWrapping = TextWrapping.WrapWholeWords;
textblock1.Margin = new Thickness(5.0);
panel1.Children.Add(textblock1);
panel1.Children.Add(button1);
border.Child = panel1;
// Set the Child property of Popup to the border
// which contains a stackpanel, textblock and button.
p.Child = border;
// Set where the popup will show up on the screen.
p.VerticalOffset = 400;
p.HorizontalOffset = 100;
// Open the popup.
p.IsOpen = true;
}
}
Related
I'm creating a ComboBox control using ToolStripControlHost and ToolStripDropDown that can host any kind of control in the DropDown window. For example, the DropDown window might display a listview or treeview or even another usercontrol.
I'm posting a simplified code below where dropdown host a usercontrol with a listview and a button like this:
The problem occurs when the control is positioned at the bottom of the screen in such a way that the dropdown window will extrapolate the lower boundary of the screen. When this occurs, the dropdown ends up hiding the control.
In this case, I'd like to fix the _dropDown.Show method call to show dropdown window as follows:
To repeat the problem, just run the code below and drag the window to the bottom of the screen and open the dropdown.
using System;
using System.Windows.Forms;
public class CustomComboBox : UserControl
{
ToolStripDropDown _dropDown;
public CustomComboBox()
{
var textbox = new TextBox();
textbox.Location = new System.Drawing.Point(0, 0);
textbox.Size = new System.Drawing.Size(this.Width - 22, 20);
textbox.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
this.Controls.Add(textbox);
var button = new Button();
button.Location = new System.Drawing.Point(this.Width - 22, -1);
button.Size = new System.Drawing.Size(22, 22);
button.Text = "\u2BC6";
button.Anchor = AnchorStyles.Right | AnchorStyles.Top;
button.Click += new System.EventHandler(this.Button_Click);
this.Controls.Add(button);
var dropDownControl = new DropDownControlTest();
var controlHost = new ToolStripControlHost(dropDownControl);
_dropDown = new ToolStripDropDown();
_dropDown.AutoSize = true;
_dropDown.Items.Add(controlHost);
}
void Button_Click(object sender, EventArgs e)
{
_dropDown.Show(this, 0, this.Height);
}
}
public class DropDownControlTest : UserControl
{
public DropDownControlTest()
{
var listview = new ListView();
listview.Location = new System.Drawing.Point(3, 1);
listview.Size = new System.Drawing.Size(400,300);
listview.View = View.Details;
listview.Columns.Add("Col 1",100);
listview.Columns.Add("Col 2",100);
this.Controls.Add(listview);
var button = new Button();
button.Location = new System.Drawing.Point(3, 305);
button.Text = "More...";
this.Controls.Add(button);
}
}
public class Form1 : Form
{
private static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
public Form1 ()
{
CustomComboBox ccBox = new CustomComboBox();
ccBox.Location = new System.Drawing.Point(10, 10);
ccBox.Height = 20;
this.Text = "Test CustomComboBox";
this.Controls.Add(ccBox);
}
}
You can use the ToolStripDropDown.Show Method (Control, Point, ToolStripDropDownDirection) overload to control the drop direction. The code will need to perform bounds checking to decide whether to place the dropdown above or below the textbox.
The following is a simplistic method for doing the bounds checking and was only tested on a single screen configuration.
First, make textbox a class level variable.
private TextBox textbox;
public CustomComboBox()
{
//var textbox = new TextBox();
textbox = new TextBox();
The display logic is as follows.
void Button_Click(object sender, EventArgs e)
{
Point textBoxScreenLocation = textbox.PointToScreen(textbox.Location);
// try to position _dropDown below textbox
Point pt = textBoxScreenLocation;
pt.Offset(0, textbox.Height);
// determine if it will fit on the screen below the textbox
Size dropdownSize = _dropDown.GetPreferredSize(Size.Empty);
Rectangle dropdownBounds = new Rectangle(pt, dropdownSize);
if (dropdownBounds.Bottom <= Screen.GetWorkingArea(dropdownBounds).Bottom)
{ // show below
_dropDown.Show(pt, ToolStripDropDownDirection.BelowRight);
}
else
{ // show above
_dropDown.Show(textBoxScreenLocation, ToolStripDropDownDirection.AboveRight);
}
}
}
I can not comment that is why I am answering your question. You can use the reflection and then re-position you control. I have found a custom combobox control same as you developed. Please check this. At least, you will get some idea what you need to do.
I have a TextBox, but I can't find any source explaining how to call a function when a button is pressed down.
public Simple()
{
Text = "Server Command Line";
Size = new Size(800, 400);
CenterToScreen();
Button button = new Button();
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.Parent = this;
button.Text = "SEND";
button.Size = new Size (50, 20);
button.Location = new Point(620, Size.Height-70);
button.Parent = this;
button.Click += new EventHandler(Submit);
}
Some sources tell me to use a function, but I don't understand how it is going to get called.
If I understood correctly you want to call a method when users press Enter while typing anything in textbox? If so, you have to use the KeyUp event of TextBox like this:
public Simple()
{
Text = "Server Command Line";
...
TextBox txt = new TextBox ();
txt.Location = new Point (20, Size.Height - 70);
txt.Size = new Size (600, 30);
txt.KeyUp += TextBoxKeyUp; //here we attach the event
txt.Parent = this;
Button button = new Button();
...
}
private void TextBoxKeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//Do something
e.Handled = true;
}
}
you already have a button as well like this
button.Click += new EventHandler(Submit);
if you want to call this function you can do this
button.PerformClick(); //this will call Submit you specified in the above statement
Why my Ok and Cancel Button are not showing up on the UI screen? I see the form, the label and the text box but I can't see the Cancel and OK buttons.
To give you a background I am creating this dialog box programmatically and all I need a a couple of text boxes , and their labels of course. And an OK and Cancel button .
All these sizes that I have used here is by trial and error as I am not much experienced in the UI control area of Visual C# 2010.
public void function x ()
{
var fileNameDialog = new Form();
fileNameDialog.Text = "Save New Name";
Label fileLabel = new Label();
fileLabel.Size = new System.Drawing.Size(150, 40);
fileLabel.Text= "Enter Person Name";
fileNameDialog.Controls.Add(fileLabel);
TextBox fileTextBox = new TextBox();
fileTextBox.Location = new System.Drawing.Point(fileLabel.Location.X + 300, fileLabel.Location.Y);
fileTextBox.Size = new System.Drawing.Size(220, 40);
fileNameDialog.Controls.Add(fileTextBox);
fileTextBox.TextChanged += TextBox_TextChanged;
fileTextBox.Text= textboxValue;
Button okButton = new Button();
okButton.Visible = true;
okButton.Text = "OK";
okButton.Location = new System.Drawing.Point(fileTextBox.Location.X, fileTextBox.Location.Y - 80);
fileNameDialog.Controls.Add(okButton);
okButton.Click += new EventHandler(okButton_Click);
Button cancelButton = new Button();
cancelButton.Visible = true;
cancelButton.Text = "Cancel";
fileNameDialog.Controls.Add(cancelButton);
cancelButton.Click += new EventHandler(cancelButton_Click);
cancelButton.Location = new System.Drawing.Point(fileTextBox.Location.X+50, fileTextBox.Location.Y - 80);
fileNameDialog.ShowDialog();
}
Your fileTextbox.Location.Y is zero, so subtracting 80 puts in above the form.
Try fileTextBox.Bottom + 4 or something like that.
Using the designer to create this dialog form is probably the better route to take. Along with the placement of the controls, you can use Anchors to make the controls relate to the size of the form.
I add a MenuStrip to my form and I would like to add other controls below it like usual Point(0, 0) is the top left corner of empty form space. After I add the menu to my form and add more controls they overlap each other. So I want to take away some height of the client rect for the menu and a button with Location = (0,0) must be RIGHT below the menu.
How do I do that ?
If I assign a MainMenu to Menu property of the form it does that automatically but I really want and need to use MenuStrip.
Edit: this doesn't work:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = 50;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 25, 128, 50);
Controls.Add(b);
While this works like I would like it to do with MenuStrip:
Menu = new MainMenu();
Menu.MenuItems.Add("File");
Button b = new Button();
b.Text = "hello world";
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
When you SetBounds(0, 25, 128, 50), you are actually setting b.Top to 25 (the second parameter). In order to set the top bound relative to the menu control, use:
b.SetBounds(0, menu.Bottom, 128, 50);
[UPDATE]
Alternatively, you could use:
public partial class Form1 : Form
{
private int menuStripHeight = 50;
public Form1()
{
InitializeComponent();
this.ControlAdded += Form1_ControlAdded;
}
private void Form1_Load(object sender, EventArgs e)
{
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Button b = new Button();
b.Text = "hello world";
// note that the position used is 0,0
b.SetBounds(0, 0, 128, 50);
Controls.Add(b);
}
void Form1_ControlAdded(object sender, ControlEventArgs e)
{
if (e.Control.GetType().FullName != "System.Windows.Forms.MenuStrip")
e.Control.Top += menuStripHeight;
}
}
[UPDATE 2]
Or you could just use a Panel:
MenuStrip menu = new MenuStrip();
menu.Items.Add("File");
menu.AutoSize = false;
menu.Height = menuStripHeight; ;
menu.Dock = DockStyle.Top;
MainMenuStrip = menu;
Controls.Add(menu);
Panel p = new Panel();
p.SetBounds(0, menuStripHeight, this.Width, this.Height);
Controls.Add(p);
Button b = new Button();
b.Text = "hello world";
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
Use DockStyle.Top in both MenuStrip and Panel, but add them in the reverse order. Adding a control with Dock=Top puts this last control closest to the border, that is, on top of all the other controls. So without resorting to private constants and event handlers:
MenuStrip menu = new MenuStrip() {
AutoSize = false,
Dock = DockStyle.Top
};
menu.Items.Add("File");
Panel p = new Panel(){
Dock = DockStyle.Top
};
Controls.Add(p);
Controls.Add(menu);
MainMenuStrip = menu;
Button b = new Button(){
Text = "hello world"
};
p.Controls.Add(b);
b.SetBounds(0, 0, 128, 50);
Actually, I'm using C# with WPF. Let me explain again:
I have create UserControl (WPF) with one Button (Click on Button to create Image and textBox (Like New Folder)).
And I have another Window form (WPF) with Button and I want to Click on Button in Window Form to do Action instead of Button in UserControl. Here is the code of UserControl:
private void btnNewRoom_Click(object sender, RoutedEventArgs e)
{
Image img = new Image();
img.VerticalAlignment = System.Windows.VerticalAlignment.Top;
img.Width = 100;
img.Height = 100;
var assemblyLocation = Assembly.GetExecutingAssembly().Location;
var applicationDirectory = System.IO.Path.GetDirectoryName(assemblyLocation);
img.Source = new BitmapImage(new Uri("/Resources/Images/folder.png", UriKind.RelativeOrAbsolute));
StackPanel sp = new StackPanel();
TextBox tb = new TextBox();
tb.Width = 120;
tb.Height = 25;
tb.Text = "New Folder";
sp.Children.Add(img);
sp.Children.Add(tb);
AddNewRoom.Children.Add(sp);
}
and It is possible ?
Note: Any good way to Click button to Create Image and TextBox?
Implement a static event handler in your WPF control, and simply call it from your Window Form's button click event.