In this page, you can add items. now you press "Save" to add another one. Heres the code:
private void Btn_Save_Click(object sender, RoutedEventArgs e)
{
// Adding the item to DB and List
MainData.MainDataItem MDI_Temp = new MainData.MainDataItem();
MDI_Temp.Int_AF = Convert.ToInt32(Tb_AF.Text);
MDI_Temp.Int_HO = Convert.ToInt32(Tb_HO.Text);
MDI_Temp.Int_ST = Convert.ToInt32(Tb_ST.Text);
MDI_Temp.Int_STD = Convert.ToInt32(Tb_STD.Text);
MDI_Temp.Int_DIA = Convert.ToInt32(Tb_DIA.Text);
MDI_Temp.Int_ECK = Convert.ToInt32(Tb_ECK.Text);
MDI_Temp.Int_MID = ((HelperClasses.Main_VM)this.DataContext).MDO_TmpStore.Int_ID;
MDI_Temp.Str_Bauteil = Str_Bauteil;
MDI_Temp.Str_Defekt = Str_Defekt;
MDI_Temp.Str_Massnahme = Str_Massnahme;
MDI_Temp.Str_Feld = Tb_Feld.Text;
MDI_Temp.Str_Zeile = Tb_Zeile.Text;
MDI_Temp.Int_Pos = Convert.ToInt32(Tb_Pos.Text);
HelperClasses.SQL_Class.DBAddItem(MDI_Temp);
// Navigate
HelperClass.Navigate("pages/New_Item.xaml");
}
And this is the void in the helperclass:
public static void Navigate(string Str_Uri)
{
((MainWindow)Application.Current.Windows[0]).Fm_MainContainer.Source = new Uri(Str_Uri, UriKind.Relative);
}
The first time you click on Btn_Save the page reloads, the second time it onyl add the item
Another option is to create an overload of navigate in helper class
public static void Navigate(object target)
{
((MainWindow)Application.Current.Windows[0]).Fm_MainContainer.Content = target;
}
ans use this way
// Navigate
New_Item item = new New_Item();
HelperClass.Navigate(item);
this will ensure to have a new initialization every time
Related
private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
This corresponds to a list box's selectionChanged event handler. Is there a way I can just use this in my code as if it were a method. e.g.
SelectionChanged(foo, bar);
Basically, is there a way of using this block of code again without creating another method to hold this stuff.
Since you don't use any of the event arguments, you can extract the code into a separate method:
private void OnCarSelectionChanged(object sender, SelectionChangedEventArgs e)
{
UpdateDisplay();
}
private void UpdateDisplay()
{
Car tmpCar = (Car)CarLST.SelectedItem;
VinNumberTB.Text = tmpCar.VinNumber;
CarMakeTB.Text = tmpCar.CarMake;
CarTypeCB.SelectedIndex = (int)tmpCar.Type;
PurchasePriceTB.Text = tmpCar.PurchasePrice.ToString();
ModelYearCB.SelectedItem = tmpCar.ModelYear;
MileageTB.Text = tmpCar.Mileage.ToString();
CarIMG.Source = new BitmapImage(new Uri(String.Format("ms-appx:///Assets/CarImages/{0}.png", tmpCar.Type.ToString())));
}
Then to call it separately all you need to do is:
this.UpdateDisplay();
I am making a Windows Forms App that manages a hotel. It has Client, Room, Occupancy classes. Client and Rooms have an ArrayList that is populated at runtime from a .txt file that is then displayed in a clientListView and a roomDataGridView.
As such, I have this line of code to populate the roomsDGV:
roomsDGV.DataSource = roomsArrayList;
With the roomsDGV, I'm trying to add new Rows by clicking on the roomsDGV, like when it is NOT databound. I am also trying to edit the rows and save it to txt file after editing or as I'm editing. I can post more code as necessary but I'm not sure if showing more code will help at the current moment. In the end, I'm trying for a functionality so that I can highlight a client in the list and click on one of the rows in roomsDGV and assign that clientID to that room or any sort of way like that.
On load, the datagridview is loaded and formatted correctly from the arrayList but I seem to be having this problem of being able to edit the datagridview. It gives me this error when I click on one of the rows:
System.IndexOutOfRangeException: 'Index -1 does not have a value.'
This stems from Application.Run(new HotelManager());
Here is the form:
public partial class HotelManager : Form
{
// VARIABLES
string clientID;
// FILEPATHS
string clientsTxt = "Clients.txt";
string occupanciesTxt = "Occupancies.txt";
string roomsTxt = "Rooms.txt";
string clientsDat = "Clients.dat";
// ARRAYLIST FOR ROOMS and CLIENTS
ArrayList roomsArrayList = new ArrayList();
ArrayList clientsArrayList = new ArrayList();
//STACKS AND QUEUES INIT
// Load occupancies into stack > pop
Stack roomStack = new Stack();
Queue vacancyQueue = new Queue();
// RANDOM for ID
private readonly Random rand = new Random();
public HotelManager()
{
InitializeComponent();
}
private void HotelManager_Load(object sender, EventArgs e)
{
roomsDGV.DataSource = roomsArrayList;
// LOAD clients
// LoadClients();
RefreshClientList();
// LOAD rooms
LoadRooms();
}
private void NewClientButton_Click(object sender, EventArgs e)
{
AddClient();
}
private void checkInButton_Click(object sender, EventArgs e)
{
string clientID = clientList.SelectedItems[0].Text;
string[] text = File.ReadAllLines(occupanciesTxt);
foreach (string s in text)
{
if (s.Contains(clientID))
{
var replace = s;
Console.WriteLine(s);
replace = replace.Replace("false", "true");
}
}
File.WriteAllLines(occupanciesTxt, text);
}
// METHODS
private void AddClient()
{
//COLLECT DATA > CREATE NEW client > SHOW IN **PROGRAM/DataGridView** > add to clients file
// ID GENERATION > CHECKS AGAINST clientsTXT
clientID = rand.Next(0, 999999).ToString();
if (File.ReadLines(clientsTxt).Contains(clientID))
{
clientID = rand.Next(0, 999999).ToString();
}
Client client = new Client(clientID, firstNameBox.Text, lastNameBox.Text);
try
{
if (!string.IsNullOrWhiteSpace(phoneNumBox.Text))
{
client.PhoneNumber = Convert.ToInt64(phoneNumBox.Text);
}
if (!string.IsNullOrWhiteSpace(addressBox.Text))
{
client.Address = addressBox.Text;
}
}
catch (Exception)
{
MessageBox.Show("Please use the correct format!");
throw;
}
clientsArrayList.Add(client);
using (StreamWriter file =
new StreamWriter("Clients.txt", true))
{
file.WriteLine(client.ToString());
}
RefreshClientList();
// TEST CODE // SERIALIZATION TO .DAT
SerializeClientData(client);
}
private void LoadClients()
{
// LOADS arrayList FROM .txt FILE
List<string> clientList = File.ReadAllLines(clientsTxt).ToList();
foreach (var c in clientList)
{
Client client = new Client(c);
clientsArrayList.Add(client);
}
}
private void LoadRooms()
{
List<string> roomsList = File.ReadAllLines(roomsTxt).ToList();
foreach (var r in roomsList)
{
var roomDetails = r.Split('|');
if (r.Contains("BASIC"))
{
BasicRoom basic = new BasicRoom();
basic.RoomNumber = roomDetails[0];
basic.NumberOfBeds = Convert.ToInt32(roomDetails[1]);
basic.Balcony = Convert.ToBoolean(roomDetails[2]);
basic.DownForRepair = Convert.ToBoolean(roomDetails[3]);
basic.Smoking = Convert.ToBoolean(roomDetails[4]);
roomsArrayList.Add(basic);
}
else if (r.Contains("SUITE"))
{
Suite suite = new Suite();
suite.RoomNumber = roomDetails[0];
suite.NumberOfBeds = Convert.ToInt32(roomDetails[1]);
suite.Balcony = Convert.ToBoolean(roomDetails[2]);
suite.DownForRepair = Convert.ToBoolean(roomDetails[3]);
suite.NumberOfRooms = Convert.ToInt32(roomDetails[4]);
roomsArrayList.Add(suite);
}
}
roomStack = new Stack(roomsArrayList);
foreach (var item in roomStack)
{
Console.WriteLine(item);
}
}
private void RoomsDGV_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void RoomsDGV_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{
}
}
So far I've looked through all the properties but I can't seem to find the right one. I know I can add/use comboboxes and etc to add a new item into the arrayList instead but I'm trying for datagridview functionality
I expect to edit and add rows to the DGV, but something in the designer is preventing me?
Here is the DGV, and clicking on any of the rows breaks it.
https://imgur.com/a/GG7ZwdV
check this
Insert, Update, Delete with DataGridView Control in C# (Windows Application) | A Rahim Khan's Blog
Insert, Update and Delete Records in a C# DataGridView
Good Luck
Create an object in the class "Series ema = new Series("ema");"
With the button "indicator_Click" download modal dialog.
In the window using the "ComboBox" select options: dig.type, dig.period.
Series ema = new Series("ema");
**********************************************
private void indicator_Click(object sender, EventArgs e)
{
ModalDialogBox dig = new ModalDialogBox();
{
if (dig.ShowDialog() == DialogResult.OK)
{
string formulaName = dig.type;
FinancialFormula formula = (FinancialFormula)Enum.Parse(typeof(FinancialFormula), formulaName, true);
chart1.Series.Add(ema);
chart1.DataManipulator.FinancialFormula(formula, dig.period, "price", "ema");
chart1.Series["ema"].Color = Color.Red;
chart1.Series["ema"].ChartType = SeriesChartType.Line;
chart1.Series["ema"].BorderWidth = 2;
chart1.Series["ema"].ShadowOffset = 1;
}
}
}
Next, if the indicator is not needed, delete it.
private void delete_Click(object sender, EventArgs e)
{
chart1.Series.Remove(ema);
}
I need to be able to add a second, third indicator.
If I try to add a series of the second indicator, an error occurs because the indicator already exists.
You can of course create multiple objects in a class:
Series ema1 = new Series("ema1");
Series ema2 = new Series("ema2");
Series ema3 = new Series("ema3");
but I do not like this method, because I do not know in advance how many indicators will be needed
So here's my problem. I'm creating simple android app and I want to remove item from a List view. My code kind of works, the problem is that item is deleted after second click (remove button). Also i need to select item second time.
What I wanna do is to select an item one time, and delete it by clicking remove button.
Here is my code for Remove-button.
private void Remove_Activated(object sender, EventArgs e)
{
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
{
conn.CreateTable<Book>();
var books = conn.Table<Book>().ToList();
booksListView.ItemsSource = books;
var selItem = (Book)booksListView.SelectedItem;
Book remBook = new Book()
{
Id = selItem.Id,
Name = selItem.Name,
Author = selItem.Author
};
conn.Delete(remBook);
};
}
I have also overrided OnAppearing(), can this be the problem?
protected override void OnAppearing()
{
base.OnAppearing();
Title = "Booklist";
using (SQLite.SQLiteConnection conn = new SQLite.SQLiteConnection(App.DB_PATH))
{
conn.CreateTable<Book>();
var books = conn.Table<Book>().ToList();
booksListView.ItemsSource = books;
}
}
I have done the adding part like this guy in this tutorial: https://www.youtube.com/watch?v=JhWwBOoqXQ8
The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.