Passing Value From Form to Textbox - c#

The title is a little vague. I have a form with several textboxes that require user input. The input is all numerical. This is a touchscreen application, so when the textbox gets focus, a "numberpad" form is displayed for the user to input the number. The user's input is displayed on the "numberpad" form.
The question: How do I get that input to be set as the text property of the calling textbox?
I know I could pass some int value then use a big switch statement when the value is to be passed, but there are around 30 textboxes. Any ideas?

In your NumberPad form, have a property that is your result:
public int Result { get; private set; }
When the user hits the button to save the data, assign the value and set the DialogResult for the form:
private void btnSave_Click(object sender, EventArgs e)
{
Result = // whatever
DialogResult = DialogResult.OK;
}
In the calling form, check the result and only process if it is OK (in other words, the NumberPad was saved and not cancelled):
NumberPad pad = new NumberPad();
if (pad.ShowDialog() == DialogResult.OK)
{
txtBox.Text = pad.Result.ToString();
}

Related

Get a value from another Form c#

I am trying to get a value from another form in a text box. When I click on a text box, another form appears that is a numeric keypad and I can enter a number.
What I want is for the textbox I click to get the value of that new form.
How could I do this?
Example:
I have the next textbox:
When I do click on this textbox appears the following form to introduce some numbers:
When I click on the enter button of the second form, I would like to close this form and get the value "78552" into the first textbox.
I am trying putting the textbox of the second form as a public static but is not working.
What could I do?
EDIt:
This is what I am trying:
private void micrasmin_Click(object sender, EventArgs e)
{
Tecladojm t = new Tecladojm();
t.Show();
using (var form = new Tecladojm())
{
//var result = form.ShowDialog();
if (Tecladojm.buttonEnterClicked == true)
{
string val = form.DevolverNumeroMarcado();
micrasmin.Text = val;
}
}
}
There are a number of ways to accomplish this, however I usually do the following.
1) Update your constructor in Form2 to include a parameter as so:
public Form2(string form1Text1, string form1Text2, string form1Text3)
{
InitializeComponent();
this.label1.Text = form1Text;
}
2) In your button click that opens the second form, simply do the following:
private void button1_Click(object sender, EventArgs e)
{
var form2 = new Form2(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text);
form2.Show();
}
That should work for you. Let me know if you have any questions.

Windows Form Application - C# Getting User Input

I'm using C# to make a Windows Form Application. I have a form with 4 labels, 4 textboxes, and 3 buttons. The only textbox that can have anything entered inside of it is the 1 first box, the rest have TapStop = false. I want the user to be able to enter a bunch of numbers in that 1st textbox and I want those numbers to get added to get the average and total. I already have a button that the user can push once they enter something. What I'm trying to do it similar to a calculator. User enters 1 number, then another, then another so on. I want to take all of those numbers and get the total, the amount of numbers they entered, and the average. I'm having trouble getting all of those numbers ready to be calculated. I created an event handler for the Add button and declared variables for the average and total. I'm having trouble with these 2 things:
1- I'm not sure how to get the input every time the user enters something.
2- I'm not sure how to convert the numbers the user enters since default is string in text boxes.
Tried to be as specific as I could since I don't have any code besides the event handler I made and the variables I declared because I'm not sure what to do next.
Any suggestions for these 2 things? Thanks.
1- I'm not sure how to get the input every time the user enters something.
If you want to update your values based on user input without pushing the button you can use the TextChanged event, where to find it? just double click on the textbox in the designer, then everytime the user change the text in this textbox this event will fire.
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
2- I'm not sure how to convert the numbers the user enters since default is string in text boxes.
int value = Convert.ToInt32(textBox1.Text);
EDIT
so based on your comment, here is my solution.
//The entered values will be stored as List of integars.
List<int> enteredValues = new List<int>();
//The button will store each value in the List of integers.
private void button1_Click(object sender, EventArgs e)
{
int value;
if(int.TryParse(textBox1.Text,out value))
{
enteredValues.Add(value);
}
else
{
//Show error message here.
}
}
//This button will calculate the sum of entered values.
private void button2_Click(object sender, EventArgs e)
{
int totalValues = 0;
foreach(int val in enteredValues)
{
totalValues += val;
}
}

How to Validate for Empty TextBoxes in Windows Forms Application?

I'm new and just started trying out Windows Forms and I've managed to come up the following code for checking for empty textbox:
public partial class form : Form
{
private string results;
}
private void button1_Click(object sender, EventArgs e)
{
results = "";
results += textBox1.Text;
results += textBox2.Text;
...
If (textBox1.Text == "") //I want to take this 'if' statement and turn it into a separate method called checkEmptyFields()
{
MessageBox.Show("This field cannot be left blank.");
textBox1.Focus();
}
else { MessageBox.Show(results, "This is the results window");}
}
Now, my problem comes when I will have 10+ textboxes (such as First Name, Last Name, Address, Email, etc.) that I will need my program to check if their fields have been left empty. How do I take the 'if' statement and make that into another separate method something like checkEmptyFields()?
When I press the button1 to submit the form, that's when I want the checkEmptyFields() to trigger to check for any empty fields in textboxes and display an error message to warn the user.

Handling data between multiple Forms

I am working on a program that generates a PDF file. Before the final generation of the file, I want to give the user the option to edit a portion of the file (The title of the graph that is about to be created). I want this to show up in a new form when the user clicks a button to export the PDF. Here is an outline of what I am trying to do...
private void button4_Click(object sender, EventArgs e) // Test PDF Code!
{
Form2 NewPDF = new Form2(chart3.Titles[chart3.Titles.IndexOf("Header")].Text.ToString().Substring(0, chart3.Titles[chart3.Titles.IndexOf("Header")].Text.ToString().Length - 4));
NewPDF.Show();
if (NewPDF.Selected == true)
{
// Create PDF, open save file dialog, etc
}
}
And here is the Form that is being opened by this button click...
public partial class Form2 : Form
{
public bool Selected
{
get;
set;
}
public String GraphName
{
get;
set;
}
public Form2(String FileName)
{
InitializeComponent();
textBox1.Text = FileName;
GraphName = FileName;
Selected = false;
}
public void button1_Click(object sender, EventArgs e)
{
GraphName = textBox1.Text;
this.Selected = true; // After the button is selected I want the code written above to continue execution, however it does not!
}
}
As of now, when I click on the button in Form2, nothing happens, there is something about the communication between the two Forms that I am not understanding!
You should change your Form2.GraphName like below
public String GraphName
{
get { return textBox1.Text }
}
then change your new Form2 creation like below, test it since I haven't run this through VS, but should work :)
private void button4_Click(object sender, EventArgs e) // Test PDF Code!
{
// why on earth were you doing .Text.ToString()? it's already string...
Form2 NewPDF = new Form2(chart3.Titles[chart3.Titles.IndexOf("Header")].Text.Substring(0, chart3.Titles[chart3.Titles.IndexOf("Header")].Text.Length - 4));
// show as a dialog form, so it will wait for it to exit, and set this form as parent
NewPDF.ShowDialog(this);
if (NewPDF.Selected == true)
{
// get the name from the other form
string fileName = NewPDF.GraphName;
// Create PDF, open save file dialog, etc
}
}
The answer to your problem is quite simple.
NewPDF.Show();
Show() does not pause execution of the calling form. Therefore, the check underneath that that verifies the Selected property if true will never execute properly, since that check is reached and verified just as the form starts appearing. ShowDialog() does pause execution and waits for the called form to close.
That aside; I would recommend one of two other ways to communicate between forms;
Use a global variable. Declare a variable holding the graph's name somewhere in a public module. Call the dialog that asks the user to input a name with ShowDialog(), since that pauses execution of the calling form until the called form returns a result.
if(Form.ShowDialog() == DialogResult.OK) {
// Save pdf, using title in global variable
}
Make sure to set the DialogResult in the called form before Close()-ing it.
Pass an instance variable of the calling form to the called name-input form to the constructor and save it. That way, if you expose the graph name property as a public property, you should be able to access it from the called form in the code that closes the form, which is your:
public void button1_Click(object sender, EventArgs e)
{
callingFormInstance.GraphNameProperty = textBox1.Text;
Close();
}
Hope that helps. Cheers!

Get the currently focused textbox in C#

I have two textboxes, and a button. When I press the button, I want to know where my current caret is (either of the two boxes). I need this to know where to insert a certain text. I tried textbox1.Focused; textbox1.enabled but neither worked. How should I implement this? Thanks
Keep in mind that when you click the button, your textboxes will no longer have focus. You'll want a method of keeping track of what was in focus before the button's click event.
Try something like this
public partial class Form1 : Form
{
private TextBox focusedTextbox = null;
public Form1()
{
InitializeComponent();
foreach (TextBox tb in this.Controls.OfType<TextBox>())
{
tb.Enter += textBox_Enter;
}
}
void textBox_Enter(object sender, EventArgs e)
{
focusedTextbox = (TextBox)sender;
}
private void button1_Click(object sender, EventArgs e)
{
if (focusedTextbox != null)
{
// put something in textbox
focusedTextbox.Text = DateTime.Now.ToString();
}
}
}
There's a very simple way to do this.
Your requirement is simple since you only have two textboxes.
You can assign a class-wide string variable that holds when textbox1_GotFocus() is invoked as well as textbox2_GotFocus().
So if that textbox GotFocus() is called you assign a value.
Then put a condition for the class-wide string variable in the button that if the class-wide variable has a value of thats kind, that textbox is populated whatever you want to put in the textbox.
It worked for me so I believe it should work on you.

Categories