|
Here is a hint:
if (!File.Exists("startlog1.txt"))
{
stw1 = new StreamWriter("startlog1.txt");
}
else
{
File.AppendText("startlog1.txt");
}
What are you doing when the file exists vs. when the file does not exist? What is the value of stw1 after this if statement in the two scenarios.
|
|
|
|
|
Hi...
The best overloaded method for WriteLine() with two arguments are
WriteLine(string,object) and WriteLine(string,object[])
But your method call supplies both the arguments as string...
soo doo something like this...
string logString = "Started time: " + start1.ToString();
stw1.WriteLine(logString);
This may help i believe...
Have a Happy Coding.....
|
|
|
|
|
You are still using the objects start1 and stw1 . One (or both) are null. Your code by itself will not solve this error.
|
|
|
|
|
Hi Colin.....
I dont get you... i just used the string value logString to be passed to the stream writer.....
Why do you say it will not solve the problem???
suggest...
Have a Happy Coding.....
|
|
|
|
|
King Julien wrote: i just used the string value logString to be passed to the stream writer.....
True, but all you did was move the original posters problem onto a different line of code. You moved his problem on to the line of code that generates the content for logString .
The OP's problem was a NullReferenceException . Your code will still produce a NullReferenceException for the reasons I gave in my previous post.
|
|
|
|
|
I'm looking to write a program that reads mouse inputs at the most 'raw' level possible (directly from the HID driver if possible), and modifies the output before it reaches Windows.
The end goal is a program that allows the creation and modification of custom acceleration curves, which can by applied globally while the program runs in the taskbar.
As a side interest, I am also wondering if it is possible to hook the actual drawing of the mouse cursor in Windows (XP, Vista) and replace it with a customized output (animated with alpha transparency).
Thanks so much for any suggestions
|
|
|
|
|
Al Mendoza wrote: I'm looking to write a program that reads mouse inputs at the most 'raw' level possible (directly from the HID driver if possible), and modifies the output before it reaches Windows.
That means writing a device-driver, a general replacement for the mouse-driver. I think that it will prove quite a challenge.
Al Mendoza wrote: As a side interest, I am also wondering if it is possible to hook the actual drawing of the mouse cursor in Windows (XP, Vista) and replace it with a customized output (animated with alpha transparency).
You're in luck! There's a really cool article[^] on CP on cursors
I are troll
|
|
|
|
|
I'm having some trouble getting control on the active mdi child of a richtextbox that is inside of a tab control. All the common text editor functions work in the first richtextbox. Like a right click contextmenu, toolbar functions, font etc. Its name is RichTextBox. I have a treeview control that I added a doubleclick event that creates a new mdi tabpage in the existing tabcontrol and in that tabpage adds a new mdichild richtextbox containing the text of the file that was clicked in the double click event. The tabpage and the richtextbox are named the name of the file + 1 to the count of the current number of tabs. I also have some buttons on the main form that will open specific text files in the original richtextbox (this is easy enough since I have a static richtextbox with a name I can point to) What Im having trouble with is getting control of the currently active richtextbox. this.ActiveMdiChild does not seem to work. I would like to eliminate the static richtextbox and change all the controls in the mainform to point to the active mdichild instead. I think part of my problem maybe that the tabpage is also called as the active mdichild in order to handle my close events(this was recommended in order to have easier control in making them the active) i have put on the tab. I tried it with a different name but that didn't seem to help either. An example of one of the ways I have tried to get control is from the msdn forums. All i get
Form activeChild = this.ActiveMdiChild;
if (activeChild != null)
{
try
{
RichTextBox theBox = (RichTextBox)activeChild.ActiveControl;
if (theBox != null)
{
Clipboard.SetDataObject(theBox.SelectedText);
}
}
catch
{
MessageBox.Show("You need to select a RichTextBox.");
}
Th code Im using to create the new tabs with the richtextbox
System.IO.StreamReader StreamReader1 = fi.OpenText();
String text = StreamReader1.ReadToEnd();
TextEditorForm editForm = new TextEditorForm();
TabPage childTab = new TabPage();
editForm.MdiParent = this;
editForm.Name = "Child" + createdTab.ToString();
editForm.Text = lblFile.Text;
childTab.Name = editForm.Name;
childTab.Text = editForm.Text;
TextEditortabControl.TabPages.Add(childTab);
editForm.richTxtBox.Parent = childTab;
editForm.richTxtBox.Name = editForm.Name;
editForm.richTxtBox.ContextMenu = new ContextMenu();
ToolStripMenuItem newtexteditMenuTab = new ToolStripMenuItem();
newtexteditMenuTab.Text = editForm.Text;
newtexteditMenuTab.Name = editForm.Name;
newtexteditMenuTab.Click += new EventHandler(newMenuTab_Click);
TextEditortabControl.SelectTab(childTab);
editForm.EditText = text;
editForm.Show();
createdTab++;
StreamReader1.Close();
the TextEditorForm
public TextEditorForm()
{
this.richTxtBox = new RichTextBox();
this.richTxtBox.Dock = DockStyle.Fill;
this.richTxtBox.Multiline = true;
this.richTxtBox.ScrollBars = RichTextBoxScrollBars.Vertical;
this.richTxtBox.WordWrap = true;
this.richTxtBox.DetectUrls = true;
this.richTxtBox.Visible = true;
this.Controls.Add(this.richTxtBox);
}
I know if I can just get 1 control working on the active mdi child the rest will follow easy enough.
This seems like it should work and I'm been stuck on this for a few days now scratching my head and cant figure out what I'm doing wrong maybe more eyes will help. This is my first c# application and I have only been at it for a month or so now so go easy on me lol
|
|
|
|
|
I am having a little difficulty in understanding exactly what it is that you are doing, since you seem to be a little bit confused about what 'MDI' is.
However, that has never stopped me before.
Firstly, please forgive me if I seem to be going at a too simplistic level, but you said you are a beginner, so that is why.
The TabControl has a SelectedTab property, which returns a TabPage .
The TabPage control, like most Windows Forms controls, has a Controls property. This is a collection of all controls within the TabPage .
So we can find the active tab of the TabControl (SelectedTab ) then all that is needed is to go through all the controls until a RichTextBox is found. (This assumes that there is only one RTB on each tab)
If you Google for 'iterating the controls collection C#', you will get lots of hits, find one that you like and experiment with it.
Hint: You will need to cast the result to RichTextBox e.g.
RichTextBox result = (RichTextBox)foundControl;
Hope this is of some use.
Good luck!
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
thanks for the advice and the attempt at helping me however im long past that and have tried every example I could fine. I know google and how to use it. If it was another forum post i was looking for i would have went there. I have tried and tried and what ever im doing is not working. hints dont help me. i posted my code because i am at the point i neeed help and was hoping someone, another set of eyes could see what i was doing wrong and perhaps give me a code example on how to get copy selected text from the active tabs richtext box. And im not confused about MDI perhaps theres someone that understands what im trying to do that can provide some insite.
thanks
|
|
|
|
|
ok ill dumb down my question. picture this i have a wordpad like editor with a ton of custom controls. everything works great. I added a tabcontrol. tabpage1 has my original richtextbox everything works great still. i added the ability to open new documents in a new tab and of course none of my custom controls work in any of the new documents. So in order to try to make all my current controls work in the new documents(richtextboxes) I dont want to re-write controls for every knew document. I just want to use the current controls on what ever the active richtextbox is. So thats when I went to mdi for easier control of both the tabs and the current open documents. Perhaps theres a better or easier way. I have tried more ways then my wife would care for. The above codes are just part of an example from what I am working on.
|
|
|
|
|
Sorry I was unable to help.
I hope that someone does understand better than I did, and is able to help you out.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
how do i program to close notification ballon tip manually. Thanks
|
|
|
|
|
How do you mean?
Like how some balloon tips have a (X) Close button on the top-right corner or programmatically or are you talking about closing a balloon tip of another program?
|
|
|
|
|
Hello everyone, I have to find the biggest rectangle(square) in a matrix which holds only even numbers.
If I have normal rectangle with even numbers my code is working, but if I have some strange figure like two rectangles in one my code fails.
Please anyone who have done something similar before to give me a clue how to get this rectangle.
I am not very sure is it my post for here, so if its not please excuse me.
Here is my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace EvenNumberMatrix
{
class EvenNumbers
{
public static int[,] givenArray ={
{5,2,6,8,5,9},
{7,5,3,2,4,7},
{9,6,3,6,4,2},
{8,5,3,6,2,2},
{9,6,3,7,4,7},
{8,5,6,2,2,5}
};
public static int maxCol = 0, maxRow = 0;
public static int[,] tempGivenArray;
static void PrintArray(int[,] a)
{
for (int x = 0; x < a.GetLength(0); x++)
{
for (int y = 0; y < a.GetLength(1); y++)
{
if (a[x, y] != 0)
{
Console.ForegroundColor = ConsoleColor.Green;
}
else
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write(a[x, y] + " ");
}
Console.WriteLine();
}
}
static void Main(string[] args)
{
int number = 6; int countRows = 0, countCollums = 0, x, y;
tempGivenArray = new int[number, number];
for (int i = 0; i < number - 1; i++)
{
for (int j = 0; j < number - 1; j++)
{
if (givenArray[i, j] % 2 == 0 &&
givenArray[i + 1, j] % 2 == 0 &&
givenArray[i, j + 1] % 2 == 0 &&
givenArray[i + 1, j + 1] % 2 == 0)
{
tempGivenArray[i, j] = givenArray[i, j];
tempGivenArray[i + 1, j] = givenArray[i + 1, j];
tempGivenArray[i, j + 1] = givenArray[i, j + 1];
tempGivenArray[i + 1, j + 1] = givenArray[i + 1, j + 1];
}
}
}
for (int i = 0; i < number - 1; i++)
{
for (int j = 0; j < number - 1; j++)
{
x = i; y = j;
while (tempGivenArray[x, y] > 0)
{
x++;
countRows++;
if (countRows > maxRow)
maxRow = countRows;
if (x > number - 1)
break;
}
x = i; y = j; countRows = 0;
while (tempGivenArray[x, y] > 0)
{
y++;
countCollums++;
if (countCollums > maxCol)
maxCol = countCollums;
if (y > number - 1)
break;
}
countCollums = 0;
}
}
PrintArray(tempGivenArray);
Console.WriteLine("The biggest rectangle with even numbers is: "+maxCol * maxRow+" positions!");
}
}
}
Thank you very much in advance
|
|
|
|
|
Hi,
I haven't studied it all in detail, I do have two things for you to reflect on:
1.
why are you using a second int matrix, where 0 means one thing, and non-zero another; all you are interested in is odd or even. BTW: what would happen if some of the input numbers are zero (zero is even!)
2.
when checking for an even subrectangle, how come you have two while loops that are not nested?
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
Thank you for your reply
1. I am using zero based matrix for better visualization and when I make logic conditions to check the numbers are they even is easier for me to check are they more than zero. In my case I have to check for numbers >0.
2. I tried with nested while loops but the result is the same, or my knowledge is not enough to figure it out how to check for rectangle, for that I have nested for loops to get the current position and with while loops I am checking from current positions are they any rectangle existing.
Greetings
|
|
|
|
|
Hello, I have populated my WPF listbox from c# code like this:
private void button1_Click(object sender, RoutedEventArgs e)
{ listbox1.Items.Add("blah"); }
Well, the items appear in the listbox, but I CAN'T SELECT THEM INDIVIDUALLY. I've also found that the "index" property is the same for each item.
However, if I populate the listbox from XAML, it works PERFECTLY: selection is OK, indexes are different.
What's wrong with my code?
|
|
|
|
|
I've found it out, this question is out of... question
|
|
|
|
|
I have two pages. Page A cross postbacks to Page B. On both pages I have a hidden field. I want to set Page B's hidden field with the value from Page A. The problem is that when it leaves Page A a value is in the hidden field. However when it Page B initializes (which is where I am checking the value and writing it to the new hidden field.) the value is an empty string. I have stepped through the code to make sure that when it leaves Page A there is a value and when it "arrives" Page B there is an empty string.
I cannot figure out why this is happening. Can anyone assist? Here are the pieces of my code:
protected void Page_Init(object sender, EventArgs e)
{
Page pg = PreviousPage;
if (pg != null)
{
HiddenField hfPrevStudent = Helpers.FindControl<HiddenField>(pg, "hfStudent");
HiddenField hfPrevSchool = Helpers.FindControl<HiddenField>(pg, "hfSchool");
if (hfPrevStudent != null)
{
hfStudent.Value = hfPrevStudent.Value;
hfSchool.Value = hfPrevSchool.Value;
}
}
}
~Candi
|
|
|
|
|
Hi! I am doing a project about a game - a 5x5 grid with letter. The player must search words from the grid.
void SeekWords(string word, int[,] visited, int x, int y)
{
if (!(x < 0 || x > 4 || y < 0 || y > 4) && visited[x, y] == 0)
{
visited[x, y] = 1;
SeekWords(word + Grid[x, y], visited, x - 1, y - 1);
SeekWords(word + Grid[x, y], visited, x - 1, y);
SeekWords(word + Grid[x, y], visited, x - 1, y + 1);
SeekWords(word + Grid[x, y], visited, x, y - 1);
SeekWords(word + Grid[x, y], visited, x + 1, y - 1);
SeekWords(word + Grid[x, y], visited, x + 1, y);
SeekWords(word + Grid[x, y], visited, x + 1, y + 1);
visited[x, y] = 0;
}
}
This method is good, but for words longer than 6 letters it's too slow. Are there other algorithms for searching the matrix which are faster?
Thanks
Still learning...
|
|
|
|
|
I can understand this code is slow :P.
But I don't really get what you want to achieve? Can you maybe clearify that?
Is it like a snake? which will check for a word like this:
{a, b, c}
{d, e, f}
{h, i, j}
and if you try the word 'abf' it will find it and starts at point 0,0 and ends at 2,1. But if you try 'abj', it won't find anything?
If it is, I think it is funny and I'll try to code it , if it isn't please explain how and what you mean exactly.
|
|
|
|
|
Yes, it is like a snake. The word 'abj' form your example isn't valid according to the rules of the game. Each next letter must be a neighbor to the previous. For example: 'adi', 'hec'...the word 'ada' is not valid (a letter from a box of the matrix is used only once).
I was thinking of a solution with combinations and a check if the letters are neighbors (this is only an idea, I haven't thought about it much) .
Still learning...
|
|
|
|
|
Hi,
I have several remarks:
1.
From every position you could move to 8 adjacent squars, however you only have 7 SeekWords calls; you skipped one, it will become even slower!
2.
I don't see the word length anywhere in your code; the code as it is will search for all the longest words; i.e. on a 5*5 board it will find several 25-character words, and a lot of shorter ones because "the head of the snake" gets caught in a dead-end. On a 6*6 board, it will find several 36-character words, and a lot of shorter ones, including all the ones found for a 5*5 board. The complexity of the job is more than exponential in z, where z is the size of the board.
Therefore, there is no decent general solution; it grows at an enormous pace.
3.
However, there are ways to improve, not so much by changing the algorithm (you have to investigate all possibilities, there is no escape), but rather by changing the implementation. The biggest factor I can see is the matrix. I suggest you smash it into a linear array, i.e. let Grid[0]...Grid[z-1] represent the first row, Grid[z]...Grid[2*z-1] be the second row, etc.
That means the current position (former x,y) becomes p, and a line such as SeekWords(word + Grid[x, y], visited, x - 1, y - 1); becomes SeekWords(word + Grid[p], visited, p-z-1);
which just is simpler, hence faster.
4.
visited[] could hold booleans instead of ints with values 0 and 1; I don't expect that to influence performance. Also visited[] could be a class member, making it unnecessary to pass it as a method parameter; doing this probably would slow things down, since class members are "farther away" than method parameters.
5.
if x equals 0 there is no need to try three SeekWords(x-1) since they will fail their initial test.
similar for y==0, x==z-1 and y==z-1
you could perform one test inside SeekWords as in
SeekWords(...) {
if (...) {
if (x>0) {
SeekWords(... x-1 y-1)
SeekWords(... x-1 y)
SeekWords(... x-1 y+1)
}
if (x<z-1)>etc.
Of course, in a one-dimensional array it becomes slightly more complex.
One easy and fast way is to make 8 extra boards, one for each direction, initialize them properly and then just do eight times something like if (canStepInDirectionMM[p]) SeekWords( p-z-1)
canStepInDirectionMM[] would contain true everywhere except for those starting positions that don't allow a step in the direction (use M for minus, Z for none, P for plus in both X and Y direction).
Doing so, the complex if at the start of SeekWords reduces to if(!visited[p]) {...}
I expect the above to speed up things by say a factor of 3, so maybe it becomes acceptable for a 6*6 board, but I see no easy way to tackle larger boards unless there were an acceptance test on the words, say "an exact length of N letters", or "all letters appear in alphabetical order". If so, the nesting of SeekWords with be curtailed most of the time, leaving only very few (relatively speaking) combination to be investigated.
Luc Pattyn [Forum Guidelines] [My Articles]
- before you ask a question here, search CodeProject, then Google
- the quality and detail of your question reflects on the effectiveness of the help you are likely to get
- use the code block button (PRE tags) to preserve formatting when showing multi-line code snippets
|
|
|
|
|
In the code I have posted, some parts are missing, including the one with the word length. For word with maximum length 5 and minimum length 3 is very good. I will consider your suggestions.
I have an idea. If the current word is 4 characters, the program will also search the first four characters of the dictionary words. If there isn't a match, the words which derive from the current also won't have a match.
Current word: ac
Dictionary:
abcde
abefg
abegh
In the example there is no word starting with 'ac', so the words derived from 'ac' won't be in the dictionary. This will save a lot of roaming in the grid.
Still learning...
|
|
|
|
|