How to change textbox text automatically when other textbox text changes? - c#

I have these textboxes named bags,rate,quantity,packing size and amount what i want to do is that when user enters bags and rate and packing size the quantity textbox should automatically shows the corresponding quantity and amount but in my case when i click on calculate button then it calculates and show the quantity and amount i have tried using textchanged event but it does not do the job?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Login
{
public partial class Sale : Form
{
SaleCalci sale;
SaleBillheader SaleHeaderModel = new SaleBillheader();
tbl_SaleBillDetails SaleDetailModel = new tbl_SaleBillDetails();
public Sale()
{
InitializeComponent();
}
private void Cancelbtn_Click(object sender, EventArgs e)
{
clear();
}
private void clear()
{
txtBillNo.Text = txtDesc.Text = "";
txtBags.Text = txtQty.Text = txtRate.Text = txtAmt.Text = "0.00";
if(txtQty.Text !=null && txtAmt.Text !=null )
{
txtQty.Text = "0.00";
txtAmt.Text = "0.00";
}
Savebtn.Text = "Save";
SaleHeaderModel.SaleBillHeaderId = 0;
SaleDetailModel.SaleBill_Id = 0;
}
private void Exitbtn_Click(object sender, EventArgs e)
{
var result = MessageBox.Show("Are you sure you want to close this form ?", "Confirm", MessageBoxButtons.YesNo);
if (result == DialogResult.Yes)
{ this.Close(); }
}
private void Sale_Load(object sender, EventArgs e)
{
ItemCombo();
PartyCombo();
PackingSizeCombo();
// clear();
}
private void ItemCombo()
{
UserDataEntities db = new UserDataEntities();
Itembox.DataSource = db.tbl_ItemId.ToList();
Itembox.ValueMember = "ItemId";
Itembox.DisplayMember = "ItemName";
}
private void PartyCombo()
{
UserDataEntities db = new UserDataEntities();
PartyBox.DataSource = db.tbl_Parties.ToList();
PartyBox.ValueMember = "Id";
PartyBox.DisplayMember = "PartyName";
}
private void PackingSizeCombo()
{
UserDataEntities db = new UserDataEntities();
PackingBox.DataSource = db.PackingSizes.ToList();
PackingBox.ValueMember = "PackingSizeId";
PackingBox.DisplayMember = "PackingSize1";
}
private void Savebtn_Click(object sender, EventArgs e)
{
CalculateAmount();
DisplayAmt();
}
private void CalculateAmount()
{
int bags = 0;
decimal rate = 0;
int pksize = 0;
bags = Convert.ToInt32(txtBags.Text);
rate = Convert.ToDecimal(txtRate.Text);
pksize = Convert.ToInt32(PackingBox.Text);
sale = new SaleCalci(bags,rate, pksize);
//sale.Bags = Convert.ToInt32(txtBags.Text);
//sale.Rate = Convert.ToDecimal(txtRate.Text);
//SaleDetailModel.Bags = int.Parse(txtBags.Text.Trim());
//SaleDetailModel.Qty = Convert.ToDecimal(txtQty.Text.Trim());
//SaleDetailModel.Rate = Convert.ToDecimal(txtRate.Text.Trim());
// SaleDetailModel.Amount = amount;
}
private void txtAmt_TextChanged(object sender, EventArgs e)
{
// txtAmt.Text = sale.CalucalteAmt.ToString();
}
private void Sale_Click(object sender, EventArgs e)
{
if ((txtBags.Text == "0.00") && (txtQty.Text == "0.00")&&(txtRate.Text == "0.00")&& (txtAmt.Text =="0.00"))
{
txtAmt.Clear();
txtBags.Clear();
txtQty.Clear();
txtRate.Clear();
}
}
private void txtQty_TextChanged(object sender, EventArgs e)
{
DisplayAmt();
}
private void txtBags_TextChanged(object sender, EventArgs e)
{
// sale.Bags = Convert.ToInt32(txtBags.Text);
// DisplayAmt();
}
private void PackingBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void txtRate_TextChanged(object sender, EventArgs e)
{
// DisplayAmt();
}
private void DisplayAmt()
{
decimal _amt = sale.CalucalteAmt;
txtQty.Text = sale.CalculateQty().ToString();
txtAmt.Text = _amt.ToString();
}
}
}

Normally "TextChanged" event fires autamatically if text value changes.
So here the problem is i think about your other "partial class" in which there has to exist eventhandler work. Something like:
txtBags.TextChanged += new EventHandler(txtBags_TextChanged);
Please check your other partial class if this staement exist.
This eventhandler sometimes disappears from project if you move your gui elements or for some other causes...
You can readd this statement manually.
By the way if you have not experience with the other partial class then you can try to remove these textboxes and re-add them then your problem will autamatically solves.

You need to call DisplayAmt in the TextChanged event of txtBags , txtRate and Size. In the code above, call to DisplayAmt is commented out. Instead, you are calling DisplayAmt in the TextChanged event of txtQty.
You should be, instead doing.
private void txtAmt_TextChanged(object sender, EventArgs e)
{
DisplayAmt();
}
private void txtRate_TextChanged(object sender, EventArgs e)
{
DisplayAmt();
}
Similarly, you need to add Changed event for Text Control for Size. The txtQty is updated by the DisplayAmt() method. So you don't necessarily need it, unless for reasons that are not specified in OP.

Related

Button can be enabled only if textbox AND datetimepicker is filled

[Edited Version based on replies]
I would like the button1 to be enabled only if both textbox and datetimepicker are filled. I am able to do for the text but not for datetimepicker. Here is my full code. FYI: my datetimepicker is being formatted to empty initially to allow user to choose a date himself.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Inspection
{
public partial class Vdetails : Form
{
public Vdetails()
{
InitializeComponent();
Vnew.Enabled = false;
}
//Allow user input date
private void DTafes_ValueChanged(object sender, EventArgs e)
{
DTafes.CustomFormat = #"";
}
private void DTfa_ValueChanged_1(object sender, EventArgs e)
{
DTfa.CustomFormat = #"";
}
private void DTfe_ValueChanged(object sender, EventArgs e)
{
DTfe.CustomFormat = #"";
}
//Condition to allow button to go next
private void ActivateButton()
{
Button1.Enabled = (Vehicle.Text != "" && Distance.Text != "");
}
private void Vehicle_TextChanged(object sender, EventArgs e)
{
ActivateButton();
}
private void Distance_TextChanged(object sender, EventArgs e)
{
ActivateButton();
}
}
What and how do I add the code to allow for datetimepicker to be filled. Hope that someone could help me.
The dateTimePicker has no textchanged-event. There will never be an empty string inside the dateTimePicker. (Maybe you can set it via code, but not by user-input) You can use the valueChanged-event of the dateTimePicker to do something when the date was changed by the user.
Since the text will never be empty, you dont have to check it in you ActiveButton-function.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void ActiveateButton()
{
this.button1.Enabled = textBox1.Text != string.Empty;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
ActiveateButton();
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
ActiveateButton();
}
}
DateTimePicker is always got a value but you can listen to the ValueChanged event
private bool dateChanged;
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
dateChanged = false;
button1.Enabled = false;
}
private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
{
dateChanged = true;
SetEnabledness();
}
private void SetEnabledness()
{
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text) && dateChanged;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetEnabledness();
}
If you have more than one DateTimePicker you can use a HashSet instead of a bool (just make sure you wire up all the ValueChangedEvents to dp_ValueChanged e.g.
private HashSet<DateTimePicker> dateChanged = new HashSet<DateTimePicker>();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
dateChanged.Clear();
button1.Enabled = false;
dateTimePicker1.Format = DateTimePickerFormat.Custom;
dateTimePicker2.Format = DateTimePickerFormat.Custom;
dateTimePicker3.Format = DateTimePickerFormat.Custom;
dateTimePicker1.CustomFormat = #" ";
dateTimePicker2.CustomFormat = #" ";
dateTimePicker3.CustomFormat = #" ";
}
private void dp_ValueChanged(object sender, EventArgs e)
{
var dp = (DateTimePicker) sender;
dp.Format = DateTimePickerFormat.Short;
dateChanged.Add(dp);
SetEnabledness();
}
private void SetEnabledness()
{
button1.Enabled = !string.IsNullOrWhiteSpace(textBox1.Text) && dateChanged.Count == 3;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
SetEnabledness();
}

Checking if the correct label was clicked?

to start of Im not good at programming and I am completely new to it. With that said, I am trying to make a game, where a pattern of labels show up (by the speciffic labels changing colors), and then the user has to click that speciffic pattern after it has been shown. I have already made the pattern show, and put into a list. The problem I now have is how I am going to check if the correct label was licked, acording to the random pattern that has been made. Sorry if my code seems clumsy, but here it is (sorry that there are no commemts yet also):
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Spil
{
public partial class Form1 : Form
{
Random rnd = new Random();
Label[] labelArray;
int turn = 1;
int lives = 3;
List<Label> orderList = new List<Label>();
public Form1()
{
InitializeComponent();
labelArray = new Label []{ label1, label2, label3, label4, label5, label6, label7, label8, label9 };
}
private void DisplayOrder()
{
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i].BackColor = Color.Blue;
}
for (int i = -2; i < turn; i++)
{
int chosenNumber = rnd.Next(0, 9);
labelArray[chosenNumber].BackColor = Color.Green;
Thread.Sleep(1000);
labelArray[chosenNumber].BackColor = Color.Blue;
orderList.Add(labelArray[chosenNumber]);
}
}
private void Click0(object sender, EventArgs e)
{
}
private void Click1(object sender, EventArgs e)
{
}
private void Click2(object sender, EventArgs e)
{
}
private void Click3(object sender, EventArgs e)
{
}
private void Click4(object sender, EventArgs e)
{
}
private void Click5(object sender, EventArgs e)
{
}
private void Click6(object sender, EventArgs e)
{
}
private void Click7(object sender, EventArgs e)
{
}
private void Click8(object sender, EventArgs e)
{
}
private void Click9(object sender, EventArgs e)
{
}
private void Form1_Shown(object sender, EventArgs e)
{
System.Timers.Timer t = new System.Timers.Timer(100);
t.Elapsed += t_Elapsed;
t.Start();
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
((System.Timers.Timer)sender).Stop();
DisplayOrder();
}
}
}
You can have all your labels registered for the same click event and use the sender parameter to identify the clicked label.
for (int i = 0; i < labelArray.Length; i++)
{
labelArray[i].BackColor = Color.Blue;
labelArray[i].Click += label_Click;
}
void label_Click(object sender, EventArgs e)
{
string name = ((Label)sender).Name;
}
You need to generate the Click even for each individual label,you can find the events tab here with the properties tab (in case you didn't know).Simply lick your label in the designer and navigate to the label_click event and double click it.

Do not allow a combobox dropdownlist to close when clicked outside of it

I am displaying all of the worlds countries in a combobox called countrybox.
A second combobox contains all of the cities in a country called citybox.
When you choose a country, citybox appears and when you open countrybox, citybox dissapears again.
The problem arises when you open citybox and click outside of it.
citybox dissapears when you
open countrybox and does not come back when you click outside of countrybox.
I tried with this:
string ctext { get; set; }
private void countrybox_SelectedIndexChanged(object sender, EventArgs e)
{
citybox.Visible = true;
string ctext = countrybox.Text;
}
private void countrybox_DropDownClosed(object sender, EventArgs e)
{
if (countrybox.Text == ctext)
{
citybox.Visible = true;
}
else
{
citybox.Visible = false;
}
However, this did not work as i wanted it to.
Im guessing it is because the Combobox class does not recognize a click outside of the box as a _DropDownClosed event.
I have also tried with using the
validating event to check if a user clicks on the form
private void countrybox_Validating(object sender, CancelEventArgs e)
{
if (string.Equals((sender as Form).Name, #"Form1") && string.IsNullOrEmpty(countrybox.Text))
{
e.Cancel = true;
MessageBox.Show("You have to select a country!");
}
}
Is there anyway to make the combobox dropdownlist to not close when clicked outside of the list?
I apologize if i have any Spelling misstakes, my native language is not English.
Posting my full code below for people who want more details.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] Countries = File.ReadAllLines(#"C:\Travelbureau\Countries.txt");
Array.Sort(Countries);
for (int i = 0; i < Countries.Length; i++)
{
countrybox.Items.Add(Countries[i]);
}
}
string ctext { get; set; }
private void countrybox_SelectedIndexChanged(object sender, EventArgs e)
{
citybox.Visible = true;
string ctext = countrybox.Text;
switch (countrybox.Text)
{
case "Afghanistan":
string[] AfgCity = File.ReadAllLines(#"C:\Travelbureau\Afghanistan.txt");
Array.Sort(AfgCity);
for (int i = 0; i < AfgCity.Length; i++)
{
citybox.Items.Add(AfgCity[i]);
}
break;
default:
citybox.Text = "City";
citybox.Items.Clear();
break;
}
}
private void countrybox_DropDown(object sender, EventArgs e)
{
citybox.Visible = false;
}
private void countrybox_DropDownClosed(object sender, EventArgs e)
{
if (countrybox.Text == ctext)
{
citybox.Visible = true;
}
else
{
citybox.Visible = false;
}
private void countrybox_Click(object sender, EventArgs e)
{
countrybox.Text = "";
citybox.Visible = false;
}
private void citybox_Click(object sender, EventArgs e)
{
citybox.Text = "";
}
private void countrybox_Validating(object sender, CancelEventArgs e)
{
if (string.Equals((sender as Form).Name, #"Form1") && string.IsNullOrEmpty(countrybox.Text))
{
e.Cancel = true;
MessageBox.Show("You have to select a country!");
}
}
}
}
It's been a while since I did anything with Windows Forms, but you should be able to accomplish this with the Validating event. If you check in that to see whether it has a value and set e.Cancel accordingly, I believe that will give you your desired behavior.
In my experience, it's a very good idea to set some sort of status visible to the user in that event, though. It can be confusing when a user is no longer able to click anywhere. You can do that with a MessageBox or a Label. But that's really a UX matter that isn't directly relevant to your question.
You might also have to set a property that indicates that it should validate when you leave the control, but I don't believe that's the case. If someone knows for sure I'll update this answer to reflect that.
And yes, you can change the visibility of cityBox in that method as well.

How can I make a reset button in my C# Windows Form?

I am trying to make a reset button my my C# Windows Form. But, when I have it clear the textboxes with a code like this:
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
textBox5.Clear();
textBox6.Clear();
then, it gives me the following error: "Input string was not in a correct format"
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;
namespace MidTermPizzas
{
public partial class Form1 : Form
{
pizzaOrder aOrder = new pizzaOrder();
public Form1()
{
InitializeComponent();
}
private void formTitle_Click(object sender, EventArgs e)
{
}
//click File, Exit
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Enjoy your pizza!");
this.Close();
}
//click View, Summary of Orders Placed
private void summaryOfOrdersToolStripMenuItem_Click(object sender, EventArgs e)
{
SummaryOfOrdersPlaced myForm = new SummaryOfOrdersPlaced();
myForm.Show();
}
//amount of Pizzas label
private void label1_Click(object sender, EventArgs e)
{
}
//form load
private void Form1_Load(object sender, EventArgs e)
{
}
//sales tax label
private void label3_Click(object sender, EventArgs e)
{
}
//text in box to the right of "Amount of Pizzas"
private void textBox1_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfPizzas = int.Parse(textBox1.Text);
}
//text in box to the right of "Amount of Cokes"
private void textBox2_TextChanged(object sender, EventArgs e)
{
aOrder.numberOfCokes = int.Parse(textBox2.Text);
}
//text in box to the right of "Sales Tax"
private void textBox4_TextChanged(object sender, EventArgs e)
{
}
//File Tool Strip Menu
private void fileToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//amount of cokes label
private void label2_Click(object sender, EventArgs e)
{
}
//reset button
private void button1_Click_1(object sender, EventArgs e)
{
//textBox1.Clear();
//textBox2.Clear();
//textBox3.Clear();
//textBox4.Clear();
//textBox5.Clear();
//textBox6.Clear();
}
//text in box to the right of "Amount Due"
private void textBox3_TextChanged_1(object sender, EventArgs e)
{
}
//text in box to the right of "Amount Paid"
private void textBox5_TextChanged(object sender, EventArgs e)
{
aOrder.getAmountPaid = double.Parse(textBox5.Text);
}
//click Calculate Change Due button
private void calculateChangeDue_Click(object sender, EventArgs e)
{
textBox6.Text = Convert.ToString(aOrder.GetChangeDue());
}
//text in box to right of Change Due
private void textBox6_TextChanged(object sender, EventArgs e)
{
}
//amount due label
private void label4_Click(object sender, EventArgs e)
{
}
//amount paid label
private void label5_Click(object sender, EventArgs e)
{
}
//change due label
private void label6_Click(object sender, EventArgs e)
{
}
//click Calculate Amount Due
private void calculateAmountDue_Click(object sender, EventArgs e)
{
textBox3.Text = Convert.ToString(aOrder.GetAmountDue());
}
//click Calculate Sales Tax
private void button2_Click(object sender, EventArgs e)
{
textBox4.Text = Convert.ToString(aOrder.TaxDue());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace MidTermPizzas
{
class pizzaOrder
{
public int numberOfCokes
{
get;
set;
}
public int numberOfPizzas
{
get;
set;
}
public double InputOrder()
{
const double COKE_PRICE = 1.49;
const double PIZZA_PRICE = 7.99;
double inputOrder = (numberOfCokes * COKE_PRICE) + (numberOfPizzas * PIZZA_PRICE);
return inputOrder;
}
public double TaxDue()
{
const double TAX = .073;
double taxDue = (this.InputOrder() * TAX);
return taxDue;
}
public double GetAmountDue()
{
double getAmountDue = this.InputOrder() + this.TaxDue();
return getAmountDue;
}
public double getAmountPaid
{ get; set; }
public double GetChangeDue()
{
double getChangeDue = this.getAmountPaid - this.GetAmountDue();
return getChangeDue;
}
}
}
private void textBox1_TextChanged(object sender, EventArgs e){
aOrder.numberOfPizzas = textBox1.Text == "" ? 0 : int.Parse(textBox1.Text);
}
Do the same for other textBoxes, you should use TryParse and also use 1 TextChanged event handler for all the textBoxes.
The following code uses TryParse and suppose if the parsing fails, the default value will be 0:
private void textBox1_TextChanged(object sender, EventArgs e){
int v;
if(int.TryParse(textBox1.Text, out v)){
aOrder.numberOfPizzas = v;
} else aOrder.numberOfPizzas = 0;
}
Either use TryParse as suggested by other answers or place 0 for text boxes used for numeric input in your clear method. Another approach is to use MaskedTextBox for text boxes used for numeric input.
Here you can use the manual method as well. Write the following code inside your button click function,
if (textBox1.Text != string.Empty || textBox2.Text != string.Empty || textBox3.Text != string.Empty || textBox4.Text != string.Empty || textBox5.Text != string.Empty || textBox6.Text != string.Empty)
{
textBox1.Text = "";
textBox2.Text = "";
textBox3.Text = "";
textBox4.Text = "";
textBox5.Text = "";
textBox6.Text = "";
}

Problem with appendText in c#

I have an app that has 3 text boxes, (users name, what company they are from, and who they are visiting) A button to print, and a keyboard on the screen (monitor is touch screen). I have everything working and functioning...
BUT, the one thing that does not work is when the user points to previous character in the text box that has already been typed, the buttons that "AppendText" (keyboard) do not start typing where the user pointed but it continues typing at the end of what has been typed.
Is this because of "AppendText" or some other issue that I have in my code?
I also am trying to get the first text box (Name_Box) to be sent to form one, which then will be split into two labels (1, first name| 2, last name) right now I have it being sent to one label But I want to split it so the first name is stacked above the second name in the next form (which is printed out).
Thank you so much.
Here is my code: First Form
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
namespace SMART
{
public partial class Form1 : Form
{
private TextBox tbSelected; // Last focused TextBox
private int posCaret; // Caret position
private int selLength; // Selection length
public Form1()
{
InitializeComponent();
// We will use leave event for textboxes
Name_Box.Leave += new System.EventHandler(textBox_Leave);
Company_Box.Leave += new System.EventHandler(textBox_Leave);
Visiting_Box.Leave += new System.EventHandler(textBox_Leave);
// Set initial selection to the first textbox
Name_Box.Select();
tbSelected = Name_Box;
posCaret = 0;
selLength = 0;
}
// Leave event handler
private void textBox_Leave(object sender, EventArgs e)
{
// Remember the last focused thextbox,
// the caret position in it and the selection length
tbSelected = (TextBox)sender;
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
// Helper method to restore selection
private void RestoreLastSelection()
{
tbSelected.Select();
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
private void Form1_Load(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
Form2 frm = new Form2(Name_Box.Text);
frm.Show();
frm.Close();
StreamWriter sw;
sw = File.AppendText ("C:\\SignIn.txt");
sw.WriteLine ("Date and Time: " + label5.Text + " | Name: " + Name_Box.Text + " | Company: " + Company_Box.Text + " | Visiting: " + Visiting_Box.Text + " |");
sw.Close ();
Name_Box.Clear();
Company_Box.Clear();
Visiting_Box.Clear();
}
private void button42_Click(object sender, EventArgs e)
{
//SPACE BAR
tbSelected.AppendText(" ");
}
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
private void button12_Click(object sender, EventArgs e)
{
tbSelected.AppendText("-");
}
private void button13_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Q");
}
private void button14_Click(object sender, EventArgs e)
{
tbSelected.AppendText("W");
}
private void button15_Click(object sender, EventArgs e)
{
tbSelected.AppendText("E");
}
private void button16_Click(object sender, EventArgs e)
{
tbSelected.AppendText("R");
}
private void button17_Click(object sender, EventArgs e)
{
tbSelected.AppendText("T");
}
private void button18_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Y");
}
private void button19_Click(object sender, EventArgs e)
{
tbSelected.AppendText("U");
}
private void button20_Click(object sender, EventArgs e)
{
tbSelected.AppendText("I");
}
private void button21_Click(object sender, EventArgs e)
{
tbSelected.AppendText("O");
}
private void button22_Click(object sender, EventArgs e)
{
tbSelected.AppendText("P");
}
private void button25_Click(object sender, EventArgs e)
{
tbSelected.AppendText("A");
}
private void button26_Click(object sender, EventArgs e)
{
tbSelected.AppendText("S");
}
private void button27_Click(object sender, EventArgs e)
{
tbSelected.AppendText("D");
}
private void button28_Click(object sender, EventArgs e)
{
tbSelected.AppendText("F");
}
private void button29_Click(object sender, EventArgs e)
{
tbSelected.AppendText("G");
}
private void button30_Click(object sender, EventArgs e)
{
tbSelected.AppendText("H");
}
private void button31_Click(object sender, EventArgs e)
{
tbSelected.AppendText("J");
}
private void button32_Click(object sender, EventArgs e)
{
tbSelected.AppendText("K");
}
private void button33_Click(object sender, EventArgs e)
{
tbSelected.AppendText("L");
}
private void button35_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Z");
}
private void button36_Click(object sender, EventArgs e)
{
tbSelected.AppendText("X");
}
private void button37_Click(object sender, EventArgs e)
{
tbSelected.AppendText("C");
}
private void button38_Click(object sender, EventArgs e)
{
tbSelected.AppendText("V");
}
private void button39_Click(object sender, EventArgs e)
{
tbSelected.AppendText("B");
}
private void button40_Click(object sender, EventArgs e)
{
tbSelected.AppendText("N");
}
private void button41_Click(object sender, EventArgs e)
{
tbSelected.AppendText("M");
}
private void button2_Click_1(object sender, EventArgs e)
{
tbSelected.AppendText("'");
}
private void button3_Click(object sender, EventArgs e)
{
tbSelected.Clear();
}
}
}
Heres is my code: Second Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace SMART
{
public partial class Form2 : Form
{
public Form2(string strTextBox)
{
InitializeComponent();
label3.Text = strTextBox;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
PrintDocument pd = new PrintDocument();
Margins margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
/*
//My sad attempt at splitting the Name
var fullname = strTextBox;
var names = fullname.Split (" ");
label3.Text = names[0];
label5.Text = names[1];
*/
}
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
this.DrawToBitmap(img, bounds);
Point p = new Point(0, 0);
e.Graphics.DrawImage(img, p);
}
}
}
You're right in that your problem is the use of AppendText, which always appends to the end (that's what append means).
You need to Insert the character at the current carat position.
You might do better to post a message that simulates a keypress from a physical keyboard.
If you want to insert text at the user's current position, you can use SelectedText. This will replace the current selection (if the user has selected characters):
tbSelected.SelectedText = "V";
Edit: The problem is in here:
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
You set the text, which returns the cursor to the beginning of the textbox. You should set tbSelected.SelectionStart after you clear the text.

Categories

Resources