|
When you press the Activate button the request will be sent to the server in microseconds at most, so there is no way you could react fast enough to stop it. I don't understand what you think you can achieve with this button.
|
|
|
|
|
Okay, pretty simple question, but I'm struggling in figuring it out. I need a label (that's reading from a text file) to return only certain values from within that same text file. The code I have is below:
private void btnCalculate_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string number = txtNumber.Text;
double hourlyRate = Convert.ToDouble(txtPay.Text);
double hoursWorked = Convert.ToDouble(txtHours.Text);
double grossPay = 0.0;
TextWriter txt = new StreamWriter("employee_information.txt");
if (hoursWorked <= 40)
grossPay = hourlyRate * hoursWorked;
else
grossPay = (hourlyRate * 40) + (hourlyRate * 1.5) * (hoursWorked - 40);
txt.Write("Employee Name: "+ txtName.Text + " " + txtNumber.Text + " " + txtPay.Text + " " + txtHours.Text);
txt.Close();
try
{
using (StreamReader sr = new StreamReader("employee_information.txt" ))
{
string line = sr.ReadToEnd();
lblInformation.Text = line;
}
}
catch (Exception ex)
{
lblInformation.Text = "The file could not be read:";
}
}
I require the lblInformation.Text to read the Name value from the text file, along with the total pay being formatted (in a dollar amount) but it is currently reading all the information from said text file.
Any help or feedback is appreciated,
Thank you
|
|
|
|
|
We can;t help you with that - you need to look at your file and work out exactly how it stores the info you need. The chances are that it's stored by line (judging from your code) but what part of the line is the name and what the salary depend on the data content of each line - if you are lucky, it will be separated, delimited, or structured in order to make it relatively simple. But without the data? We can't even begin to guess.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I see, to answer your question, the data is stored as: name, id, pay, then hours worked. That's the order I have it writing it to the file. Does that help in any way?
|
|
|
|
|
So your data is stored as
Employee Name: John smith Junior 1234 20000.00 36 That's a bad idea - it makes it very difficult to read back, particularly since you are passing user input direct to the output file. You don't check what the user entered and have no idea if the data you write is correct!
So there are two things to do:
1) Validate your inputs!
If the user should enter a number, check it is a valid number:
double pay;
if (!double.TryParse(txtPay.Text, out pay))
{
... report problem to user
return;
} And the same for all other non-text values.
Text values are another problem, and that leads to:
2) Structure your file! When you separate with spaces, it's very difficult to work out what is what: did the user type the name as
John
John Smith
John Smith Junior
Charles Philip Arthur George Mountbatten-Windsor When your separator is the same as a legitimate name character you leave yourself a lot of problems.
There are a number of ways to deal with that, in order of effectiveness and complexity:
2a) Move the variable length entry (the name) to the end of the line. That way, it doesn't matter how long it is!
2b) Use an "special character" as the field separator: comma is a good one, as is '|'
2c) Delimit text strings, and use a special character:
"John",
"John Smith",
"John Smith Junior",
"Charles Philip Arthur George Mountbatten-Windsor",
2d) Use a "proper" CSV data system - there are some good ones out there: A Fast CSV Reader[^] works well.
2e) Use a structured data file: XML or JSON are both excellent and widely supported.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks for the reply! I've managed to figure it out though, added semicolons to the file after each item, then split it on the semicolon, does what I want it to, and I plan on putting in validations, just wanted to get this part working first.
|
|
|
|
|
You "joined" some constants and variables using " " as a delimter to get the text line that you wrote.
To "split" that same text line back into its "parts", you can use the String.Split() method which will create an array of your "constants and variables"; split on " ".
How to: Parse Strings Using String.Split (C# Guide) | Microsoft Docs
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
I got some really cool C# code from the article "Managing Printers Programatically using C# and WMI", by Raymund Macaalay.
I am struggling with the ManagementScope class. VS2017 cannot find it. Here is a code snippet from the article's code:
using System;
using System.Management;
class PrinterSettings
{
private static ManagementScope oManagementScope = null;
Intellisense seems completely ignorant of the ManagementScope class. MSDN.Microsoft.com states that it is in System.Management assembly.
I know I am not including something correctly and not deprecated, but kinda noobish on C#.
thanks!
|
|
|
|
|
It works for me - I just tried a new project in VS2017 (a .NET console since it's the simplest), added the reference to System.Management , and added your code:
namespace ConsoleApp1
{
class Program
{
private static ManagementScope oManagementScope = null;
static void Main(string[] args)
{
oManagementScope.
}
}
} At the ".", intellisense lists all the properties and methods I would expect.
What am I doing that is different to you?
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
You're adding a reference to the assembly.
"Just guessing"
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Probably, but ... is ManagementScope in .NET Core?
We don't know what exactly he is doing, so find one that works, and see how it differs is a start at least...
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Typing in System.Management. Intellisense only displays "Instrumentation" as a member for me. It is the only member....
|
|
|
|
|
What kind of project are you working on?
I get the full list (including Instrumentation).
Have you tried turning it off and back on again?
(I know, I know - but it's worth a try - I hear a few bad things about VS2017 getting confused at times.)
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
THanks, I closed VS and opened it again and no better. I created a new Console app like you did. Under C#, I selected "Windows Classic Desktop", then "Console App(.NET Framework). added using System.Management;
still it does not recognize ManagementScope.
Could it be that I need to install/add an assembly or something to VS2017? Just guessing here....
|
|
|
|
|
Just adding the using does not reference the assembly. Right-click on the project, go to references, and add it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
As Eddie says - and I mentioned in my original answer - you need a reference:
Quote: ...added the reference to System.Management...
Open the Project in the Solution Explorer pane, right click "References", select "Add Reference...", then tick the checkbox to the left of the "System.Management" assembly under the "Assemblies...Framework" selector on the left hand side. Click OK, and it'll appear in the references list for the project.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
You're welcome!
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I have an Ink Canvas control on my application. Is it possible that whenever i draw a line, there's a value that appears between on that line by the use of C#?
Here is my code, XAML:
<DockPanel>
<StackPanel DockPanel.Dock="Bottom">
<Button x:Name="ClearButton" Content="Clear" Click="ClearButton_Click"/>
<Button x:Name="SaveButton" Content="Save" Click="SaveButton_Click"/>
</StackPanel>
<InkCanvas x:Name="DrawingPane" Cursor="Cross" Background="White"/>
</DockPanel>
And here is my C# code:
namespace WpfApp1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.WindowStartupLocation = WindowStartupLocation.CenterScreen;
}
private void ClearButton_Click(object sender, RoutedEventArgs e)
{
DrawingPane.Strokes.Clear();
}
}
}
I hope you can help me out for this. Thank you.
|
|
|
|
|
Since this is not "just" C#, you should post this in the right forum: Silverlight / WPF Discussion Boards[^] for the best results.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
This is from the top of my head so you'll have to work out the details yourself.
Catch the events mousedown and mousemove. If the mouse moves AND mouse button is down you're drawing (or so I assume).
You'll need to find out the coordinates from your canvas where top-left = 0,0 and x moves up to the right and y moves up going down! It might be you need to fiddle with it to get the right coordinates as windows tends to work with multiple sets (your window, your control, the screen, ...)
Once you have the correct coordinates and you start drawing a line (= first point is known) you can calculate the length of your line: sqrt((x2-x1)^2 + (y2-y1)^2) use your mouse location to get the second point.
To set that value on screen, draw a string on the location (x2+x1)/2 and (y2/y1)/2.
This solution should at least get you a bit further, but again, this is on top of my head, you'll need to figure out a lot of it yourself.
|
|
|
|
|
You don't draw "between" an object (i.e. the line); only "over" (or under).
Determine "where" the "value" should go, and simply place a Label, TextBlock, TextBox, Image, etc. with the appropriate styling (e.g. Margin; transparent background) there.
"(I) am amazed to see myself here rather than there ... now rather than then".
― Blaise Pascal
|
|
|
|
|
Hi programmer friend, I finished my software that is connected to a SQLite database that works perfectly and I wanted to deploy it on another machine. I have an error message: Can not load file or assembly 'System.Data.SQlite.
Version = 1.0.108.0. Culture = neutral. PublicKeyToken = db937bc2d44ff139 '
or one of his dependencies. The specified file can not be found
Thank you for bringing me your help here is the connectoin code:
(
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.Data.OleDb;
using System.Data.SQLite;
namespace Acode
{
public partial class Form1: Form
{
public Form1 ()
{
InitializeComponent ();
}
private SQLiteConnection sql_con;
private SQLiteCommand sql_cmd;
private SQLiteDataAdapter DB;
private DataSet DS = new DataSet ();
private DataTable DT = new DataTable ();
private void setConnection ()
{
// CONNECTING TO THE DATABASE
sql_con = new SQLiteConnection (@ "Data Source = DBcode.db; Version = 3; New =; Compress = True;");
}
private void LoadData ()
{
SetConnection ();
sql_con.Open ();
sql_cmd = sql_con.CreateCommand ();
string CommandText = "select * from InfoCode";
DB = new SQLiteDataAdapter (CommandText, sql_con);
DS.Reset ();
DB.Fill (DS);
DT = DS.Tables [0];
dataGridView1.DataSource = DT;
sql_con.Close ();
}
private void Form1_Load (object sender, EventArgs e)
{
LoadData ();
}
private void ExecuteQuery (String txtQuery)
{
SetConnection ();
sql_con.Open ();
sql_cmd = sql_con.CreateCommand ();
sql_cmd.CommandText = txtQuery;
sql_cmd.ExecuteNonQuery ();
sql_con.Close ();
}
public static string randomstring (int length)
{
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWYZ0123456789";
Random random = new Random ();
return new string (Enumerable.Repeat (floats, length) .Select (s => s [random.Next (s.Length)]). ToArray ());
}
private void btnGenerer_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (4);
txtAfficPrice.Text = "100";
}
private void button1_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (5);
txtAffichPrice.Text = "300";
}
private void button2_Click (object sender, EventArgs e)
{
lblDisplay.Text = randomstring (6);
txtAffichPrice.Text = "500";
}
private void btnRegister_Click (object sender, EventArgs e)
{
if (lblDisplay.Text == "")
{
MessageBox.Show ("Please generate the code");
}
else
{
using (SQLiteConnection con = new SQLiteConnection (sql_con))
{
try
{
con.Open ();
// Insert code to process data.
using (SQLiteCommand cmd = new SQLiteCommand ("INSERT INTO InfoCode (Code, DateCode, PriceCode) VALUES (@Code, @DateCode, @CodePrice)", con))
{
cmd.Parameters.AddWithValue ("@ Code", lblDisplay.Text);
cmd.Parameters.AddWithValue ("@ DateCode", dateTimePicker1.Text);
cmd.Parameters.AddWithValue ("@ PriceCode", txtAfficPrice.Text);
cmd.ExecuteNonQuery ();
}
LoadData ();
MessageBox.Show ("Registration completed successfully");
lblDisplay.Text = "";
groupBoxGenerer.Enabled = false;
btnSave.Enabled = false;
btnPrint.Enabled = true;
This.Refresh ();
}
catch (Exception ex)
{
MessageBox.Show ("Failed to connect to data source" + ex.Message);
}
finally
{
con.Close ();
}
}
}
}
private void btnAdd_Click (object sender, EventArgs e)
{
groupBoxGenerer.Enabled = true;
btnSave.Enabled = true;
}
private void exitToolStripMenuItem_Click (object sender, EventArgs e)
{
this.Close ();
FrmAuthentification
|
|
|
|
|
|
OK I install it and I give you the rest
thank you for your promptitude
|
|
|
|
|