Loop the split
array with a foreach and add it to the same string + Environment.NewLine
and finally set the textBox1.Text
property with the string.
Loop the split
array with a foreach and add it to the same string + Environment.NewLine
and finally set the textBox1.Text
property with the string.
Deferred execution might not make a difference in smaller tasks. The point of lazy execution is to not retrieve the data unless you need it. For example in a linq expression against a table:
var a = customers.Where(Active).(c => c.Name);
Lazy execution would not load the whole table into memory, but instead take the next customer when MoveNext() is called on the IEnumerator. And as far as I know that would mean the GarbageColletor would be called fewer times.
You might be right also, that when used incorrectly it could result in a decrease in performance.
In this case, the foreach loop requires a ReadAllLines instead of a line by line read. So unless you need the whole file in memory at once there is no reason not to use while. Besides, the StreamReader uses lazy evaluation which increases performance... so they say
As far as I know foreach loops are always slower than while/for. Although the syntax looks clearer, I don't know if there are any other advantages to what he needs.
Your while condition is wrong. It should be while ((line = sr.ReadLine()) != null)
You could do this if you create your own Image class. Something like:
public class MyImage
{
string Path { get; private set; }
Image Image { get; private set; }
public MyImage (string path, Image image)
{
Path = path;
Image = image;
}
public bool Equals(MyImage secondImage)
{
if (secondImage == null)
{
return false;
}
return string.Compare(Path, secondImage.Path) == 0;
}
}
What this does is compare the paths used by two MyImages and if they are the same returns true;
Use a byte array (byte[]) and then work with it using streams (memory, file...).
You can create a constructor for Form2 that accepts a string parameter and pass that value when showing the second form. Be sure to set the textbox text on second form on Init().
if you need to disable your internet connection the simplest way is to change your gateway
to some ip that doesn't provide the functionality.
ex:
your curent settings:
your modified setting:
this method still lets u use the local network. if you want to disable that too just change your ip address too.
sample code to change ip address
public void setIP(string ip_address, string subnet_mask)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
ManagementBaseObject setIP;
ManagementBaseObject newIP =
objMO.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ip_address };
newIP["SubnetMask"] = new string[] { subnet_mask };
setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
}
catch (Exception)
{
throw;
}
}
}
}
sample code to change gateway
public void setGateway(string gateway)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
ManagementBaseObject setGateway;
ManagementBaseObject newGateway =
objMO.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = new string[] { gateway };
newGateway["GatewayCostMetric"] = new int[] { 1 };
setGateway = objMO.InvokeMethod("SetGateways", newGateway, null);
}
catch (Exception)
{
throw;
}
}
}
}
one way (not the best at all) is to create a table, insert the select results and pass that table as datasource to the grid since you are using datasources
and before app exit or whatever, just drop the table
hello there, i have a problem, it might be trivial but i cannot for the life of me understandw what is causing it.
so i have a numeric updown and based on the value, it loops and creates as many txtboxes as the value in hte numupdown. and then i have a loop that reads the text in these but its not working as intended.
for (int i = 1; i <= numericUpDown1.Value;i++)
{
string textname = "txtName"+i;
TextBox txtTemp = new TextBox();
txtTemp.Location = new Point(x,y);
txtTemp.Name = textname;
txtTemp.Width = 100;
grpCol.Controls.Add(txtTemp);
}
this is the txtbox creating code.
and this is the read loop:
for(int i =1; i <= numericUpDown1.Value; )
{
if (childControl.Name.EndsWith(i.ToString()))
{
if(childControl.GetType().ToString().Equals("System.Windows.Forms.TextBox"))
{
txt2.Text += ((TextBox)childControl).Text;
}
if (childControl.HasChildren)
{
IterateThroughChildren(childControl);
}
}
i++;
}
and so when i run the code and creat 4 txtboxes like this
txta1 txtb1
|_______| |________|
txta2 txtb2
|_______| |________|
with the values being:
1 2
3 4
i get the output 3124, when i want 1234.
sorry if im being incoherent, but couldnt explain it better.if you read this far... thank you:P
one question: why do you increment i inside the for loop?
this is not the best practice but it gets the job done if you need it fast.
ArrayList ar1 = new ArrayList();
ArrayList ar2 = new ArrayList();
ArrayList ar3 = new ArrayList();
ArrayList ar4 = new ArrayList();
ArrayList ar5 = new ArrayList();
ArrayList ar6 = new ArrayList();
string filePath = "file.txt";
StreamReader sr = new StreamReader(filePath);
while (sr.ReadLine() != null)
{
string[] line = sr.ReadLine().Split("\t".ToCharArray());
for(int i = 0; i < line.Length; i++)
{
ar1.Add(line[i]);
ar2.Add(line[i + 1]);
ar3.Add(line[i + 2]);
ar4.Add(line[i + 3]);
ar5.Add(line[i + 4]);
break;
}
}
sr.Close();
This assumes you know the number of columns in the text file. If you later need to use the values from the array as integers just use Convert.ToInt32(ar1[0])
and so on.
Hello all, i want to make icon (shortcut to run my program) on System Tray (usually on Right bottom) with code, i'm using visual studio 2008 c#, on classlibrary project, thanks before :D
add a NotifyIcon control to the form. Then you can code the Form_Resize event to hide the form and the doubleclick event of the notifyicon to show the form.
Thanks pitic
But it is even Enabled in IE also. It seems it doesn't depend upon the browser.
Any other suggestions
as far as i saw what you're trying to do can't be done with httpwebrequest because the request only downloads html code from the website you provide. try to see if inside the html code you receive there is a script tag with path and try to get the code from the script file not the html.
or open the site in firefox with the firebug addon to catch the script path and send the path to the webrequest
(don't know if this works though)
Hi, I am using HTTPRequest and HTTPResponse to get the page source of a site. and i'm getting a warning message "Enable JavaScript to view your site".
Is there any request i should add to enable JavaScript
since visual studio uses ie engine try enabling it in internet explorer and see if it works
IE settings Tools>> Internet option>> Secourity>> Custom level>> there click on radio button to enable active scripting
[B]Book mybook = new Book();[/B]
public void checkRatio()
{
const double RATE = .10;
// The price-to-pages ratio is determined by checking the following: price > RATE * pages where RATE = 0.10
if (Price > (RATE * NumPages))
{
[B]mybook[/B].validRatio = true;
}
// Throw a BookException if a book object is created that has a price that is more than 10 cents per page
else
{
[B]mybook[/B].validRatio = false;
BookException be = new BookException();
/* Create an error message that is passed to the Exception class constructor for the Message property
* when a book does not meet the price-to-pages ratio. */
Console.WriteLine("Ratio for {0} is invalid. Price is {1} for {2} pages.", Title, Price, NumPages);
throw(be);
}
}
} //End Book Class
you are creating an instance of class book inside the class declaration. remove the line in bold, and inside the checkRatio() method just use the cariable names (without the preceding object)... c# is clever enough to use the object you created in main.