i write this code for add in database .
public partial class Unit : UserControl
{
private ProductUnit _pu { get; set; }
public ProductUnit PU
{
get
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
return _pu;
}
set { }
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}
i need to get textbox.text value with backing field but when i need to add value in database this _pu is null .
whats the problem ???
You never initialize _pu, which should be a field and not a property by the way:
private ProductUnit _pu = new ProductUnit();
I don't really understand the purpose of the PU property though.
You might as well create a ProductUnit directly in the event handler:
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
var pu = new ProductUnit();
pu.UnitNicname = TxtUnitNicName.Text;
pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(pu);
}
Try this:
public partial class Unit : Form // <--------- Try this
{
private ProductUnit _pu= new ProductUnit();
public ProductUnit PU
{
get {return _pu;} // read only
}
public Unit()
{
InitializeComponent();
}
private void BtnAdd_Click(object sender, RoutedEventArgs e)
{
_pu.UnitNicname = TxtUnitNicName.Text;
_pu.UnitFaName = TxtUnitName.Text;
StructureMapDefenation.Container.GetInstance<IUnitService>().Add(_pu);
}
}
Related
I have this code:
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
}
}
}
Is there a way, how to create a new instance of Human whenever I click the Button(AddNewHuman_Click)?
After clicking few times on the button, there will still be only one Human h1, right?
You will have to create list of object to store multiple object of human class.
I do changed here for you. I hope it will work for you.
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public class Human
{
public string Name;
public Human(string name)
{
Name = name;
}
}
List<Human> objHumanList;
private void Form1_Load(object sender, EventArgs e)
{
objHumanList=new List<Human>();
textBox1.Text = "Jane";
}
private void AddNewHuman_Click(object sender, EventArgs e)
{
Human h1 = new Human(textBox1.Text);
objHumanList.add(h1);
/** Or
objHumanList.add (new Human(textBox1.Text))
**/
}
}
}
You can create a list of Humans and keep on adding a new Human to the list whenever the button is clicked.
So, I have two forms. The form1 has a button to open the form2. In the form2 I have a list of elements that I fill with elements I create in the form2. My problem is, when I close my form2 and reopen it, my list is empty. I know that is because Im initializing my list again (ListaComida = new List<Comida>();), so I get my data erased but I dont see how to solve this.
My code
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void addDia_Click(object sender, EventArgs e)
{
string dia = (DateTime.Today.ToString("dd/MM/yyyy"));
TabPage myTabPage = new TabPage(dia);
tabControl1.TabPages.Add(myTabPage);
}
private void AddComida_Click(object sender, EventArgs e)
{
FormAddComida addComida = new FormAddComida();
DialogResult resultaod = addComida.ShowDialog();
}
}
Form2
public partial class FormAddComida : Form
{
public List<Comida> ListaComida;
public FormAddComida()
{
InitializeComponent();
ListaComida = new List<Comida>();
}
private void addComidaAdicionar_Click(object sender, EventArgs e)
{
Comida comidaAdicionada = new Comida(tbNome.Text,
Convert.ToInt32(tbCalorias.Text),
Convert.ToInt32(tbHidratos.Text),
Convert.ToInt32(tbProteinas.Text),
Convert.ToInt32(tbGorduras.Text)
);
ListaComida.Add(comidaAdicionada);
RefreshListaComida();
}
private void RefreshListaComida()
{
lbListaComida.Items.Clear();
lbListaComida.Items.AddRange(ListaComida.ToArray());
}
private void AddComidaCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
}
You can use MemoryCache, even if you close your form your List will stay in Memory and you can retrieve by the Key. But if you need to save this data permanently (or long time running the app )i recommend you store in a DB.
using System.Runtime.Caching;
private ObjectCache cache = MemoryCache.Default;
public class Food
{
public string Name { get; set; }
public double Price { get; set; }
}
public void AddFood()
{
FoodList.Add(new Food { Name = "Pizza", Price = 10 });
FoodList.Add(new Food { Name = "Fries", Price = 5 });
cache.Add("UserCacheFood", FoodList, DateTimeOffset.MaxValue);
}
public List<Food> ReturnListFromCache()
{
return (List<Food>)cache.Get("UserCacheFood");
}
private void button1_Click(object sender, EventArgs e)
{
AddFood();
var result = ReturnListFromCache();
}
private void button2_Click(object sender, EventArgs e)
{
var ret2 = ReturnListFromCache();
}
I'm trying to make my own custom input dialogue by designing a form. How would I initialize it so that once I press OK, I can receive the value of the textbox in it, back to where I initially called it?
You can create a form that exposes a property like this:
public class InputDialog:Form
{
public string Result { get; set; }
private void OK_Click(object sender, EventArgs e)
{
Result = txtResult.Text;
this.Close();
}
}
And in your base form you do:
var dialog = new InputDialog();
dialog.ShowDialog();
string Result = dialog.Result;
You can use events for communication between forms. This way InputForm hides logic, properties from outside world.
public class InputEventArgs : EventArgs
{
public string Input { get; private set; }
public InputEventArgs(string input)
{
Input = input;
}
}
public class InputDialog : Form
{
public EventHandler<InputEventArgs> InputSet;
private void OkClick(object sender, EventArgs e)
{
var ev = InputSet;
if (ev != null)
{
ev(this, new InputEventArgs(txtInput.Text));
}
}
}
and in your calling form:
private void ShowInputForm()
{
using (var frm = new InputDialog())
{
frm.InputSet += (s, e) =>
{
txtResult.Text = e.Input;
}
frm.ShowDialog();
}
}
I have two windows, windowA that has a button to open windowB, and windowB has a button to close itself and also return List value. I tried this code, but the value keep null. windowB has RadGridView control, i want to get the selectedItem from it and add it on a list.
public class WindowA : Window
{
...
private void button_Click(object sender, RoutedEventArgs e)
{
WindowB winB = new WindowB();
if (winB.ShowDialog() == false)
{
listClass lc = winB.SelectedItemButton;
...
}
}
}
public class WindowB : Window
{
...
public listClass SelectedItemButton
{
get { return selectedItem; }
set
{
selectedItem = ((listClass)AGridView.SelectedItem);
}
}
private void button_Click(object sender, RoutedEventArgs e)
{
this.Close();
}
}
the results are a listClass, but has no value inside. Why? and how can i make selectedItem = ((listClass)AGridView.SelectedItem); this line works to another window?
An example for you:
public partial class MainWindow : Window
{
private void Button_Click(object sender, RoutedEventArgs e)
{
Window1 dlg = new Window1();
if(dlg.ShowDialog()??false)
{
MessageBox.Show(dlg.S);
}
}
}
// Dialog
public partial class Window1 : Window
{
public string S
{
get
{
return this.txt1.Text;
}
}
private void btnClose_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
}
you should create your listClass variable in Window1 and while you are opening the Window2 you should pass this variable as a parameter. Here is my demo:
First window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
TestClass testClass = new TestClass();
testClass.Test = "Initial";
Second second = new Second(testClass);
second.ShowDialog();
label.Content = testClass.Test; // It prints "Changed"
}
}
Second window:
public partial class Second : Window
{
TestClass testClass;
public Second(TestClass sent)
{
InitializeComponent();
testClass = sent;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
testClass.Test = "Changed"; // Change the value
}
}
My testClass (listClass):
public class TestClass
{
public string Test { get; set; }
}
I want to see if notch50hzbutton is checked in another form, something like: if (SettingsForm.notch50hzbutton.Checked == true) ..... How can I do this?
namespace ClassLibrary1
{
using GraficDisplay;
using GraphLib;
using PrecisionTimer;
public partial class SettingsForm : Form
{
public SettingsForm()
{
InitializeComponent();
notch50hzbutton.Checked = false;
notch60hzbutton.Checked = true;
}
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
}
}
}
public bool Notch50HzIsChecked
{
get { return notch50hzbutton.Checked; }
set { notch50hzbutton.Checked = value; }
}
You may then access it like a regular property from outside the class.
Make a public property on the form and pass through the value you want to access externally?
A way to check to see if the control is checked without exposing the control itself is to add a public method (or a public property with just the getter) that does that to your SettingsForm.
public bool IsNotch50hzbuttonChecked()
{
return notch50hzbutton.Checked;
}
and then you can check
if (settingsFormInstance.IsNotch50hzbutton())
{
...
}
I would make a corresponding public property that's accessible to the outside:
public partial class SettingsForm : Form
{
public bool Is60Hz {get; private set;}
...
private void notch50Hz_Checked(object sender, EventArgs e)
{
notch50hzbutton.Checked = true;
Is60Hz = false;
}
private void notch60Hz_Checked(object sender, EventArgs e)
{
notch60hzbutton.Checked = true;
Is60Hz = true;
}