In here:
using (TextReader rdr = new StreamReader(@"C:\"text.txt"))
{
//code in here
}
In here:
using (TextReader rdr = new StreamReader(@"C:\"text.txt"))
{
//code in here
}
Please remember shyla, one question per thread. MArk this one as salved (vote as useful to those guys who answered on your question), and mark the whole thread as answered.!
Remember, ONE question per thread.
Bye
Your code works fine for me, and even my code which I added.
Double check again.
Mitja
If I was wrong in my last statement, you cna do it like:
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataTable table = new DataTable();
//getting new results:
using (SqlConnection sqlConn = new SqlConnection("connectionString"))
{
string query = @"SELECT OrderNumber, Address, City, ZipCode FROM Customer WHERE OrderNumber = @number";
SqlCommand cmd = new SqlCommand(query,sqlConn);
cmd.Parameters.Add("@number", SqlDbType.Int).Value = textBox1.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(table);
}
//clearing and populating listView:
listView1.Items.Clear();
foreach (DataRow dr in table.Rows)
{
ListViewItem lvi = new ListViewItem(dr[0].ToString());
lvi.SubItems.Add(dr[1]);
lvi.SubItems.Add(dr[2]);
lvi.SubItems.Add(dr[3]);
listView1.Items.Add(lvi);
}
}
You want to do an sql query which would return all the order numbers, which contains the inserted number in textBox? The order is not counting at all?
I can see you typed into a textBox 10078, and the return was 2 results: 1007851103 and 11007859.
2nd order number has 11 on the beginning - this is not what you wrote into textBox. So does it mean you return every single order number that contains at least all those numbers combination? This is strange. And it is leading no where.
I checked the code, and repaired it:
private void button1_Click(object sender, EventArgs e)
{
string fulPath = comboBox1.SelectedItem + treeView1.SelectedNode.FullPath;
if (fulPath.ToLower().EndsWith(".txt"))
{
using (StreamReader sr = new StreamReader(fulPath))
{
richTextBox1.Clear();
string line;
while ((line = sr.ReadLine()) != null)
richTextBox1.AppendText(line);
}
}
else
MessageBox.Show("");
}
Hope it helps, (sretno)
Mitja
What is not working exactly?
Check this out:
public Form1()
{
InitializeComponent();
richTextBox1.TextChanged += new EventHandler(richTextBox1_TextChanged);
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if (richTextBox1.Lines.Length == 4)
DoCode(true);
}
private void DoCode(bool bVisible)
{
if (bVisible)
{
//Do code in here
MessageBox.Show("Verical scrollBar is now visible.");
}
}
its a simple one, but this is what you stated in the 1st post.
Mitja
If you're just checking to see if the vertical scroll bar is enabled then you can tell the textbox to write multiple lines of text. Like:
void appLaunch() { richTextBox1.Text = "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + "" + Environment.NewLine + ""; } void timer3000ms() { richTextBox1.Text = ""; }
That will force around 15 lines of blank text into the text box, then with the timer, 3 seconds later it will erase the entire text box. Hope it helps. Wasn't too sure on what you're asking.
What is this example all about?
I dont understand a think any more. How can be closing a window so hard?
Have you ever heard of "Google"? Well if you might did, try to use it. There is written every thing. I have no imagination left, sorry...
Check out this code for comparing two arrays:
int[] array1 = { 1, 2, 3, 4, 5, 6 };
int[] array2 = { 1, 3, 4, 4, 5, 7 };
int[] newArray = null;
int k = 1;
for (int i = 0; i < array1.Length; i++)
{
if (array1[i] != array2[i])
{
Array.Resize(ref newArray, k);
newArray[k - 1] = array1[i];
k++;
}
}
Mitja
How you cannot run? Whats the matter? I cannot understand what is wrong, becuase it should work. Please tell me.
Mitja
TI have converted the app. to Win form application. Now the result is shown in messageBox:
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.IO;
namespace Mar24NumberSorting_Win
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Method();
}
private void Method()
{
List<Values> list = new List<Values>();
Dictionary<int, int> dic = new Dictionary<int, int>();
string strPath = @"C:\1\test22.txt";
using (StreamReader sr = new StreamReader(strPath))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] _split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Values v = new Values();
for (int i = 0; i < _split.Length; i++)
{
int number = Convert.ToInt32(_split[i]);
//1. adding a number to common list<T>:
if (i == 0)
v.Value1 = number;
else
v.Value2 = number;
//2. adding number to a dictionary:
if (dic.ContainsKey(number))
dic = dic.ToDictionary(d => d.Key, d => d.Key == number ? d.Value + 1 : d.Value);
else
dic.Add(number, 1);
}
//1.1 adding values of both numbers to the list<T>:
list.Add(v);
}
}
//arranging the array:
string allText = null;
foreach (Values v in list)
{
int value1 = v.Value1;
int value2 = v.Value2;
int value1Rep = 0;
int value2Rep = 0;
foreach (KeyValuePair<int, int> kvp in dic)
{
if (kvp.Key == value1)
value1Rep = kvp.Value;
else if (kvp.Key == value2)
{
value2Rep = kvp.Value;
break;
}
}
if (value2Rep > value1Rep)
{
v.Value1 = value2;
v.Value2 = value1;
}
//show the result:
allText += v.Value1 + " - …
I strongly recommend you to switch to dataGridView - this is especially important when it comes to editing columns. ListView is so clumsy on this area. really. DGV is a far way better control. ListView is only meant to show data, not to edit it, even if its possible.
And besides, I doubt that its possible to create columns as disabled in listView - just as far as I have worked with it.
Hope it helps,
Mitja
Look. YOu have to create a Form_Closing even handler. Insite you do:
public Form2()
{
InitializeComponent();
this.FormClosing+=new FormClosingEventHandler(Form1_FormClosing);
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
e.Cancel = false;
this.Dispose();
}
Hope it helps,
Mitja
I dont get it. You will get some values from the file (like: gram, ounce, 5).
Then you will ask the user to insert something. But I still dont get what would that be? I dont understand your previous post. Can you be a bit more specific?
Mitja
Yes it it, or even better it to use:
this.Dispose();
The basic difference between Close() and Dispose() is, when a Close() method is called, any managed resource can be temporarily closed and can be opened once again. It means that, with the same object the resource can be reopened or used. Where as Dispose() method permanently removes any resource ((un)managed) from memory for cleanup and the resource no longer exists for any further processing.
Read here.
And what would be the user input?
Man I did it - finally. It works like charm.
Here`s the code:
class Program
{
static void Main(string[] args)
{
List<Values> list = new List<Values>();
Dictionary<int, int> dic = new Dictionary<int, int>();
using (StreamReader sr = new StreamReader(@"C:\1\test22.txt"))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] _split = line.Split(new string[] { " " }, StringSplitOptions.RemoveEmptyEntries);
Values v = new Values();
for (int i = 0; i < _split.Length; i++)
{
int number = Convert.ToInt32(_split[i]);
//1. adding a number to common list<T>:
if (i == 0)
v.Value1 = number;
else
v.Value2 = number;
//2. adding number to a dictionary:
if (dic.ContainsKey(number))
dic = dic.ToDictionary(d => d.Key, d => d.Key == number ? d.Value + 1 : d.Value);
else
dic.Add(number, 1);
}
//1.1 adding values of both numbers to the list<T>:
list.Add(v);
}
}
//arranging the array:
foreach (Values v in list)
{
int value1 = v.Value1;
int value2 = v.Value2;
int value1Rep = 0;
int value2Rep = 0;
foreach (KeyValuePair<int, int> kvp in dic)
{
if (kvp.Key == value1)
value1Rep = kvp.Value;
else if (kvp.Key == value2)
{
value2Rep = kvp.Value;
break;
}
}
if (value2Rep > value1Rep)
{
v.Value1 = value2;
v.Value2 = value1;
}
//show the result:
Console.WriteLine("{0} - {1}", v.Value1, v.Value2);
}
Console.ReadLine();
}
}
class Values
{
public int Value1 { get; set; }
public int Value2 { get; set; }
}
Tell me what do you think.
Mitja
Why would you delete whole dgv. You dont need to. If you want to delete the row, and the dgv is bound to a data source, you have to remove the row from this data source.
Then will surely work.
Mitja
Whaz I dont understand also, if the sorting (yout last request). I dont get it why you changed the 3 and 2.
Maybe would be a good idea to explain what all these numbers mean, and what is the connection between them.
I will add additional row to ask you if I understand this correctly:
Lets say we have on beginning:
1 2
1 3
1 4
2 3
3 4
5 3
The output would be:
1 2
3 1 //here chnages, becuase 3 has 4 prepetitions
1 4
3 2 //here too
3 4
3 5 //and here
Am I right?
set of data in text file
1 2
1 3
1 4
2 3
3 4Now, i wan to read each item and count the frequency for each item.
ITEM FREQUENCY
1 contain 3
2 contain 2
3 contain 3
4 contain 2
Where did you get this last table? How, explain more? 1 contaions 3 ? whats is that suppose to mean?
Mitja
What exactly are you trying to do?
Do you want to chage the state of the tick? From true to false in runtime?
Because your code has no sence to checking if the tick is in both states.
Just tell me what exactly would you like to do.
Mitja
:) I thought it has to work.
bye
And close the thread (mark as answered).
Mitja
and your solution is... ?
Better check what your dataTable holds. Check if there is data inside of it 8in datatable of the dataSet). If its empty, then surely can happen much. If there are data, it has to be shown in listView (with using my code):
ListView.Items.Clear();
DataTable dtable = myDataSet.Tables["CoffeeTypes"];
//Display items in the listview ctrl.
foreach (DataRow drow in myDataSet.Tables[0]) //you have to specify which table (or its name ot its index)
{
ListViewItem lvi = new ListViewItem(drow["CoffeeID"].ToString());
lvi.SubItems.Add(drow["coffeeName"].ToString());
lvi.SubItems.Add(drow["coffeePrice"].ToString());
lvi.SubItems.Add(drow["coffeeStrength"].ToString());
lvi.SubItems.Add(drow["Origin"].ToString());
lvi.SubItems.Add(drow["QuantityInStock"].ToString());
listView1.Items.Add(lvi);
}
Sure. There is no other way.
YOu have to use sql query (SELECT x FROM y WHERE a = b) if condition is needed.
All with a sqlCommand help (sqlCommandBuilder is not needed here).
In which format do you have "0030"?
So, what is the problem here?
This has got to work:
//clear the items if they exist:
ListView.Items.Clear();
DataTable dtable = myDataSet.Tables["CoffeeTypes"];
//Display items in the listview ctrl.
foreach (DataRow drow in myDataSet.Tables)
{
ListViewItem lvi = new ListViewItem(drow["CoffeeID"].ToString());
lvi.SubItems.Add(drow["coffeeName"].ToString());
lvi.SubItems.Add(drow["coffeePrice"].ToString());
lvi.SubItems.Add(drow["coffeeStrength"].ToString());
lvi.SubItems.Add(drow["Origin"].ToString());
lvi.SubItems.Add(drow["QuantityInStock"].ToString());
listView1.Items.Add(lvi);
}
Same as in mine example (I have only renamed it to "items".
As ddanbe said. I will only show an example:
DateTime dateNow = DateTime.Now;
// I will add just an example dateTime:
DateTime dateFromDataBase = new DateTime(2011, 3, 21);
//comparing both dates:
if (dateNow.Date == dateFromDataBase.Date)
{
//if both dates are equal do code in here:
}
else
{
//if not equal, do in here (if needed)
}
Mitja
Man, how do I know what is "SplitItem" variable? Is this a string (which cannot be), is an array or any other collection type?
PLEASE, try to paste all the code needed for us, if you want any decent help.
Lets says its an array (string[]). The same goes for "listItem. Is this a generic list( Dictionary<string, int> or is a SortedList<string, int> ?
You see, I am guessing, even if I am close to what it might be.
Lets see now what you can do:
string[] items = { "A", "B", "A", "C", "B", "A" };
Dictionary<string, int> dic = new Dictionary<string,int>();
foreach (string item in items)
{
if (!dic.ContainsKey(item))
dic.Add(item, 1);
else
{
int a = dic[item];
dic[item] = a + 1;
}
}
Hope it helps,
Mitja
try this:
int rowSelected = dataGridView1.CurrentCell.RowIndex;
What hash funstion do you mean? There is no operation with this name in Lambda Expressions. But here is an example with Intersept operator:
SortedList<int, int> s1 = new SortedList<int, int>();
for (int i = 0; i < 10; i++)
s1.Add(i, i); //ADDED: 0,1,2,3,4,5,6,7,8,9
SortedList<int, int> s2 = new SortedList<int, int>();
for (int i = 0; i < 10; i++)
s2.Add(i * 2, i * 2);//ADDED: 0,2,4,6,8,10,12,14,16,18,20
var s3 = s1.Intersect(s2).ToList().Count; //RESULT IS 5 - only number(of keys 0,2,4,6,8)
Hope it helps,
Mitja
You want to sort how? What has to do spaces , tabs, - * here?
Anytime. Iam in the mood to do coding atm, this doesnt accur often :) but its time for bed.
best regards,
Mitja
LOL... I got the code in my 1st attempt. No repairing it. Damn Im good :)
Here it is:
//form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(this);
f2.Show();
}
public void SelectingText(string searchText, bool bSelectUp)
{
int position = textBox1.Text.IndexOf(searchText);
if (position > 0)
{
if (bSelectUp)
textBox1.Select(position, (textBox1.Text.Length - position));
else
textBox1.Select(0, (position + searchText.Length));
textBox1.ScrollToCaret();
textBox1.Focus();
}
else
textBox1.DeselectAll();
}
}
//form2:
public partial class Form2 : Form
{
Form1 f1;
public Form2(Form1 _f1)
{
InitializeComponent();
this.f1 = _f1;
//select by defualt the up radio button (so one will always be selected):
radioButton1.Checked = true;
}
private void button1_Click(object sender, EventArgs e)
{
string value = textBox1.Text;
bool bSelectUp = (radioButton1.Checked) ? true : false;
f1.SelectingText(value, bSelectUp);
}
}
Hope it helps,
Mitja
Ups.
So when will select UP:
will select the searching word and the rest of the letter to the right
When you will select DOWN:
will select the searching word and the rest of the letters to the left.
Am I right?
Do you mean if you have more equal words, the search will go forward (from let to right) when up radioButton will be selected?
What do you mean with radion buttons direstions?
YOu have only one "main textBox" so how will you be using these 2 radionbuttons? What would be the difference in selecting one or the other?
Mitja
Lets say you were doing C# for 3 years. But now we have to ask you, what were you doing in these past 3 years? Have you learned every single day (for at least 2-3 hours for day), or did you only occasionally used any book(s) and Visual Studio for your practice, or there was something in between?
You see what I mean, its a big difference in those 3 examples. Someone can be almost an expert, when the other doesn`t even know what is (means) an Object in OOP, or how to use Lambda Expressions for an instance.
I am learning C# for around 2 years, and I can say every day, sometimes I`m infront of the book and pc for a lots of hours, sometimes maybe only for an hour (when I have a hard times with my work). But Iam doing a progress, even if its tuff. There is so much to learn, and every day comes something new.
In programming there is many areas that you can work on (like sql, win form, some specific coding - algoritms, or sometihng else), so its hard to learn all at ones. Best way is to go step by step, to learn sometihng and that you do that good. Its worthless to try to learn all, because on the end you will no nothing, or becuase there will be to many stuff at ones, or you will simply forget what you have been learing a couple …
And do you have any clue what might cause the dictionary change? Because from the code, which looks clean, I cant find any reason for that.
Make sure that the connection to the database is open when doing reader.Read()
This is not the point here, it would have beed other error.
The problem is that he (as far as am think) wants to create 2nd reader, when 1st one is nor even closed yet.
If so, Close() 1st reader and set it to null (reader = null;) and then create another reader (as the 1st one).
Hope it helps,
Mitja
In which format do you save it? I hope not it txt. Iam affraid best solutin your be to use *.doc. But in this case you would need some additional code for Converting the cryped doc code.
What exactly are you trying to do? Before pasting some code here, would be nice to do some basic explanation of what is your project aobut. So we can have a clue about, and then its a way easier for us to help you out.
Now I dont know what are you doing, and how can I / we help you. So some basic explanation would be welcome.
I hope you understand.
Mitja
This code will do what you want:
string path = @"C:\1\test21.txt";
if (!File.Exists(path))
{
FileStream fs = File.Create(path);
fs.Close();
}
using (TextWriter tw = File.AppendText(path))
{
foreach (string item in listBox1.Items)
tw.WriteLine(item);
}
It creates a file if it does not exist yet, and if it does exist, it only appends the text into a new line.
Its tested and it works 100%.
Hope it helps,
Mitja
What now, is it working or now? You marked the thread as answered, why? Your last post didnt sound like it has beed salved.
Or you have you found out the solution?
:) Indeed. Maybe we can find out some thing which even Microsoft doesn`t know, and it will be implemented in VS 4.5
:)
string writetext = ListItemCom2.SelectedItems.ToString();
Is the error. You want to get an array and put it into a stirng. No go.
You needa loop, which will go through the listBox items, like:
FileStream fs2 = new FileStream(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt", FileMode.Append, FileAccess.Write, FileShare.Write);//create text file
StreamWriter sw2 = new StreamWriter(fs2, Encoding.Default);
for (int i = 0; i < ListItemCom2.SelectedItems.Count; i++)
sw2 = File.AppendText(@"C:\Users\Fish\Desktop\fyp-coding\Combine.txt");
sw2.Flush();
sw2.Close();
Hope it helps,
Mitja