Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
I

ignrod

@ignrod
About
Posts
16
Topics
1
Shares
0
Groups
0
Followers
0
Following
0

Posts

Recent Best Controversial

  • Access to the path is denied
    I ignrod

    You can try something like this. I have modified your code slightly to place reading and writing inside the main loop. Also, I recommend having your streams inside a using block, it is simpler to write and safer if any exception is thrown. Note: This code is untested!!

        public static void Download(String strURLFileandPath, String strFileSaveFileandPath)
        {
            HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(strURLFileandPath);
            HttpWebResponse ws = (HttpWebResponse)wr.GetResponse();
            byte\[\] inBuf = new byte\[100000\];
            int bytesToRead = (int)inBuf.Length;
            int bytesRead;
            using (Stream str = ws.GetResponseStream()) 
            {
                using (FileStream fstr = new FileStream(strFileSaveFileandPath, FileMode.OpenOrCreate, FileAccess.Write))
                {
                    try
                    {
                        while ((bytesRead = str.Read(inBuf, 0, bytesToRead)) > 0) 
                        {
                            fstr.Write(inBuf, 0, bytesRead);
                        }
                    }
                    catch (Exception e) {
                        MessageBox.Show(e.Message);
                    }
                }
            }
        }
    
    C# help

  • Access to the path is denied
    I ignrod

    What you should do to avoid your buffer overflowing is to read some data to your buffer and then writing to the file, all within a loop. This is a very common technique, for example, to copy files.

    C# help

  • Strange List<string[]> behaviour...
    I ignrod

    You have to take into account that strArray is a reference type. It seems that when you add it to strArrayList, a reference is added. Then, strArrayList contains a list of references to string arrays, which are the same array since you have been reusing the variable without reasignating it. This is similar to what happens in the following case:

    public void Test() {
    string[] a = new string[] {"", ""};
    string[] b = a;
    a[0] = "A";
    b[0] = "B";
    Console.WriteLine("{0} {1}", a[0], b[0]);
    }

    C# algorithms data-structures question learning

  • Index Out of Range
    I ignrod

    Difficult to know without all the source code. However, from your description, seems that variable i in function AddText is out of range. You should check that i is positive and less than list1.Length. Something like this

    //Function to add string dynamically in Class1
    private List list1 = new List();
    
    public void AddText(string myText) {
    	if ((i < 0) || (i >= list1.Length)) {
    		MessageBox.Show(String.Format("i is {0}. Should be between 0 and {1}", i, list1.Length));
    		return;
    	}
    	if((list1\[i\] == "") && (k < 1)) {
    		k = i;
    		list1\[i\] = myText;
    		k++;
    	}
    }
    

    By the way, please mind the ampersand in line

    	if((list1\[i\] == "") && (k < 1)) {
    

    Also, if you are looping, shouldn't there be a loop somewhere? I can't see it.

    C# css database help

  • Export To Excel Advanced
    I ignrod

    I use ExcelLibrary which can be downloaded from Google code

    License is GNU LGPL.

    C# csharp com

  • I hope this is not contagious...
    I ignrod

    Beware of the "}" right after comment "// This is plain wrong - should be false" What is Konstanten?

    The Weird and The Wonderful json

  • Storing Info Between Events
    I ignrod

    Sorry for the delay. If you want to keep number of clicks inside the button, all you have to do is subclass Button class and add a field with the number of clicks. Then, override the OnClick method to increase the number of clicks by one every time the button is clicked. Don't forget to call the parent OnClick so that the normal button click behavior is executed. You have an example below.

    using System;
    using System.Drawing;
    using System.Windows.Forms;

    class CountClickButton : Button {

    int clickCount; // Number of clicks
    
    public CountClickButton() {
    	clickCount = 0;
    }
    
    // OnClick is called when the button is clicked. It must increase clickCount by one
    // and then execute the event handlers (which is done when base.OnClick
    // is executed).
    protected override void OnClick(EventArgs e) {
    	clickCount = clickCount + 1;
    	base.OnClick(e);
    }
    
    // Only allow other classes to read number of clicks. Number of clicks cannot be
    // modified by other classes.
    public int ClickCount {
    	get {
    		return clickCount;
    	}
    }
    

    }

    class CountClickForm : Form {

    CountClickButton b;
    
    public CountClickForm() {
    	b = new CountClickButton();
    	b.Text = "Click me!";
    	b.Size = b.PreferredSize;
    	ClientSize = new Size(700, 400); // Should be big enough. Fit to your own needs.
    	b.Left = (ClientSize.Width - b.Width) / 2;
    	b.Top = (ClientSize.Height - b.Height) / 2;
    	b.Click += new EventHandler(b\_clicked);
    	Controls.Add(b);
    	Text = "I count clicks!";
    }
    
    // b\_clicked is registered as an event. It will be executed when button is clicked,
    // after number of clicks is increased by one.
    void b\_clicked(object sender, EventArgs e) {
    	string text = String.Format("Button has been clicked {0} times.", b.ClickCount);
    	if (b.ClickCount < 1) { 
    		// This should never happen.
    		text = "Sorry, I was drunk when programming this. Cheers!!";
    	}
    	if (b.ClickCount == 1) {
    		text = "Button has been clicked once.";
    	}
    	if (b.ClickCount == 2) {
    		text = "Button has been clicked twice.";
    	}
    	Text = text;
    }
    	
    \[STAThreadAttribute\]
    static void Main() {
    	Application.Run(new CountClickForm());
    }
    

    }

    C# question

  • Storing Info Between Events
    I ignrod

    In fact, I have. If you have a look at the link you just posted, you will see a note in yellow color which says: "The static keyword has more limited uses than in C++. To compare with the C++ keyword, see Static (C++)." So, if you try the following code, it will not compile in C#

    class ClickCount {

    static void Main() {
    	ClickCount click = new ClickCount();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    }
    
    void IncreaseClickCountByOne() {
    	static int clickCount = 1;
    	clickCount = clickCount + 1;
    	Console.WriteLine(clickCount);
    }
    

    }

    Of course, member variables ( fields ) can be static or nonstatic. Any of them will keep their value between method calls.

    class ClickCount {

    static int clickCount = 1;
    
    static void Main() {
    	ClickCount click = new ClickCount();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    }
    
    void IncreaseClickCountByOne() {
    	clickCount = clickCount + 1;
    	Console.WriteLine(clickCount);
    }
    

    }

    class ClickCount {

    int clickCount = 1;
    
    static void Main() {
    	ClickCount click = new ClickCount();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    	click.IncreaseClickCountByOne();
    }
    
    void IncreaseClickCountByOne() {
    	clickCount = clickCount + 1;
    	Console.WriteLine(clickCount);
    }
    

    }

    Both work exactly in the same way.

    C# question

  • Storing Info Between Events
    I ignrod

    Sorry to disagree with you, but this behavior of the static keyword corresponds to C++ and not C#. In fact, if you try to set a local variable ( i.e. defined inside a method ) as static in C#, you will obtain a compiler error. In my opinion, the best option to have clickCount record the number of clicks is to define it as a member variable and not a local one.

    C# question

  • How to mark part of the image that appear on my application ?
    I ignrod

    That's probably true. In that case

    Image im = pb.Image; // Assume pb is your PictureBox
    Graphics g = Graphics.FromImage(im);
    g.DrawRectangle(Pens.Red, pbleft, pbtop, pbwidth, pbheight); // Draw red rectangle in your image.
    g.Dispose();
    pb.Image = im;

    C# question csharp wpf tutorial

  • How to mark part of the image that appear on my application ?
    I ignrod

    Maybe you want something like the following:

    Graphics g = Graphics.FromImage(pb.Image); // Assume pb is your PictureBox
    g.DrawRectangle(Pens.Red, pbleft, pbtop, pbwidth, pbheight); // Draw red rectangle in your image.
    g.Dispose();

    This will draw a red rectangle in your image. Is that what you want?

    C# question csharp wpf tutorial

  • Change .exe icon
    I ignrod

    If compiling from command line ( csc ) use the switch /win32icon. If developing in Visual Studio, go to Project, Properties, the in the Application Tab where is says Icon.

    Windows Forms csharp question asp-net winforms

  • Context Menu Location [Solved]
    I ignrod

    Thank you for your reply. The ContextMenuStrip has a Size property and can be used to place the Context Menu above the point I want to use. Interestingly, the Size of the ContextMenuStrip is wrong the first time Show is called, so finally I used the method ContextMenuStrip.Show(PointToScreen(p, ToolStripDropDownDirection.AboveRight).

    C# help tutorial question

  • Help: Can't format string using String.Format
    I ignrod

    As has already been pointed out, this format string works with numeric types. Why don't you try?

    double dAverageDensity = Double.Parse(strAverageDensity);
    strAverageDensity = String.Format("{0:0.00}", dAverageDensity);

    C# help tutorial

  • Center(ish) point of a polygon.
    I ignrod

    To calculate the centroid of a polygon: Source: http://en.wikipedia.org/wiki/Centroid[^] and http://local.wasp.uwa.edu.au/~pbourke/geometry/polyarea/[^]

    PointF centroid(PointF[] p) {
    // p is an array with the vertices of the polygon
    float cx = 0;
    float cy = 0;
    float a = 0;
    float ca;
    for (int i = 0; i < p.Length; i++) {
    ca = x[i] * y[(i + 1) % p.Length] - x[(i + 1) % p.Length] * y[i];
    cx = cx + (x[i] + x[(i + 1) % p.Length]) * ca;
    cy = cy + (y[i] + y[(i + 1) % p.Length]) * ca;
    a = a + ca / 2;
    }
    cx = cx / (6 * a);
    cy = cy / (6 * a);
    return new PointF(cx , cy);
    }

    By the way, I am not sure if what you need is a centroid. Hope this helps, anyway.

    Algorithms algorithms question

  • Context Menu Location [Solved]
    I ignrod

    Hi all, Using the Show() method of ContextMenu class ( in Windows.Forms ) it is possible to show a Context Menu. One of the arguments to the Show() method is the top left position of the Context Menu. What I know is, instead of the top left position, the bottom left position of the Context Menu. I would like to translate this somehow to the top left position, however, neither ContextMenu nor MenuItem seem to have a Height or Size property. Does anyone know how to fix the bottom left position of a Context Menu? Any help would be greatly appreciated. Thank you.

    modified on Saturday, August 14, 2010 11:23 AM

    C# help tutorial question
  • Login

  • Don't have an account? Register

  • Login or register to search.
  • First post
    Last post
0
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups