Windows C# Form: Prompt focus on a textbox - c#

I was wondering how would I auto select a textbox when using a Prompt on windows form. My code below shows what I have tried, but it is still focusing on the button and not the textbox. Thank you in advance for the help and assistance.
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (sender, e) => { prompt.Close(); };
textBox.Select();
textBox.Focus();
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.ShowDialog();
return textBox.Text;

You need to wait to focus the textbox until after the form is shown. Before the form has been shown for the first time it is not able to focus anything. You can use the Shown event to execute some code after the form is first shown.
string text = "Text";
string caption = "caption";
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 200;
prompt.Text = caption;
Label textLabel = new Label() { Left = 50, Top = 20, Text = text };
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 };
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 };
confirmation.Click += (s, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(textBox);
prompt.Shown += (s, e) => textBox.Focus();
prompt.ShowDialog();
return textBox.Text;

Related

Popup in WPF does not open

I have a datagrid with modified column header which contain a button which shall open a popup.
This is written in code behind because of different data sources with different number of columns.
That's how it looks like:
Popups are stored in:
Dictionary<string, Popup> HeaderPopups = new Dictionary<string, Popup>();
And here the code behind:
dgMaterials.AutoGeneratingColumn += (ss, ee) =>
{
Button b = new Button() { Content = "...", Name = "btn_" + ee.PropertyName, Margin = new Thickness(3) };
b.Click += HeaderFilterButtonClick;
StackPanel stackPanel = new StackPanel() { Orientation = Orientation.Horizontal };
stackPanel.Children.Add(new TextBlock() { Text = ee.PropertyName, VerticalAlignment = VerticalAlignment.Center });
stackPanel.Children.Add(b);
ee.Column.Header = stackPanel;
Popup pop = new Popup() { Name = "pop_" + ee.PropertyName, Placement = PlacementMode.Bottom, PlacementTarget = b, StaysOpen = false, Width = 200, Margin = new Thickness(3) };
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.DataContext = bord;
HeaderPopups.Add(ee.PropertyName, pop);
StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
bord.DataContext = stack;
StackPanel stackButtons = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(0, 0, 0, 15) };
Button bAll = new Button() { Margin = new Thickness(0, 0, 0, 0), Name = "btnAll_" + ee.PropertyName };
bAll.Click += btnAllClick;
TextBlock txtAll = new TextBlock() { Text = "Select All", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
bAll.Content = txtAll;
Button bNone = new Button() { Margin = new Thickness(10, 0, 0, 0), Name = "btnNone_" + ee.PropertyName };
bNone.Click += btnNoneClick;
TextBlock txtNone = new TextBlock() { Text = "Select None", Foreground = Brushes.Blue, Cursor = Cursors.Hand };
bNone.Content = txtNone;
stackButtons.Children.Add(bAll);
stackButtons.Children.Add(bNone);
ListBox list = new ListBox() { Name = "lst_" + ee.PropertyName, BorderThickness = new Thickness(0) };
stack.Children.Add(stackButtons);
stack.Children.Add(list);
};
So for each column a popup is generated and I have the popups with the keys Spec_No, Grade and Class in my HeaderPopups dictionary.
I want the appropriate popups to show up beneath the clicked button, like in the example from http://www.jarloo.com/excel-like-autofilter-in-wpf/
Look here:
My problem is to open these popups in HeaderFilterButtonClick-Event. I tried it with:
private void HeaderFilterButtonClick(object sender, RoutedEventArgs e)
{
Button b = sender as Button;
txtTest.Text += e.OriginalSource.ToString() + Environment.NewLine;
txtTest.Text += e.Source.ToString() + Environment.NewLine;
txtTest.Text += b.Name;
if (b.Name == "btn_Spec_No")
{
HeaderPopups["Spec_No"].IsOpen = true;
}
}
but it doesn't work.
Can anybody help?
Your Popup is currently empty and thus completely invisible.
You should set the Child property of it to the Border and also set the Child property of the Border to something for it to render:
Popup pop = new Popup() { ... };
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1, 1, 1, 1) };
bord.Child = new TextBlock() { Text = "some content..." };
pop.Child = bord;
The popup is opening and rendering, but it is empty, so it can't be seen.
the problem is here
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.DataContext = bord;
Datacontext is used to set Binding targets, which an empty popup has no bindings.
You need the fill the child object instead by changing the above into
Border bord = new Border() { Background = Brushes.White, BorderBrush = Brushes.Gray, BorderThickness = new Thickness(1,1,1,1) };
pop.Child = bord;
this sets the root of the popup container to ther Border object.
You will also have to do the same with the stack panel and border
StackPanel stack = new StackPanel() { Margin = new Thickness(5, 5, 5, 15) };
bord.Child = stack;

Close button also save values

I have a dialog where the user can input two integers with OK button. The entered values are saved to variables. However, if I close it from the close button it also save the last entered values, which means not only the OK button save them. what am I doing wrong here?
The dialog:
namespace WindowsFormsApplication1
{
public static class inputBoxDialog
{
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170 };
confirmation.Click += (sender, e) => { prompt.Close();};
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
prompt.ShowDialog();
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
}
}
I call the dialog here:
namespace WindowsFormsApplication1
{
public partial class MainWindow : Form
{
public MainWindow()
{
InitializeComponent();
}
int[] loopParams = new int[2] { 0, 0 };
private void button1_Click(object sender, EventArgs e)
{
inputBoxDialog.ShowDialog("For Loop Begin: ", "For Loop End: ", "Popup", loopParams);
//display the result
label1.Text = "1:- " + loopParams[0]+"\r\n2:- " + loopParams[1];
}
}
}
As pointed out by Jimi, you need to set the OK buttons DialogResult property to DialogResult.OK:
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
And to check the returned DialogResult value from the prompt.ShowDialog() method:
if (prompt.ShowDialog() == DialogResult.OK)
{
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
Here is your code with the above additions:
public static void ShowDialog(string text1, string text2, string caption, int[] lastValue)
{
Form prompt = new Form();
prompt.Width = 500;
prompt.Height = 250;
prompt.Text = caption;
prompt.StartPosition = FormStartPosition.CenterScreen;
//input 1
Label textLabel = new Label() { Left = 50, Top = 20, Text = text1 };
NumericUpDown inputBox1 = new NumericUpDown() { Left = 50, Top = 50, Width = 400 };
inputBox1.Value = lastValue[0];
//input 2
Label textLabe2 = new Label() { Left = 50, Top = 100, Text = text2 };
NumericUpDown inputBox2 = new NumericUpDown() { Left = 50, Top = 130, Width = 400 };
inputBox2.Value = lastValue[1];
Button confirmation = new Button() { Text = "Ok", Left = 350, Width = 100, Top = 170, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.Controls.Add(inputBox1);
prompt.Controls.Add(textLabe2);
prompt.Controls.Add(inputBox2);
if (prompt.ShowDialog() == DialogResult.OK)
{
// OK
lastValue[0] = (int)inputBox1.Value;
lastValue[1] = (int)inputBox2.Value;
}
else
{
// Cancel
}
}
Checking the prompt.DialogResult in the prompt.FormClosed event:
prompt.FormClosed += (sender, e) =>
{
if (prompt.DialogResult == DialogResult.OK)
{
// OK
}
else
{
// Cancel
}
};

Text box added at runtime will not get focus

I have created a user authentication form at runtime that is supposed to have focus when it is loaded. I want to have focus on the first textbox which is for the username, but when the form loads both the form and textbox do not have focus. If I click on the form then the focus will be set to the appropriate textbox.
Form frm = Variables.FormCntrls.fmAuth;
frm.Width = 315;
frm.Height = 175;
frm.StartPosition = FormStartPosition.CenterScreen;
frm.FormBorderStyle = FormBorderStyle.None;
frm.TopMost = true;
frm.BackColor = Color.Black;
frm.ShowInTaskbar = false;
frm.Opacity = .9;
frm.Name = "userAuthentication";
frm.ShowInTaskbar = false;
frm.KeyPreview = true;
frm.Visible = true;
frm.Enabled = true;
Label lb = new Label()
{
Text = "User Authentication",
Width = frm.Width - 20,
Height = 30,
TextAlign = ContentAlignment.MiddleCenter,
Left = 10,
Top = 10,
ForeColor = Color.White,
Font = new Font("Arial", 16, FontStyle.Bold | FontStyle.Underline)
};
frm.Controls.Add(lb);
lb = new Label()
{
Text = "Username: ",
AutoSize = true,
TextAlign = ContentAlignment.MiddleCenter,
Left = 10,
Top = lb.Bottom + 20,
ForeColor = Color.White,
Font = new Font("Arial", 10, FontStyle.Bold)
};
frm.Controls.Add(lb);
TextBox tb = new TextBox()
{
Name = "user",
Width = 200,
Left = lb.Right + 2,
Top = lb.Top,
ForeColor = Color.Black,
TabIndex = 1,
};
frm.Controls.Add(tb);
tb.Select();
lb = new Label()
{
Text = "Password: ",
AutoSize = true,
TextAlign = ContentAlignment.MiddleCenter,
Left = lb.Left,
Top = lb.Bottom + 20,
ForeColor = Color.White,
Font = new Font("Arial", 10, FontStyle.Bold)
};
frm.Controls.Add(lb);
tb = new TextBox()
{
Name = "pass",
Width = tb.Right - (lb.Right + 2),
Left = lb.Right + 2,
Top = lb.Top,
ForeColor = Color.Black,
PasswordChar = '*',
TabIndex = 2,
};
frm.Controls.Add(tb);
Button btn = new Button()
{
Name = "UserAuthenticationBtn",
Width = 75,
Height = 30,
Left = tb.Right - 75,
Top = tb.Bottom + 15,
Text = "Login",
BackColor = default(Color),
UseVisualStyleBackColor = true,
TabIndex = 3,
};
btn.Click += new EventHandler(controlActions.btnActions.btnAuthorize);
frm.Controls.Add(btn);
btn = new Button()
{
Name = "Cancel",
Width = 75,
Height = 30,
Left = btn.Left - 85,
Top = tb.Bottom + 15,
Text = "Cancel",
BackColor = default(Color),
UseVisualStyleBackColor = true,
TabIndex = 4,
};
btn.Click += new EventHandler(controlActions.btnActions.btnCancle);
frm.Controls.Add(btn);
}
}
Any help with this would be great. I am sure I am just missing a simple detail somewhere.
Try this when you create the control:
tb.Focus();
tb.Select();

how to get return value from prompt input box? C# win Forms

I have created prompt input box in which user enter two values and press button and i want to return values on button click and get these values in other method.
Here is my code
public static string ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? textBox.Text : "";
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string promptValue = Form1.ShowDialog("Find What", "Replace");
}
I want to get values of textbox and textbox2 in other method.Thanks
Okay, just return an array with the two textbox values.
public static string[] ShowDialog(string text, string caption)
{
Form prompt = new Form()
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen
};
Label textLabel = new Label() { Left = 50, Top = 10, Text = text };
Label textLabel2 = new Label() { Left = 50, Top = 55, Text = text };
textLabel2.Text = "Replace with";
TextBox textBox = new TextBox() { Left = 50, Top = 70, Width = 200 };
TextBox textBox2 = new TextBox() { Left = 50, Top = 30, Width = 200 };
Button confirmation = new Button() { Text = "Replace", Left = 350, Width = 100, Top = 70, DialogResult = DialogResult.OK };
confirmation.Click += (sender, e) => { prompt.Close(); };
prompt.Controls.Add(textBox);
prompt.Controls.Add(textBox2);
prompt.Controls.Add(textLabel2);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(textLabel);
prompt.AcceptButton = confirmation;
return prompt.ShowDialog() == DialogResult.OK ? new string[] { textBox.Text, textBox2.Text } : null;
}
private void stringReplacedToolStripMenuItem_Click(object sender, EventArgs e)
{
string[] promptValue = Form1.ShowDialog("Find What", "Replace");
var textBoxValue = promptValue[0];
var textBox2Value = promptValue[1];
}

How do I set it so the text box is selected when prompt is called?

public int dialog()
{
Form prompt = new Form(); // creates form
//dimensions
prompt.Width = 300;
prompt.Height = 125;
prompt.Text = "Adding Rows"; // title
Label amountLabel = new Label() { Left = 75, Top = 0, Text = "Enter a number" }; // label for prompt
amountLabel.Font = new Font("Microsoft Sans Serif", 9.75F);
TextBox value = new TextBox() { Left = 50, Top = 25, Width = prompt.Width / 2 }; // text box for prompt
Button confirmation = new Button() { Text = "Ok", Left = prompt.Width / 2 - 50, Width = 50, Top = 50 }; // ok button
confirmation.Click += (sender, e) => { prompt.Close(); }; // if clicked it will close
prompt.AcceptButton = confirmation; // enter
// adding the controls
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.Controls.Add(value);
prompt.ShowDialog();
int num;
Int32.TryParse(value.Text, out num);
return num;
}
This is what my prompt looks like when it's called
I just clicked a button to call that method. Now as you notice, the text box is not selected. How do I make it so that if this method is called, it will make the text box selected by default without having to click it or tab to it?
(I know this is minor but every detail would look nicer)
The order used to tab between controls is determined by the property TabIndex. This property is determined automatically by the order in which you add the controls (If you don't change it manually) The control with TabIndex = 0 will be focused at the opening of the form (Of course if the control could be focused)
Try with
prompt.Controls.Add(value);
prompt.Controls.Add(confirmation);
prompt.Controls.Add(amountLabel);
prompt.ShowDialog();
You mean Focused ? Like this:
textBox1.Focus();
Write this code after your show dialog,It should work.
prompt.ShowDialog();
prompt.Controls.OfType<TextBox>().First().Focus();
Or if it doesn't work try to set ActiveControl property before opening your prompt:
promt.ActiveControl = value;
prompt.ShowDialog();

Categories

Resources