winForms, help with converting text in txtbox to double if possible? - winforms

i have the below coding in winForms in 3 different btns.
I have very limited knowledge of winForms, as i mostly use Console applications, but i am trying to use text boxes and buttons and user input to create a program that calculates the price variations of a product.
basically i what i am struggling with is converting whatever is in a text box (usually doubles) to a given name to be used to perform calculations on and then append them to the next text box etc.
can anyone help me with this please? it would be much appreciated!
thank you!
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;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public Form1 salePrice { get; private set; }
public Form1 discountPrice { get; private set; }
public Form1 vat { get; private set; }
public Form1 onlyVat { get; private set; }
public Form1 totalPrice { get; private set; }
public Form1 changeGiven { get; private set; }
public Form1 payment { get; private set; }
private void calcPriceAndDiscount_Click(object sender, EventArgs e)
{
salePrice = PriceBox;
discountPrice = DiscountBox;
salePrice = (salePrice - discountPrice);
SubtotalBox.AppendText(String.Format("{0:c}", salePrice));
}
private void calcWithVat_Click(object sender, EventArgs e)
{
onlyVat = (salePrice / 100.00 * vat);
totalPrice = (onlyVat + salePrice);
totalPrice = FinalPriceBox;
vat = VATBox;
FinalPriceBox.AppendText(String.Format("{0:c}", totalPrice));
}
private void calcPaymentMinPrice_Click(object sender, EventArgs e)
{
changeGiven = (payment - totalPrice);
payment = PaymentBox;
ChangeGivenBox.AppendText(String.Format("{0:c}", changeGiven));
}
}
}

double dbl;
if (double.TryParse(TextBox1.Text, out dbl))
{
// dbl contains the value of the text
}
else
{
// The text could not be converted to a double
}
When you're done with the value:
TextBox1.Text = dbl.ToString();

Related

Bindingsource on combobox

I create the small winform to do test bing
Existing class as below
public class Book
{
public int Id { get; set; }
public string BookName { get; set; }
public int? CatalogID { get; set; }
}
public class BookCatalog
{
public int Id { get; set; }
public string CataLogName { get; set; }
}
and I create the form with two control, Textbox and Combobox
which initial as below
private Book BookRecord;
private List<BookCatalog> bookCatalogs;
private BindingSource BindingSource = new BindingSource();
public frmBook()
{
InitializeComponent();
// I have initial one bookrecord and multi bookCatalogs here
cbBookCatagories.DisplayMember = "CataLogName";
cbBookCatagories.ValueMember = "Id";
cbBookCatagories.DataSource = bookCatalogs;
BindingSource.DataSource = BookRecord;
txtBoxBookName.DataBindings.Add("Text", BindingSource, "BookName");
cbBookCatagories.DataBindings.Add("SelectedValue", BindingSource, "CatalogID");
}
The first run is fine, but when I want to clear BookRecord as below code
BookRecord.Id = 0;
BookRecord.BookName = null;
BookRecord.AuthorName = null;
BookRecord.CatalogID = null;
BindingSource.ResetBindings(false);
My combobox cannot change value and always point to zeroindex
can anyone guide me how to handle BindingSource() on comboxbox ?
Thank you

Why I couldn't call Model inside my Controller

I have one problem and I have no idea what to do. I am try couple of method but nothing works for me. I have model Patient
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Text;
namespace BergClinic.Models
{
public class Patient
{
[Key]
public int Id { get; set; }
[Required]
[Display(Name ="First Name")]
[MaxLength(50)]
public string FirstName { get; set; }
[Required]
[Display(Name = "Last Name")]
[MaxLength(50)]
public string LastName { get; set; }
[Required]
[Display(Name = "Date of Birth")]
public DateTime DateOfBirth { get; set; }
[Display(Name = "Address")]
[MaxLength(50)]
public string Address { get; set; }
[Display(Name = "Phone Number")]
public string PhoneNumber { get; set; }
[Display(Name = "Gender")]
public PatientGender Gender { get; set; }
}
public enum PatientGender
{
[Display(Name ="Male")]
Male,
[Display(Name = "Female")]
Female,
Unknown
}
}
And IPatientRepository and PatientRepository which contains following logic:
using BergClinic.DataAccess.Data;
using BergClinic.DataAccess.Repository.IRepository;
using BergClinic.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BergClinic.DataAccess.Repository
{
public class PatientRepository : Repository<Patient>, IPatientRepository
{
private readonly ApplicationDbContext _db;
public PatientRepository(ApplicationDbContext db) : base(db)
{
_db = db;
}
public void Update(Patient patient)
{
var objDb = _db.Patients.FirstOrDefault(s => s.Id == patient.Id);
if (objDb != null)
{
objDb.FirstName = patient.FirstName;
objDb.LastName = patient.LastName;
objDb.DateOfBirth = patient.DateOfBirth;
objDb.Address = patient.Address;
objDb.PhoneNumber = patient.PhoneNumber;
objDb.Gender = patient.Gender;
_db.SaveChanges();
}
}
}
}
And here is IPatientRepository which containt Update method
using BergClinic.Models;
using System;
using System.Collections.Generic;
using System.Text;
namespace BergClinic.DataAccess.Repository.IRepository
{
public interface IPatientRepository : IRepository<Patient>
{
void Update(Patient patient);
}
}
Once I creat this in my Admin Area in PatientController I want to initialize object Patient but I couldn't. I want to create Upsert method for Update and Insert Patient but whatever I type It doesn't see my model only what it is see is Patient workspace
'Patient' is a namespace but is used like a type
using BergClinic.DataAccess.Repository.IRepository;
using Microsoft.AspNetCore.Mvc;
using BergClinic.Models;
namespace BergClinic.Areas.Admin.Controllers
{
[Area("Admin")]
public class PatientController : Controller
{
private readonly IUnitOfWork _unitOfWork;
public PatientController(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
}
public IActionResult Index()
{
return View();
}
public IActionResult Upsert(int? Id)
{
Patient patient = new Areas.Patient
return View();
}
#region API_CALLS
[HttpGet]
public IActionResult GetAll()
{
var patient = _unitOfWork.Patient.GetAll();
return Json(new { data = patient });
}
#endregion
}
}
Just to notice I use RepositoryPatten and thee way arhitecture. I try to restart Visual Studio but seem nothing happened, I try to remove Project Reference and try to add again, but noting happened againg.
Here is couple of sceen of my ProjectStructures which you can check:

WPF Datagrid get data on CheckboxChecked-Event

I've got a datagrid with a checkbox, name and email.
On CheckboxChecked I want to copy the email into another list or string.
How do I get the specific value from the checked row?
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
object row = lbxCC1.SelectedItem;
int columnIndex = lbxCC1.Columns.Single(c => c.Header.Equals("eMail")).DisplayIndex;
String eMail = (lbxCC1.SelectedCells[columnIndex].Column.GetCellContent(row) as TextBlock).Text;
MessageBox.Show(eMail);
}
Edit (09.09.2016):
Maybe I should show you a bit more code.
public class Person
{
public string Nachname { get; set; }
public string Vorname { get; set; }
public string eMail { get; set; }
public string Abteilung { get; set; }
}
public static class PersonService
{
public static List<Person> ReadFile(string filepath)
{
var lines = File.ReadAllLines(filepath);
var data = from l in lines.Skip(1)
let split = l.Split(';')
select new Person
{
Nachname = split[1],
Vorname = split[2],
eMail = split[31],
Abteilung = split[4],
};
return data.ToList();
}
}
I call it with:
lbxCC1.DataContext = PersonService.ReadFile(#"C:\Test.csv");
As I'm building the columns from code behind, I guess I have to bind them aswell am I right?
Sorry for this, but I'm new to datagrids :-)
I think this might help you:
Dim row As Data.DataRowView = DirectCast([yourDataGrid].SelectedItems(rowIndex), Data.DataRowView)
Then in your CheckBox_Checked Event:
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
MessageBox.Show(row("email"); 'Assuming the column name is email
}
This is IF your values are data-bound to the DataGrid.

c# winforms binding not working

The following code works without a problem
using System;
using System.Windows.Forms;
namespace ClassLibrary1 {
using System.Diagnostics;
public class person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
person fred = new person();
private void button1_Click(object sender, EventArgs e)
{
fred.FirstName = "Fred";
this.textBox1.DataBindings.Add("Text", fred, "FirstName");
Debug.Assert(textBox1.Text == fred.FirstName); // true
}
} }
yet when i try a similar patttern to databind a class with many more properties, the assertion fails.
the textbox is blank even though the bound property has data.
what could i be doing wrong?
OOps, the pattern was not the same.
I was setting the DataBindings before the form was visible, and this seems to be the problem.

How to bind DataGridView with List<T> or BindingList<T>

I've done it one thousand of times and it works but now .... not :(
Am I doing something wrong here because nothing is shown in grid ?
namespace theGridIsNotWorking
{
using System;
using System.Collections.Generic;
using System.Windows.Forms;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
var items = new List<Item>();
items.Add(new Item(){ TheName = "first"});
items.Add(new Item(){ TheName = "Second"});
items.Add(new Item(){ TheName = "Third"});
dataGridView1.DataSource = new List<Item>(items);
}
public class Item
{
public string TheName;
}
}
}
Nothing spectaculos .... but really sad.
I think the problem is that TheName is a member variable, but you need a property.
Try the following for the Item class:
public class Item
{
public string TheName;
public string TheNameProperty
{
get
{
return TheName;
}
}
public Item(string name)
{
TheName = name;
}
}
Try BindingListView. Easiest way to bind a List<T> to a DGV.
BindingList<Notification>(notifications);
shouldn't it be
BindingList<Notification>(activeNotifications);
?

Resources