I have made this timer class and when its end timer function is called it is supposed to write a text file however it only seems to create a blank file every time. any ideas why? i have checked and the date modified is always the time i last ran the function.
public class Timer100NanoSeconds
{
long startTick;
List<Tuple<string, long>> stepTicks;
readonly string functionName;
int timesRan;
public Timer100NanoSeconds(string name)
{
functionName = name;
StartNew();
}
public void StartNew()
{
timesRan = 0;
startTick = DateTime.Now.Ticks;
stepTicks = new List<Tuple<string, long>>();
}
public void AddStep(string temp)
{
stepTicks.Add(new Tuple<string, long>(temp, DateTime.Now.Ticks));
}
public void counterForTimesRan(int ran)
{
timesRan = ran;
}
public void EndTimer()
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(functionName + ".txt"))
{
file.WriteLine("Ran " + timesRan + " Times");
for (int i = 0; i < stepTicks.Count; i++)
{
if (i == 0)
{
if (stepTicks[i].Item2 - startTick != 0)
{
file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
}
else
{
if (stepTicks[i].Item2 - stepTicks[i - 1].Item2 != 0)
{
file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
}
}
}
}
public void EndTimer(bool showZero)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(functionName + ".txt"))
{
for (int i = 0; i < stepTicks.Count; i++)
{
if (showZero)
{
if (i == 0)
{
file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
else
{
file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
}
else
{
if (i == 0)
{
if (stepTicks[i].Item2 - startTick != 0)
{
file.WriteLine(("" + (stepTicks[i].Item2 - startTick)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
}
else
{
if (stepTicks[i].Item2 - stepTicks[i - 1].Item2 != 0)
{
file.WriteLine(("" + (stepTicks[i].Item2 - stepTicks[i - 1].Item2)).PadLeft(15, ' ') + " , " + stepTicks[i].Item1.Trim());
}
}
}
}
}
}
}
Related
I am having troubles with datagridview when I want to have data partialy loaded and load next/previous data by scrolling (putting scrollbar with mouse to limit positions also).
This topic is much discussed, but mostly suggested answer "adding dataGridView1.PerformLayout()" does not works for me (Datagridview error : Value' should be between 'minimum' and 'maximum').
Due to this, I have created simple test form only with datagridview, code:
using System;
using System.Linq;
using System.Windows.Forms;
namespace DataGridViewTester
{
public delegate void DeleteTableDelegate(DataGridView table);
public delegate void FillRowsDelegate(DataGridView table, int rowsToAdd);
public partial class Form1 : Form
{
VScrollBar vScrollbar;
int index = 0;
public Form1()
{
InitializeComponent();
vScrollbar = dataGridView1.Controls.OfType<VScrollBar>().First();
vScrollbar.ValueChanged += new EventHandler(this.scrollbarValue_Changed);
AppendRange(dataGridView1, 100);
}
public void ClearTableContents(DataGridView table)
{
//For calling from different threads
if (table.InvokeRequired)
{
var d = new DeleteTableDelegate(ClearTableContents);
table.Invoke(d, new object[] { table });
return;
}
try
{
table?.Rows.Clear();
}
catch (InvalidOperationException ex) {
Console.WriteLine(ex);
}
table?.PerformLayout();
}
public void AppendRange(DataGridView table, int rowsToAdd)
{
if (table.InvokeRequired)
{
var d = new FillRowsDelegate(AppendRange);
table.Invoke(d, new object[] { table, rowsToAdd });
return;
}
Console.WriteLine(rowsToAdd);
DataGridViewRow[] dataGridViewRows = new DataGridViewRow[(rowsToAdd < 0 ? -rowsToAdd : rowsToAdd)];
index = (rowsToAdd >= 0) ? (index ) : (index - 2*rowsToAdd);
for (int i = 0; i < dataGridViewRows.Length; i++)
{
dataGridViewRows[i] = new DataGridViewRow();
dataGridViewRows[i].CreateCells(dataGridView1, new object[] { index + i, dataGridViewRows[i].GetHashCode() });
}
index = (rowsToAdd >= 0) ? (index+ rowsToAdd) : (index);
table?.Rows.AddRange(dataGridViewRows);
table?.PerformLayout();
}
private void scrollbarValue_Changed(Object sender, EventArgs e)
{
if (vScrollbar.Visible)
{
if (vScrollbar.Value + (vScrollbar.LargeChange - 1) >= vScrollbar.Maximum)
{
System.Console.WriteLine("Get NEXT results " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
try
{
ClearTableContents(dataGridView1);
Console.WriteLine("CLEARED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
AppendRange(dataGridView1, 1); //Append it to Grid
Console.WriteLine("APENDED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
}
catch (System.ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex);
}
System.Console.WriteLine(vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
}
else if (vScrollbar.Value <= vScrollbar.Minimum)
{
System.Console.WriteLine("Get PREVIOUS results " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
try
{
ClearTableContents(dataGridView1);
Console.WriteLine("CLEARED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
AppendRange(dataGridView1, -1); //Append it to Grid
Console.WriteLine("APENDED " + vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
dataGridView1?.PerformLayout();
}
catch (System.ArgumentOutOfRangeException ex)
{
Console.WriteLine(ex);
}
System.Console.WriteLine(vScrollbar.Maximum + ">=" + vScrollbar.Value + ">=" + vScrollbar.Minimum + " Large change -1 = " + (vScrollbar.LargeChange - 1));
}
}
}
}
}
Thank you for your answers.
EDIT: This behaviour is caused holding scrollbar by mouse. If this paging is mapped to PageDown and PageUp buttons, there are no errors.
I am trying to convert a program from console to windows form but the problem is that the output is not showing on my textbox. My program is that user inputs how many number of rows.
int n = int.Parse(textbox1.Text);
int counter1 = 1,counter2 = 1,size = n + n -1;
for(int i = 0;i<size;i++){
for(int j = 0;j<size;j++){
if(i<n-1){
if(j<n-1){
if(i == j){
textBox2.Text = "{0} " + counter1;
counter1++;}
else{
textBox2.Text = " ";
} }
else{
if(i + j == size-1){
textBox2.Text = "{0} " + counter2;
counter2++;}
else{
textBox2.Text = " ";
}
} }
else if(i == n- 1 && j == n-1){
textBox2.Text = "{0} " + counter1;
counter1--;
counter2--; }
else if(i == n-1 && j != n-1){
textBox2.Text = " ";
}
else if(i > n-1){
if(j>n-1){
if(i == j){
textBox2.Text = "{0} " + counter1;
counter1--;
}
else{
textBox2.Text = " ";
} }
else
{
if(i + j == size-1){
textBox2.Text = "{0} " + counter2;
counter2--;
}
else{
textBox2.Text = " ";
}
} } }
textBox2.Text = " ";
}
The program is to display the input x number pattern. Thanks in advance.
You are resetting the text of textBox2 every time. You should use the += operator instead of =.
Side note: You should also use the special '\n' character when you need a new line.
For some reason when I run this code I am noticing that the letter "I or i" or word "it" crashes the program. Also when I just click translate with nothing entered it crashes as well. I have gone over this code over and over but I can't find the problem. Any suggestions?
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void btnTranslate_Click(object sender, EventArgs e)
{
String input = Convert.ToString(txtInput.Text.Trim());
String inputTr = Regex.Replace(input, " {2,}", " ");
String pigLatin = "";
String temp = "";
String restOfWord = "";
String vowels = "AEIOUaeiou";
String consonants = "YBCDFGHJKLMNPQRSTVXWZbcdfghjklmnpqrstvxwzy";
string[] words = inputTr.Split();
foreach (string word in words)
{
if (string.IsNullOrEmpty(txtInput.Text))
{
MessageBox.Show("Text must be entered");
}
int index = word.IndexOfAny(new char[] { 'A', 'E', 'I', 'O', 'U', 'a', 'e', 'i', 'o', 'u' });
if (Regex.IsMatch(word, "[##$%0-9]"))
{
pigLatin += word + " ";
}
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower()))
{
pigLatin += word + "way" + " ";
}
else if (char.IsPunctuation(word.Last()) && vowels.Contains(word[0]) && word.Contains(word.Substring(1, 2).ToLower()))
{
pigLatin += word.Substring(0, word.Length - 1) + "way" + word.Last() + " ";
}
else if (!(char.IsPunctuation(word.Last())) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord + firstPart + "ay" + " ";
}
else if (char.IsPunctuation(word.Last()) && consonants.Contains(word[0]) && word.StartsWith(word.Substring(0, 1).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, restOfWord.Length - 1) + firstPart + "ay" + restOfWord.Last() + " ";
}
else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper()))
{
pigLatin += word + "WAY" + " ";
}
else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && vowels.Contains(word.Substring(0, 1).ToUpper()))
{
pigLatin += word.Substring(0, word.Length - 1) + "WAY" + word.Last() + " ";
}
else if (!(char.IsPunctuation(word.Last())) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(1, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + " ";
}
else if (char.IsPunctuation(word.Last()) && word.StartsWith(word.Substring(0, 1).ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()) && word.Contains(word.Substring(1, 2).ToLower()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
temp = restOfWord.Substring(0, 1).ToUpper() + restOfWord.Substring(0, restOfWord.Length - 1).ToLower() + firstPart.ToLower() + "ay" + restOfWord.Last() + " ";
temp = temp.Remove(0, 1);
pigLatin += temp.Substring(0, 1).ToUpper() + temp.Substring(1, temp.Length - 1).ToLower() + " ";
}
else if (!(char.IsPunctuation(word.Last())) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.ToUpper() + firstPart.ToUpper() + "AY" + " ";
}
else if (char.IsPunctuation(word.Last()) && word.Contains(word.ToUpper()) && consonants.Contains(word.Substring(0, 1).ToUpper()))
{
string firstPart = word.Substring(0, index);
restOfWord = word.Substring(index, word.Length - index);
pigLatin += restOfWord.Substring(0, restOfWord.Length - 1).ToUpper() + firstPart.ToUpper() + "AY" + word.Last() + " ";
}
txtOutput.Text = pigLatin;
}
}
private void btnClear_Click(object sender, EventArgs e)
{
txtInput.Text = "";
txtOutput.Text = "";
txtInput.Focus();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void txtInput_TextChanged(object sender, EventArgs e)
{
}
}
In several places throughout your code you have "Substring(1, 2)" - if the word you are currently processing is short than three characters long then you will get an exception because you are trying to get a substring that extends beyond the end of the string.
You need to add length checking into your code.
e.g.
...
...
else if (!(char.IsPunctuation(word.Last())) && vowels.Contains(word[0]) &&
&& (word.Length >= 3) && word.Contains(word.Substring(1, 2).ToLower()))
...
...
Just as a note on debugging - you could put a (conditional) breakpoint on the line where the exception occurs & then check each individual part of your if statement in the Immediate Window (copy & paste) to see which clause is causing the exception.
for (int i = 0; i < metroGrid1.Rows.Count; i++)
{
if (metroGrid1.Rows[i].Cells[0].Value.ToString() == radGridView1.SelectedRows[0].Cells[0].Value.ToString())
{
counter = i;
metroGrid1.Rows[counter].Cells[2].Value = Convert.ToInt32(metroGrid1.Rows[counter].Cells[2].Value) + radSpinEditor1.Value;
MessageBox.Show("for loop");
}
else
{
metroGrid1.Rows.Add(radGridView1.SelectedRows[0].Cells[0].Value.ToString(), radGridView1.SelectedRows[0].Cells[1].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[2].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[3].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[4].Value.ToString(), radSpinEditor1.Value, decimal.Round(prodamt, 2), decimal.Round(prodtotamt, 2));
totamt += prodtotamt;
metroLabelTotalamt.Text = (string.Format("{0:#,###0.00}", totamt));
radSpinEditor1.Value = 1;
MessageBox.Show("else ");
}
}
Shouldn't that be? Since you yourself adding rows to your gridview by calling Add() method as can be seen in your posted code
metroGrid1.Rows.Add(radGridView1.SelectedRows[0].Cells[0].Value.ToString(),
No idea since I don't know your requirement but to me it feels like you wanted to have the other part as else block.
for (int i = 0; i < metroGrid1.Rows.Count; i++)
{
if (metroGrid1.Rows[i].Cells[0].Value.ToString() == radGridView1.SelectedRows[0].Cells[0].Value.ToString())
{
counter = i;
metroGrid1.Rows[counter].Cells[2].Value = Convert.ToInt32(metroGrid1.Rows[counter].Cells[2].Value) + radSpinEditor1.Value;
MessageBox.Show("for loop");
}
else
{
metroGrid1.Rows.Add(radGridView1.SelectedRows[0].Cells[0].Value.ToString(), radGridView1.SelectedRows[0].Cells[1].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[2].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[3].Value.ToString() + " " + radGridView1.SelectedRows[0].Cells[4].Value.ToString(), radSpinEditor1.Value, decimal.Round(prodamt, 2), decimal.Round(prodtotamt, 2));
totamt += prodtotamt;
metroLabelTotalamt.Text = (string.Format("{0:#,###0.00}", totamt));
radSpinEditor1.Value = 1;
MessageBox.Show("else ");
}
}
SELECT
machine_id, operator_id, member_id, card_id, name, paid_amount, due_amount,
paid_date, phone_number, #curRow := #curRow + 1 AS row_number
FROM
transaction
JOIN
(SELECT #curRow := 0) r where card_id='c1' order by Row_number desc limit 3 ;
When I run this in workbench it returns the last 3 records. But in my code it returns only 2 records. What is the problem?
Here is the c# code:
String query3 = "SELECT machine_id,operator_id,member_id,card_id,name,paid_amount,due_amount,paid_date,phone_number ,#curRow := #curRow + 1 AS row_number FROM transaction JOIN (SELECT #curRow := 0) r where card_id=#card order by Row_number desc limit 4 ";
MySqlCommand command3 = new MySqlCommand(query3, con);
command3.Parameters.AddWithValue("#card", cardid);
using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
if (rdr3.Read())
{
while (rdr3.Read())
{
if (count == 1)
{
AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT1 = rdr3["paid_date"].ToString();
}
if (count == 2)
{
AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT2 = rdr3["paid_date"].ToString();
}
if (count == 3)
{
AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT3 = rdr3["paid_date"].ToString();
}
count++;
}
Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
}
You use Read() twice at the beginning and you skip the first record this way. Remove the one from if:
if (rdr3.Read())
You need just:
using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
while (rdr3.Read())
{
if (count == 1)
{
AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT1 = rdr3["paid_date"].ToString();
}
if (count == 2)
{
AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT2 = rdr3["paid_date"].ToString();
}
if (count == 3)
{
AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT3 = rdr3["paid_date"].ToString();
}
count++;
}
Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
}
Use if (rdr3.HasRows) instead of if (rdr3.Read())
using (MySqlDataReader rdr3 = command3.ExecuteReader())
{
if (rdr3.HasRows)
{
while (rdr3.Read())
{
if (count == 1)
{
AMT1 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT1 = rdr3["paid_date"].ToString();
}
if (count == 2)
{
AMT2 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT2 = rdr3["paid_date"].ToString();
}
if (count == 3)
{
AMT3 = Convert.ToDecimal(rdr3["paid_amount"].ToString());
TOT3 = rdr3["paid_date"].ToString();
}
count++;
}
}
Else
{
Response.Write("No rows found.");
}
Response.Write("$AMT1=" + AMT1 + "|TOT1=" + TOT1 + "|AMT2=" + AMT2 + "|TOT2=" + TOT2 + "|AMT3=" + AMT3 + "|TOT3=" + TOT3 + "|TS=1#");
}