darkagn 315 Veteran Poster Featured Poster

Sorry no. String is also an object, but to use it in your example of how to create objects is misleading, because C# allows you to take a shortcut as though it were a primitive type.

Human human = new Human(); // creates an object
int daysToNextBirthday = 43; // creates a primitive by assigning a value directly, no call to constructor
string monthOfBirth = "February"; // looks like a call to a primitive but it is just a shortcut, it actually creates an object for you - String is the only class I can think of that does this
darkagn 315 Veteran Poster Featured Poster

I meant the static method of the String class IsNullOrEmpty , not that the string itself is static. This static method checks whether the string is unitialised or initialised to String.Empty .

vedro-compota commented: +++ +1
darkagn 315 Veteran Poster Featured Poster

Your call to Message.Show is wrong, you need to add String.Format like so:

MessageBox.Show(String.Format("{0}",hello2(2)));

Also, it doesn't matter what number gets passed as the parameter to your hello2 function, it will always show 10 / 5. A better implementation would be:

private void button1_Click(object sender, EventArgs e)
{
  MessageBox.Show(String.Format("{0}",hello2(10, 5)));
}

public int hello2(int numerator, int denominator)
{
  int hello = numerator / denominator;
  return hello;
}
darkagn 315 Veteran Poster Featured Poster

Yes you are correct, however string is a bit of a special case in the way that it is initialised. A better description of objects is to look at your post in your other thread where you had:

Human human = new Human();

In this example, you are creating an object of type Human. Inside the Human class there will be a constructor - this is a special method that allows you to create an instance of the object of type Human.

The reason why I say that string is a special case is that yes, string is an object, however it allows a shortcut in the way you create an instance of it - you don't have to use "new", you can just set its value as though it were a primitive type (lowest level data type in a language which stores a value but is not actually an object).

darkagn 315 Veteran Poster Featured Poster

Your call to create your new Human object is calling the constructor method for the Human class, it is not itself a constructor. Calling the constructor of a class is the only way of creating a new instance of your class. You can have a different method that returns a Human object, but somewhere in that method it must call a constructor to create the instance.

darkagn 315 Veteran Poster Featured Poster

The following if-statement is equivalent to your switch statement. You can see from this code that Narue is correct:

if (fillnumb == 0)
  fillnumb = 1;
else if (fillnumb == 1)
  fillnumb = 2;
vedro-compota commented: ++ +1
darkagn 315 Veteran Poster Featured Poster

Also, you should use the static String.IsNullOrEmpty method to check for uninitialised strings.

darkagn 315 Veteran Poster Featured Poster

Without further information about the application you are building, the only minimum requirement is the .NET Framework 4.0. It can be downloaded freely from the MSDN website.

darkagn 315 Veteran Poster Featured Poster

Not if all of your methods are static. What I would do is make your methods non-static and pass the form to the constructor of the MathLogic class, then in your form add the property as suggested above and create a MathLogic instance.

darkagn 315 Veteran Poster Featured Poster

Can you pass the text box as a parameter to your Start method?

public static void Start(string e, TextBox answerBox)
{
  Entry = e;
  //Determine what to do to the entry. In this case 2 + 2
  // ...
  // update the text box with the answer
  try
  {
    answerBox.Text += Add(x, y).ToString();
  }
  catch
  {
    answerBox.Text += "SYNTAX ERROR";
  }
}
darkagn 315 Veteran Poster Featured Poster

In your form add a property like so:

public decimal Answer
{
  get
  {
    return Convert.ToDecimal(AnswerBox.Text);
  }
  set
  {
    AnswerBox.Text = value.ToString();
  }
}

Then in your other class add a reference to the form and set the Answer property accordingly.

class Calculator
{
  private MyForm form;
  public Calculator (MyForm aForm)
  {
    form = aForm;
  }
  public void Calculate()
  {
    // work out the answer
    //...
    // now set it in the form
    form.Answer = answer;
  }
}
darkagn 315 Veteran Poster Featured Poster

I see two potential issues with your code:

// first call to ReadLine() advances the stream
foreach (string record in reader.ReadLine().Split('|'))
{
  // first time will read the second line because of first call
  while ((line = reader.ReadLine()) != null)
  {
    if (Regex.IsMatch(line, searchText, RegexOptions.IgnoreCase))
    {
      // the text here will be overwritten on each pass, try += instead
      rtbRecord.Text = line;
    }
  }
}
darkagn 315 Veteran Poster Featured Poster

You need to join to the tblRespondentsChildren table twice, once for each child.
Something like this should give you a start:

SELECT r.FirstName, r.Surname FROM tblRespondents r
INNER JOIN tblRespondentsChildren rc1 ON r.RespondentID = rc1.RespondentID
INNER JOIN tblRespondentsChildren rc2 ON r.RespondentID = rc2.RespondentID
WHERE rc1.DOB BETWEEN ('1998-09-01') AND ('1999-08-31')
AND rc2.DOB BETWEEN ('1995-09-01') AND ('1995-12-08')
AND rc1.SexID = 1
AND rc2.SexID = 1
Gdyson commented: Spot on +0
darkagn 315 Veteran Poster Featured Poster

There are a few things that could be better with your code, but your syntax error is caused by this:

i_EmpPayGrade) (s_String, i_Integer)

which I believe should be this:

i_EmpPayGrade, s_String, i_Integer)

You should really use a parameterised query to pass all of those variables to your query. Also, you should wrap your execution of the command inside a try-finally clause to ensure that the connection is closed if you get an exception.

EDIT: Also, quotes in SQL are single quote ', not ", so everywhere that you have "\"" + ... should actually be "'" + ..., but a parameterised query would help with that anyway.

darkagn 315 Veteran Poster Featured Poster

From the MSDN documentation on PictureBox:

// Stretches the image to fit the pictureBox.
   pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;

   MyImage = new Bitmap(fileToDisplay);
   pictureBox1.ClientSize = new Size(xSize, ySize);
   pictureBox1.Image = (Image) MyImage;

You can substitute pictureBox1 in the above example for choices[0] in your code. fileToDisplay is the string filepath. xSize and ySize are the dimensions of your image in pixels (both are int's).

darkagn 315 Veteran Poster Featured Poster

If that is the case then the issue is specific to Vista. The PasswordChar property should only work (according to the MSDN documentation) if the textbox's Multiline property is false. However Vista allows it to be true! I assumed in my previous post that Windows 7 would follow the same as Vista, but obviously not...

darkagn 315 Veteran Poster Featured Poster

Try this:

protected readonly int ID = ID.GetNextID();
darkagn 315 Veteran Poster Featured Poster

Your first dialog is close but should make use of the overriden method for MessageBox.Show like so:

DialogResult r = MessageBox.Show("Do you want to save changes?", "Save Changes?", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);

Then you can check the variable r to see which button the user pressed and take the appropriate action:

if (r == DialogResult.Yes)
  SaveChanges();
if (r != DialogResult.Cancel)
  Close();

Then in your SaveChanges() function, you can use a SaveFileDialog to prompt the user for where to save the file. Have a go at this before I just blurt out the answer...

darkagn 315 Veteran Poster Featured Poster

Hi all,

Well this is my 1000th post here at DaniWeb and I thought "What better way to celebrate than by posting my first ever code snippet?"

This problem occurred in an application that I am working on. I have a password field in my form that was specified as Multiline, and it correctly worked on my PC, but my friend's PC was showing it as text. It turns out that Windows XP expects password fields to be single line only, but Vista (and I assume Windows 7) doesn't care if it is a single or multi line text box. In the code snippet you see three text boxes being added to a form. On XP, only the first field will correctly display a * character in place of the text, whereas in Vista all three will.

I guess a password field would be unusual to accept multiline text, but I overlooked this property when attempting to solve the issue. I hope this sheds some light for someone out there...

kvprajapati commented: Congrats! you are awesome. +11
darkagn 315 Veteran Poster Featured Poster

I prefer your method personally. I go one step further and put enums in their own file with an extension of the TypeConverter class to perform casts between an enum and a string.

darkagn 315 Veteran Poster Featured Poster

I'm trying to open a file from a folder called "Assginement" however, when the program get's released, it might not be in a folder called this. What could I do? I've tried everything.

If it is in the folder where the application is running from, use Directory.GetCurrentDirectory() static method. Otherwise, you need to use a relative path to the file you are trying to open. Decide now where the directory will be and stick to it. Maybe create a constant and use this whenever you need to access the directory, that way you only need to change it once if you decide to at a later date.

The other problem is that it creates a file a file, and places it within the same folder, however, it does not know what the foler is going to be called?

There are several ways to get the folder name, but I like to use the one I mentioned above - Directory.GetCurrentDirectory();

darkagn 315 Veteran Poster Featured Poster

Check out the Microsoft Sync Framework. It's a bit of work to set up in your application but once up and running it works a treat.

darkagn 315 Veteran Poster Featured Poster

Change the parameters line to:

command.Parameters.AddWithValue("@user", selectuser);

@user is the parameter, username is the column.

darkagn 315 Veteran Poster Featured Poster

I have noticed a small discrepancy between my Avatar in posts and my profile page. On the left you will notice that I have 995 posts to my name, but my profile screen says 997. I am unsure what the correct number should be.

Normally I wouldn't worry about such a trivial difference but I am fast approaching the 1000 post mark and my 1000th post will be the cause of much celebration :)

darkagn 315 Veteran Poster Featured Poster

You could use the String.Split function to split each line according to where each space is. Then you could just substring from the = signs to get the digits. Something like this (untested but should give you an idea):

while ((s = sr.ReadLine()) != null)
{
  string[] chunks = s.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  foreach (string part in chunks)
  {
    int i = part.IndexOf("=");
    if (i != -1)
    {
       int digit = Convert.ToInt32(part.Substring(i+1));
       // do something with digit...

    }
  }
}
darkagn 315 Veteran Poster Featured Poster

This is a relatively simple task in C#. Copying files from one place to another can be done using either of the File.Copy methods depending on your needs. Reading and writing from the database in C# is also straight forward. There are many tutorials out there, Google should provide many links.

darkagn 315 Veteran Poster Featured Poster

You are on the right track, but try to think of interface in this question as WHAT something does, while implementations are HOW something does it. What are the repercussions of changing WHAT something does as opposed to HOW it does it?

Imagine you write an application that uses code that I have written. What happens to your application if I change HOW my code does something? What happens to your application if I change WHAT my code can do?

darkagn 315 Veteran Poster Featured Poster

I would probably agree with that, but why do you think that implementations change more often than interfaces?

darkagn 315 Veteran Poster Featured Poster

Oops, sorry that line should be:

myTextBox.Text = Convert.ToInt32(myTextBox.Text).ToString("0:###,###,##0");
darkagn 315 Veteran Poster Featured Poster

Hilarious post, thanks for sharing almostbob :)

And all you lot, come to Australia and visit us eh?

darkagn 315 Veteran Poster Featured Poster

Australian Rules Football. See the AFL website for details of this amazing sport.

darkagn 315 Veteran Poster Featured Poster

Hi Ceerno,

Sorry to say but there are a number of problems with your code.

The first thing I noticed was that your for-loops run from index 1 up to and including the Array length. Arrays are indexed from 0, so it should run from 0 to (but not including) the Array length (use strictly < rather than <= in your for-loop).

Also, you never actually set or read any values in your array, you simply read and set the int matrix variable. Even if you were setting the values in the array in your first method you completely wipe the array clear at the start of your second method.

In your second loop you constantly read the matrix variable without updating it, so you are reading the same value over and over again.

Hope this gives you some idea of how to continue.

darkagn 315 Veteran Poster Featured Poster

That ContextMenuStrip is not standard for a TextBox (which defaults to a null ContextMenuStrip) so either you are using a different control to System.Windows.Forms.TextBox or you have added that strip yourself. You can't have two ContextMenuStrips on a single control, however you could switch between two different strips if you need to.

Alternatively, you could add a ContextMenu (as opposed to a ContextMenuStrip) and this will take precedence over the strip in a right-click operation. What you can then do is listen to the Opening event for your ContextMenu and cancel it from opening if you want the strip to open instead.

darkagn 315 Veteran Poster Featured Poster

Hi haripriyamca05 and welcome to DaniWeb :)

Please note that there is an ASP.NET forum here at DaniWeb where you can ask ASP.NET questions.

That said, all you need to do in your popup form is listen to the form Closing event, and read the TextBox.Text properties for each text box in your form.

Example:

this.Closing += new EventHandler(form_Closing);

private void form_Closing(object sender, CancelEventArgs e)
{
   string aString = myTextBox.Text;
   // do something with aString here...

   string anotherString = myOtherTextBox.Text;
   // do something with anotherString here...

   // and so on...
}
darkagn 315 Veteran Poster Featured Poster

I think you are talking about the TextBox.ContextMenuStrip property? You can simply create a ContextMenuStrip (in Designer or in Code) and assign it to this property. If the property is not set, there is no ContextMenuStrip for the TextBox, that is there is no "inbuilt" ContextMenuStrip.

Example:

ContextMenuStrip cms = new ContextMenuStrip();
// add some items here...
// ...
myTextBox.ContextMenuStrip = cms;
darkagn 315 Veteran Poster Featured Poster

Hi TheDocterd,

This looks like Visual Basic code. It might pay to ask your question in the Visual Basic 4 / 5/ 6 forum where someone who knows VB can help you. Also, please use code tags when posting snippets of your code for readability.

Good luck and happy programming :)

darkagn 315 Veteran Poster Featured Poster

Oops sorry, just re-read your post and realised you were asking if you can format while the text is being entered. The answer to this question is yes, you can handle the TextChanged event in your text box, and then format the number entered. For example:

private void myTextBox_TextChanged(object sender, EventArgs e)
{
  myTextBox.Text = myTextBox.Text.ToString("0:###,###,##0");
}
darkagn 315 Veteran Poster Featured Poster

Check out the MaskedTextBox class. This is a text box that all data entry must follow a specific mask (format).

darkagn 315 Veteran Poster Featured Poster

If you mean a barcode scanner, then each scanner will have its own communication protocol that you can talk to via serial port connection or some other means. You will need to read up on the technical specifications for the model you want to use. Often manufacturer's websites will have these documents.

darkagn 315 Veteran Poster Featured Poster

You need to handle incoming connections on one thread, then handle each connection on an individual thread. This tutorial is a really good starting point for a TCP client-server model.

darkagn 315 Veteran Poster Featured Poster

Hi AndreiZ3 and welcome to DaniWeb :)

You need to cast the tag back to a datarow, then you can access each part by their names, using the methods in the Convert class to convert to the datatype required. For example,

ListViewItem lvi = lvProductsOrdered.Items[lvProductsOrdered.SelectedIndices[0]];
if (lvi.Tag.GetType() == typeof(DataRow))
{
  DataRow dr = (DataRow)(lvi.Tag);
  int qtyOrdered = Convert.ToInt32(dr["QuantityOrdered"]);
  // and so on...
}
darkagn 315 Veteran Poster Featured Poster

You can set the date format for your server using the SET DATEFORMAT function, but this will set the format for all dates in your system. I think the best way to go for you is to store the full date in the database (or even just 1-Aug-10 for all dates in August 2010) then parse them accordingly in your VB6 application.

darkagn 315 Veteran Poster Featured Poster

You can use the String.Replace method for this purpose. What I would do is replace the string [var hello = ""] with your new string. Let's say you have read the file contents into a string called contents. Here is basically how you would do it:

public string SpecifyHello(string newGreeting, string contents)
{
  // \" is an escape to allow " to be inserted into a string
  string search = "var hello = \"\"";
  string replacement = "var hello = \"" + newGreeting + "\"";
  contents = contents.Replace(search, replacement);
  return contents;
}

Hope this helps :)

darkagn 315 Veteran Poster Featured Poster

This is an example of a class Property. What it is doing is providing a way to retrieve (get) and store (set) a string called Input, which in this case points directly to the Text property of a text box called myTextBox. The following code is equivalent (but a lot more typing!):

private string _input;
public string RetrieveInput()
{
   return _input;
}
public void StoreInput(string value)
{
   _input = value;
}
// event handler to handle when the text changes in the text box
private void myTextBox_TextChanged(object sender, EventArgs e)
{
   StoreInput(myTextBox.Text);
}
darkagn 315 Veteran Poster Featured Poster

Hi svatstika and welcome to DaniWeb :)

The easiest way to do this is to provide a reference to the input in your forms like so:

public partial class Form1: Form
{
   // create a property to store the input from the user
   public string UserInput
   {
      get;
      set;
   }

   // event handler for when the button is clicked - open the new form and wait until the OK dialog result is received
   private void myButton_click(object sender, EventArgs e)
   {
      UserInput = String.Empty;
      Form2 newForm = new Form2();
      if (newForm.ShowDialog() == DialogResult.OK)
        UserInput = newForm.Input;
   }
}

public partial class Form2: Form
{
   // a property that can be used by Form1 to retrieve the input from the user
   public string Input
   {
     get { return myTextBox.Text; }
     set { myTextBox.Text = value; }
   }
}
ddanbe commented: Nice. +8
darkagn 315 Veteran Poster Featured Poster

There are lots of good tutorials out there, google will provide many.

As for your elapsed time question, there are a number of ways to do this but here is the way I like to handle it:

// remember the time at the start
DateTime start = DateTime.Now;
// do your work
Evaluate();
// get the time at the end
DateTime end = DateTime.Now;
// DateTime objects can be subtracted or compared
TimeSpan duration = end - start;
// build a string explaining the time elapsed
string timeElapsed =  String.Format("{0} Hours, {1} Minutes, {2} Seconds", Math.Floor(duration.Hours), Math.Floor(duration.Minutes), Math.Floor(duration.Seconds);
// update a label with the time elapsed string
label.Text = timeElapsed;

public void Evaluate()
{
  // do something here...

}

Hope this helps.

darkagn 315 Veteran Poster Featured Poster

Test Driven Development is a methodology where you write test cases before you write the actual code, then write code to pass the tests and no more. For example, say we were writing a class to describe a square and we want a method that gives us the perimeter of the square. We can immediately think of three test cases that must always be true for our square: Each side MUST be greater than 0 units in length; each side MUST be equal in length; and the perimeter is equal to 4 times the length of one side.

With these test cases in mind, we can write the following code:

class Square
{
   int sideLength;
   public Square(int aSideLength)
   {
      if (aSideLength <= 0)
         throw new ArgumentOutOfRangeException("Side must be greater than 0");
      sideLength = aSideLength;
   }
   public int Perimeter()
   {
      return 4 * sideLength;
   }
}

It should be obvious that the above code passes all three test scenarios.

darkagn 315 Veteran Poster Featured Poster

Directory.EnumerateFiles is one of many ways to do this.

darkagn 315 Veteran Poster Featured Poster

Lol, very true, well spotted :$

darkagn 315 Veteran Poster Featured Poster

Here is how you assign an event handler programatically (my example assigns an event handler to the Button.Click event):

public void SetupButton()
{
  Button myButton = new Button();
  // assign the event handler
  myButton.Click += new EventHandler(myButton_click);
}

// definition for the event handler code
// note that the signature for this method must match the Click delegate signature
private void myButton_click(object sender, EventArgs e)
{
   // here you put the code for performing a task when the button is clicked

}

Hope this helps :)