Clicking button to send info in text box not working - c#

I try to make this: static private long number = (long)Math.Floor(GlobalRandom.NextDouble * 9_000_000_000L) + 1_000_000_000L; to be returned with this:
public string MyValtwo
{
get { return myValtwo; }
set { myValtwo = value; }
}
and finally to be sent to this:
private void button1_Click(object sender, EventArgs e)
{
MyValtwo = textBox2.Text;
}
I tried converting: public string MyValtwo = Convert.ToString(number); but nothing returns or the long (number) can't be converted into string.
The main idea is to be generated a number and automatically putted in form 1 text box

You dont need MyValtwo. Change code to below and it will work:
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = number.ToString();
}
In case you want to store the value of number as text in MyValtwo then you can add below line in button1_Click function.
MyValtwo = number.ToString();

Related

How to delete selected text from textbox in c#(SWF)

I try to do a notepad in c#,
I have some problems at delete function,
I want to delete selected text...
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a;
a = textBox1.SelectionLength;
textBox1.Text.Remove(textBox1.SelectionStart,a);
}
what is wrong?
Remove will return the truncated string, so you just need to reassign to the TextBox:
private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
{
int a = textBox1.SelectionLength;
textBox1.Text = textBox1.Text.Remove(textBox1.SelectionStart,a);
}
Use SelectedText like this :
textbox1.SelectedText = "";

the total price in the datagrid when I passed it round that price

my English is not good because I'm Spanish so I'm using a translator if you do not understand something ask me.
my problem is that I'm doing a program with two windows in main window I have a datagrid in the second window I pass information with textbox, the problem is that by passing the total price by multiplying the amount I get a dessert for the price of a dessert, the total price in the datagrid when I passed it round that price, if the price happened 1.20 the program will change to 1.
gives me no error so I'll have to spend the entire program code sorry.
This is the second window
private void Window_Loaded(object sender, RoutedEventArgs e)
{
if (Application.Current.Properties["seleccionado"] == null)
{
textBox1.IsEnabled = false;
Postresinfo = new TabladePostre();
}
else
{
Postresinfo = (TabladePostre) (Application.Current.Properties["seleccionado"]);
textBox1.IsEnabled=false;
textBox1.Text = Convert.ToString(Postresinfo.refPostre);
textBox2.Text = Postresinfo.NombrePostre;
textBox3.Text = Convert.ToString(Postresinfo.cantidad);
textBox4.Text = Convert.ToString(Postresinfo.precio);
textBox5.Text = Convert.ToString(Postresinfo.preciototal);
}
LinqdePostresDataContext BasedeDatos;
string filename = "";
private void button1_Click(object sender, RoutedEventArgs e)
{
BasedeDatos(LinqdePostresDataContext)Application.Current.Properties["basedeDatos"];
Postresinfo.NombrePostre = textBox2.Text;
Postresinfo.cantidad = Convert.ToInt32(textBox3.Text);
Postresinfo.precio = Convert.ToDecimal(textBox4.Text);
Postresinfo.preciototal = Convert.ToDecimal(textBox5.Text);
Postresinfo.imagen = filename;
if (Application.Current.Properties["seleccionado"] != null)
{
Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);
}
else
{
BasedeDatos.TabladePostres.InsertOnSubmit(Postresinfo);
}
BasedeDatos.SubmitChanges();
this.Close();
}
decimal precio = 0;
private void button2_Click(object sender, RoutedEventArgs e)
{
precio = Convert.ToDecimal(textBox4.Text);
textBox5.Text = Convert.ToString(precio * Convert.ToDecimal(textBox3.Text));
}
private void button9_Click(object sender, RoutedEventArgs e)
{
// Configure open file dialog box
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.FileName = "Document"; // Default file name
dlg.DefaultExt = ".jpg"; // Default file extension
dlg.Filter = "Text documents (.jpg)|*.jpg"; // Filter files by extension
// Show open file dialog box
Nullable<bool> result = dlg.ShowDialog();
// Process open file dialog box results
if (result == true)
{
// Open document
filename = dlg.FileName;
ImageSourceConverter conversor = new ImageSourceConverter();
image1.Source = (ImageSource)conversor.ConvertFromString(filename);
}
}
this is the main window:
LinqdePostresDataContext BasedeDatos = new LinqdePostresDataContext();
private void activar(object sender, RoutedEventArgs e)
{
Cargartabla();
}
private void Cargartabla()
{
var postre = (from n in BasedeDatos.TabladePostres
select n);
dataGrid1.ItemsSource = postre;
}
private void button1_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["seleccionado"] = null;
Ventana2 Ventana2 = new Ventana2();
Ventana2.Show();
}
private void button2_Click(object sender, RoutedEventArgs e)
{
BasedeDatos.TabladePostres.DeleteOnSubmit((TabladePostre)dataGrid1.SelectedItem);
BasedeDatos.SubmitChanges();
Cargartabla();
}
private void Activar2(object sender, EventArgs e)
{
Cargartabla();
}
private void button3_Click(object sender, RoutedEventArgs e)
{
Application.Current.Properties["seleccionado((TabladePostre)dataGrid1.SelectedItem);
Application.Current.Properties["basedeDatos"] = BasedeDatos;
Ventana2 ventana2 = new Ventana2();
ventana2.Show();
}
If you need to know also I have a database with int price, int quantity in the total price in decimal.
thanks for reply, I tried both options have given me but does not work, these are the faults:
Postresinfo.refPostre = float.Parse (textBox1.Text) gives me no errors, the program runs normally and nothing changes
float.TryParse (textBox1.Text, out Postresinfo.refPostre) has these faults:
Error 1 A property, indexer or dynamic member access May not be passed as an out or ref parameter.
Error 2 The best overloaded method match for 'float.TryParse (string, out float)' has some invalid arguments
Error 3 Argument 2: can not convert from 'out int' to 'float out'
I tried the other code and nothing changes, the program runs normally
shane now, I tried this code:
Convert.ToDecimal (textBox1.Text);
but it changes nothing and runs normally.
I've also tried the other code, but nothing changes and the program runs normally
the fault is not textbox1, I think it's because it's in that TextBox5 textbox where I enter the price in decimal and passes it to the datagrid and is the rounded
I'll also attach the column fails me:
<DataGridTextColumn Binding="{Binding Path=preciototal}" Header="Precio Total"/>
thanks.
you got a lot of code here and i'm not sure what is the problem. i understand that you get round value but it'll be helpful to mention the name of the textBox that it happens in it.
i did saw this code:
Postresinfo.refPostre=Convert.ToInt32(textBox1.Text);
in button1_Click which will cause rounding if the value in textBox1.Text is float.
you should do
Postresinfo.refPostre=float.Parse(textBox1.Text);
or
float.TryParse(textBox1.Text, out Postresinfo.refPostre);
because it's a textbox and you might get a value which isn't a number.
if you decide to use Parse then you should do
try
{
Postresinfo.refPostre=float.Parse(textBox1.Text);
}
catch
{
// Show a message or write to log or simething
}
Your problem is that you are converting a price like "1.20" to an int with Convert.ToInt32(textBox1.Text);.
Try change Postresinfo.refPostre to a decimal and use Convert.ToDecimal(textBox1.Text); so use something like this as a FormatException could be thrown:
try
{
Convert.ToDecimal(textBox1.Text);
}
catch (Exception)
{
//Deal with Error
}

displaying data from selected rows in datagridview into a textbox

Im trying to display data from datagridview into a textboxes of another form. The form 2 has textboxes. Here is my code:
private void btnAddOrder_Click(object sender, EventArgs e)
{
Add_Order addOrder = new Add_Order();
addOrder.
}
Im trying to type addOrder.textBox1.Text = dtgv_Items.SelectedRows[0].Cells[1].Value.ToString(); but textBox1 that should automatically appear is not displaying, meaning there's an error. How could I solve this? :)
Here is an example for any selected cell:
Form2:
private string pVal;
//getter and setter
public string PassVal
{
get { return pVal; }
set { pVal = value; }
}
//or event that you need
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = pVal;
}
Form1:
Form2 f2 = new Form2();
int selectedCellCount = dataGridView1.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > 0)
{
for (int i = 0; i < selectedCellCount; i++)
{
int column = dataGridView1.SelectedCells[i].ColumnIndex;
int row = dataGridView1.SelectedCells[i].RowIndex;
f2.PassVal = dataGridView1[column, row].Value.ToString();
}
}
f2.Show();
with this peace of code you'll get text from any selected cell in your textbox.
hope it helped you a little bit.
First, create getter and setter on the second form.
private string sampleData = string.Empty;
public string SampleData
{
get { return sampleData; }
set { value = sampleData; }
}
Then you can use this on your first form:
private void btnAddOrder_Click(object sender, EventArgs e)
{
Add_Order addOrder = new Add_Order();
addOrder.SampleData = dtgv_Items.SelectedRows[0].Cells[1].Value.ToString();
}
To view the result on your second form, use this:
this.TextBox1.Text = SampleData;
use the textbox it self direct
textBox1.Text = dtgv_Items.SelectedRows[0].Cells[1].Value.ToString();

How do I prepend text from one textbox to another?

Basically I'm making a simple program to help take notes at my job. I have a one line textbox1, and a multiple line textbox2.
I want to be able to type whatever in textbox1, and then press "enter" and it show up in the first line in textbox2. Any help would be appreciated.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textbox1_TextChanged(object sender, EventArgs e)
{
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
}
//in form constructor, or InitializeComponent method
textBox1.Validated += DoValidateTextBox;
//in another place of your class
private void DoValidateTextBox(object sender, EvenArgs e) {
textBox2.Text = ((TextBox)sender).Text + Environment.NewLine + textBox2.Text;
}
This should work:
private void textBox1_KeyDown(object sender, KeyEventArgs e) // Keydown event in Textbox1
{
if (e.KeyCode == Keys.Enter) // Add text to TextBox2 on press Enter
{
textBox2.Text += textBox1.Text;
textBox2.Text+= "\r\n"; // Add newline
textBox1.Text = string.Empty; // Empty Textbox1
textBox1.Focus(); // Set focus on Textbox1
}
}
If you want to add text at the firstline of your textbox, then replace in the code above:
textBox2.Text = textBox1.Text + "\r\n" + textBox2.Text;
It depends on what you want the final result to be. If all you want is the first line of the second textbox to equal the first then:
void myEvent()
{
textbox2.Text = textbox1.Text;
}
If however you want whatever is in textbox1 to be appended to textbox2 every time you press a button, then you are better off using a ListView:
void myEvent()
{
myListView.Items.add(textbox1.Text);
}
If you specifically want a textbox though (with the data always appended to the first line):
void myEvent()
{
textbox2.Text = textbox1.Text + Environment.NewLine + textbox2.Text;
}

Windows Forms: 'Input string was not in a correct format' error

I am creating a webpage re-loader and I am trying to get number of reload using input from user but I am not able to get number of input from user.
I am trying to get user input in textBox2.Text, but I am having this error:
input string was not in a currect format
This error is in this line kkk = System.Int32.Parse(textBox2.Text);
please help me how to get user input properly in an int value.
this is my program code:
public partial class Form1 : Form
{
public int kkk;
public Form1()
{
InitializeComponent();
}
private void progressBar1_Click(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (progressBar1.Value != kkk)
{
do
{
try
{
webBrowser1.Navigate(textBox1.Text);
while (webBrowser1.ReadyState != WebBrowserReadyState.Complete)
{
Application.DoEvents();
if(webBrowser1.ReadyState == WebBrowserReadyState.Complete)
{
progressBar1.Value = progressBar1.Value + 1;
}
}
MessageBox.Show("Loaded");
}
catch(Exception)
{
MessageBox.Show("failed");
}
}
while(progressBar1.Value !=kkk);
}
}
private void Form1_Load(object sender, EventArgs e)
{
kkk = System.Int32.Parse(textBox2.Text);
progressBar1.Maximum = kkk;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
In your form load event you take the contents of textbox2. text and assign it to kkk. But at that point nothing is inside textBox2 so it throws the error, and rightfully so since the textbox is empty how can it parse to an Int32 if it has no value?
You should assign the value of kkk at some later time during the process. You can always handle the exception before it happens:
int number;
bool result = Int32.TryParse(txtBox2.Text, out number);
if (result)
{
//good conversion you can use number
}
else
{
//not so good
}
But again you are doing this at form load event and I highly doubt there is anything in that textbox based on your code by the time the load event is finished.
The line:
kkk = System.Int32.Parse(textBox2.Text);
is giving an error maybe because it is an empty string which is unable to get parsed to integer. Change it to:
kkk = textBox2.Text.Trim();
if( kkk.Length > 0 ) {
try {
kkk = System.Int32.Parse(kkk);
}
catch { }
}

Categories

Resources