Windows Forms Textbox Enter key - c#

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

Related

Detect the Text of multi textBoxes

I have program with multi methods .
We create all the controls with methods .
One of methods is for creating textBox . It is like :
private TextBox textBox1;
public void CreateTextBox()
{
this.textBox1 = new System.Windows.Forms.TextBox();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(100, Position);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
Position += 30;
this.Controls.Add(this.textBox1);
}
There are several textBoxes in a form (the count of textbox might change between 10 to 20 ) .
So , If I want to create several textBoxes , Call methods like :
CreateTextBox();
CreateTextBox();
CreateTextBox();
If I want to have the Text of this textboxes , A code like this return me the last textBox Text :
MessageBox.Show(textBox1.Text);
My problem is ,,,, How can I detect the text of first called of CreateTextBox() and second called of CreateTextBox() ?
Thank u for read
You can use an array containing all TextBoxes:
var form = new Form();
var boxes = new TextBox[10];
for (int i = 0; i < boxes.Length; i++)
{
var box = new TextBox();
box.Location = new Point(10, 30 + 25 * i);
box.Size = new Size(100, 20);
form.Controls.Add(box);
boxes[i] = box;
}
var button = new Button();
button.Text = "Button";
button.Click += (o, e) =>
{
var message = String.Join(", ", boxes.Select(tb => tb.Text));
MessageBox.Show(message);
};
form.Controls.Add(button);
Application.Run(form);

change the Label text in runtime inside a panel

I am creating List of Panel. Each panel Contain a Label and Button . I have also a Button(button1). I want to change the label text of (panels[0]) when click button1. How can I do this.This is my c# code:
List<Panel> panels = new List<Panel>();
private void Panel()
{
var x = 0;
for (int i = 1; i < 5; i++)
{
x += 60;
var panel1 = new Panel() { Size = new Size(60, 140), Location = new Point(x, 100), BorderStyle = BorderStyle.FixedSingle };
panel1.Name = "pan" + i;
Label lbl = new Label();
lbl.Name = "lbl" + i;
lbl.Text = i.ToString();
lbl.Location = new Point(10, 20);
panel1.Controls.Add(lbl);
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
panel1.Controls.Add(button);
panels.Add(panel1);
Controls.Add(panel1);
}
}
private void button1_Click(object sender, EventArgs e)
{
foreach (var p in panels)
{
}
}
Output:
But i want, When i Click button1 it will change Label text of zero index panels(I have pointed it using red mark).
Can anyone help me...
Ok, so you've got a button and a label within a panel. When you click a button of a panel, you wanna do something to the label in the same panel, right ?
So
private void BtnClick(object sender, EventArgs e) {
var button = (Button)sender;//you've got the button clicked
var panel = button.Parent;//you've got the panel.
//var label = panel.Controls.OfType<Label>().FirstOrDefault();//but don't think you get this in c# 3.0
var label = GetFirstLabel(panel);
if (label != null)
label.Text = "something";
}
private Label GetFirstLabel(Control parent) {
foreach (var control in parent.Controls) {
if (control is Label) return control as Label;
}
return null;
}
Usage
When you add your buttons, you can now do
Button button = new Button();
button.Location = new Point(10, 90);
button.Size = new Size(40, 40);
button.Text = "Click";
button.Click += BtnClick;
And this will work on all panels.
If you don't store reference to that Label then you can find it in controls of the first Panel by type for example:
panels[0].Controls.OfType<Label>().First().Text = "New Text";
or by name
panels[0].Controls.OfType<Label>().Single(l => l.Name == "lbl1").Text = "New Text";
you can do this with a simple method:
private void button1_Click(object sender, EventArgs e)
{
Label l = panels[0].Controls.Find("lbl1", false).FirstOrDefault() as Label;
l.Text = "TEXT";
}

Ok and Cancel Button Not showing

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.

c# use object before it's created

I dynamic create and trackbar on an event,
now i want a textbox tto be filled with the value of the trackbar.
but how am i possible to do that? since i'll get an error saying the dynamic created trackbar does not exist. which is logic
this is what i have so far.
TrackBar trackBar = new TrackBar();
trackBar.Name = "TrackbarWidth" + trackbarName++;
trackBar.Tag = "dispose";
trackBar.Maximum = 85;
trackBar.Minimum = 65;
trackBar.SmallChange = 5;
trackBar.TickFrequency = 5;
trackBar.Value = WidthValue;
trackBar.Location = new Point(175, 440 + (50 * trackbarName));
trackBar.Size = new System.Drawing.Size(100, 25);
this.Controls.Add(trackBar);
TextBox textBox = new TextBox();
textBox.Name = "TrackbarWidth" + TextboxName++;
textBox.Text = trackBar.Value.ToString();
textBox.Tag = "dispose";
textBox.Location = new Point(300, 440 + (50 * TextboxName));
textBox.Size = new System.Drawing.Size(30, 25);
this.Controls.Add(textBox);
lineWidth += 4;
}
#endregion
}
private void trackBar1_Scroll(object sender, EventArgs e){
textBox1.Text = trackBar1.Value.ToString();
}
The problem with this solution is that I cannot access the textbox or the trackbar in the trackBar1_Scroll method.
The easiest solution here to use use an anonymous event handler that is capable of closing over the two variables that you need. Include this just after you finish constructing the textbox:
this.Controls.Add(textBox);
trackBar.Scroll += (s, args) => {
textbox.Text = trackbar.Value.ToString();
};
The sender argument is always the control which triggered the event:
private void trackBar_Scroll(object sender, System.EventArgs e)
{
// TextBox also dynamic? One way is using ControlCollection.Find
textBox1 = this.Controls.Find("textBox1", true).FirstOrDefault() as TextBox;
if(textBox1 != null)
textBox1.Text = trackBar1.Value.ToString();
}
However, if you create it dynamically you also have to create the event handler:
TrackBar trackBar = new TrackBar();
trackBar.Scroll += this.trackBar_Scroll;

Centering OK button within a MessageBox in winforms

I am learning C#, and as a part of it I wrote small app that consists of form with two groups of buttons, two buttons each. Button for closing of app is added as well. And everything works ok. Also there is MessageBox, showing that the app is going to be closed. And here is little problem that bugs me: OK button within that MessageBox is NOT centered horizontally. I guess there is a method to align that button, but what puzzles me is why it's is not centered by default? As illustration here are screenshots:
Here is code as well:
using System;
using System.Drawing;
using System.Windows.Forms;
public class myForm : Form
{
private GroupBox gboxGrp1;
private GroupBox gboxGrp2;
private RadioButton butn1a;
private RadioButton butn1b;
private RadioButton butn2a;
private RadioButton butn2b;
private Button btnClose;
public myForm()
{
InitializeComponent();
}
private void InitializeComponent()
{
this.btnClose = new Button();
this.gboxGrp1 = new GroupBox();
this.gboxGrp2 = new GroupBox();
this.butn1a = new RadioButton();
this.butn1b = new RadioButton();
this.butn2a = new RadioButton();
this.butn2b = new RadioButton();
//myForm
this.Text = "My Form";
this.StartPosition = FormStartPosition.CenterScreen;
this.Height = 350;
this.Width = 200;
//btnClose
this.Controls.Add(btnClose);
this.btnClose.Text = "Close";
this.btnClose.Location = new Point(60, 260);
this.btnClose.Click += new EventHandler(btnClose_Click);
//gboxgrp1
this.gboxGrp1.Location = new Point(20, 20);
this.gboxGrp1.Text = "Group Box 1";
this.gboxGrp1.Width = 150;
this.gboxGrp1.Height = 100;
//gboxgrp2
this.gboxGrp2.Text = "Group Box 2";
this.gboxGrp2.Location = new Point(20, 130);
this.gboxGrp2.Width = 150;
this.gboxGrp2.Height = 100;
//Radio buttons
this.butn1a.Text = "Radio 1a";
this.butn1a.Location = new Point(30, 30);
this.butn1a.Size = new Size(90, 15);
this.butn1b.Text = "Radio 1b";
this.butn1b.Location = new Point(30, 60);
this.butn1b.Size = new Size(90, 15);
this.butn2a.Text = "Radio 2a";
this.butn2a.Location = new Point(30, 30);
this.butn2a.Size = new Size(90, 15);
this.butn2b.Text = "Radio 2b";
this.butn2b.Location = new Point(30, 70);
this.butn2b.Size = new Size(90, 15);
//Controls
this.Controls.Add(gboxGrp1);
this.Controls.Add(gboxGrp2);
this.gboxGrp1.Controls.Add(butn1a);
this.gboxGrp1.Controls.Add(butn1b);
this.gboxGrp2.Controls.Add(butn2a);
this.gboxGrp2.Controls.Add(butn2b);
}
private void btnClose_Click(object sender, EventArgs e)
{
MessageBox.Show("Closing Application");
Application.Exit();
}
}
public class MyApp
{
public static void Main()
{
Application.Run(new myForm());
}
}
You can't restyle the default MessageBox as that's completely depends on the windows OS theme. However you could create your own message box by simply creating a new form and then call it by using newMessagebox.ShowDialog();

Categories