|
OK, so at least they've told you what library to use. I'm confused then why you asked us without saying you wanted to use that library. It bothers me that there are people who are told by their supervisors to do tasks they obviously have no idea how to do, and then sent to try to get someone on the web to do the task for them. If you're new to C#, then you should certainly learn that before you start writing code that people will use, or trying to work with third party libraries. In any case, the documentation for iTextSharp is your best bet, followed by any forums they have. Only because, everyone there USES iTextSharp, and will therefore be able to help you. Some people here may use it, but you sure want to put iTextSharp in your header, and generally explain that's what you're using.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Im sorry if i did not mention about it. I just wanna see is there any suggestion to it and not trying to get someone to do the task.
But still, thanks for your advice.
|
|
|
|
|
Well, there's plenty of PDF libraries, but most of them cost money. I don't think there's too many free ones, and I'd never use a free library for a commercial task. If you didn't write the code, and someone paid for it, they should expect better support than you can give by trying to debug a library you did not write.
Christian Graus
Driven to the arms of OSX by Vista.
"! i don't exactly like or do programming and it only gives me a headache." - spotted in VB forums.
I can do things with my brain that I can't even google. I can flex the front part of my brain instantly anytime I want. It can be exhausting and it even causes me vision problems for some reason. - CaptainSeeSharp
|
|
|
|
|
Here is my first attempt at writing a multi-threaded class.
The purpose of this class is to allow you to connect to a remote registry without things going bad if the remote machine doesn't respond. It also holds open remote connections for a predefined period before destroying the connection to allow for reuse.
I'd really like some feedback on the threading. Is it thread safe? Have I made any errors that might make it blow up in certain circumstances?
Many thanks!
using Microsoft.Win32;
using System;
using System.Threading;
using System.Collections.Generic;
namespace JacksTools
{
public class RemoteRegistryHandler
{
private class RegistryStub
{
private RegistryKey _reference;
private int _timeout;
private DateTime _lastAccessed;
public RegistryKey Reference
{
get {
_lastAccessed = System.DateTime.Now;
return _reference;
}
}
public Boolean TimedOut
{
get
{
return (_lastAccessed.AddMilliseconds(_timeout) < System.DateTime.Now);
}
}
public RegistryStub(int timeout, RegistryKey reference)
{
_lastAccessed = System.DateTime.Now;
_timeout = timeout;
_reference = reference;
}
}
Dictionary<String, RegistryStub> _store = new Dictionary<string,RegistryStub>();
private int _timeout;
private Thread _cleaner;
private Boolean _closingDown = false;
private class AttemptGet
{
private String _machineName;
private RegistryHive _hive;
public RegistryKey ResultKey;
public Exception Error;
public void Attempt()
{
try
{
ResultKey = RegistryKey.OpenRemoteBaseKey(_hive, _machineName);
}
catch (Exception e) {
Error = e;
Console.WriteLine("The following exception occured when attempting to connect a remote registry (" + _hive.ToString() + " on " + _machineName + ")\n" +
e.ToString());
}
}
public AttemptGet(String machineName, RegistryHive hive)
{
_machineName = machineName; _hive = hive;
}
}
public RegistryKey GetRemoteKey(String machineName, RegistryHive hive, int timeout)
{
lock (_store)
{
String key = machineName + hive.ToString();
if (_store.ContainsKey(key))
{
if (!_store[key].TimedOut)
{
return _store[key].Reference;
}
else
{
_store.Remove(key);
Console.WriteLine("RemoteRegistryHandler cleaned " + machineName + hive.ToString());
}
}
AttemptGet ag = new AttemptGet(machineName, hive);
Thread attemptThread = new Thread(new ThreadStart(ag.Attempt));
attemptThread.Name = "Attempting connection to registry " + hive.ToString() + " on " + machineName;
attemptThread.IsBackground = true;
attemptThread.Start();
attemptThread.Join(timeout);
RegistryKey reference = ag.ResultKey;
if (attemptThread.IsAlive) throw new TimeoutException();
if (reference == null) throw ag.Error;
RegistryStub newStub = new RegistryStub(_timeout, reference);
_store.Add(key, newStub);
Console.WriteLine("RemoteRegistryHandler added " + key);
if (_store.Count == 1)
{
_cleaner = new Thread(new ThreadStart(cleaner));
_cleaner.Start();
}
return newStub.Reference;
}
}
public void cleaner()
{
while (!_closingDown)
{
try
{
Thread.Sleep(_timeout);
lock (_store)
{
List<String> keys2Del = new List<String>();
foreach (KeyValuePair<String, RegistryStub> pair in _store)
{
if (pair.Value.TimedOut)
{
keys2Del.Add(pair.Key);
}
}
foreach (String key in keys2Del)
{
_store.Remove(key);
Console.WriteLine("RemoteRegistryHandler cleaned " + key);
}
}
if (_store.Count == 0) break;
}
catch (ThreadInterruptedException e)
{
if (_closingDown)
{
foreach (KeyValuePair<String, RegistryStub> pair in _store)
{
Console.WriteLine("RemoteRegistryHandler cleaned " + pair.Key);
}
_store.Clear();
}
}
}
}
public RemoteRegistryHandler(int timeout)
{
_timeout = timeout;
}
public void Close()
{
_closingDown = true;
if (_cleaner != null)
{
try
{
_cleaner.Interrupt();
_cleaner.Join();
}
catch (NullReferenceException e) { }
}
}
}
}
|
|
|
|
|
Just realised that the Cleaner method should be private
|
|
|
|
|
if (_store.Count == 0) break; in cleaner is outside the lock, may be a problem.
|
|
|
|
|
Thank you for the link. I'm now stock here please I need help on this.
SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " +
"Values ('string', 1)", myConnection);
The values is given me problem. How do I get the value from textbox before it can be inserted into batabase. I have design my form and i need to get information from user through the textbox whenever they click the save button on the form.
Thanks for your help.
Ademola
|
|
|
|
|
Adekolurejo wrote: How do I get the value from textbox before it can be inserted into batabase.
To get the value in the TextBox you use something like this (assuming your TextBox is called myTextBox )
myStringValue = myTextBox.Text;
Your SQL needs a little bit of work too. Try this:
SqlCommand myCommand= new SqlCommand("INSERT INTO table (Column1, Column2) " +
"Values (@firstParam, @secondParam)", myConnection);
myCommand.Parameters.AddWithValue("@firstParam", myStringValue);
myCommadn.Parameters.AddWithValue("@secondParam", myIntValue);
myCommand.ExecuteNonQuery();
The use of parameters means that you are not dangerously concatenating strings which can lead to SQL Injection Attacks.
Man who stand on hill with mouth open wait long time for roast duck to drop in
|
|
|
|
|
Thanks Colins.
this line of code "Values (@firstParam, @secondParam)",
@firstParam will this be the name of the first textbox and @secondParam the name of the second textbox etc.
Also myCommand.Parameters.AddWithValue("@firstParam", myStringValue); myStringValue will be if am getting string from the textbox.
myCommadn.Parameters.AddWithValue("@secondParam", myIntValue); myIntValue will be if am getting intgervalue from the textbox.
Am just learning c# on my own pleas help.
Thanks
Ademola.
|
|
|
|
|
Adekolurejo wrote: this line of code "Values (@firstParam, @secondParam)",
@firstParam will this be the name of the first textbox and @secondParam the name of the second textbox etc.
In the SQL String @firstParam and @secondParam match up with whatever parameters you add to the Parameters collection. This can be anything you define. It doesn't have to be values from user controls. (See the lines with Parameters.AddWithValue)
Adekolurejo wrote: Also myCommand.Parameters.AddWithValue("@firstParam", myStringValue); myStringValue will be if am getting string from the textbox.
myStringValue is just a string value, it doesn't have to come from a control. The database command doesn't know anything about controls (like, for example, a text box). It just knows about data.
You can create a string and populate it with the value from a textbox if that's what you need it to be.
Okay - Let's step back and show the whole thing from text box to database. NOTE: that this example shows everything in one place. This is generally considered poor practice, but as you are only just starting I'll not burden you with the principles of layered archtecture and the single responsiblity principle and so on. (Just be aware they exist and one day you'll have to learn about them)
So, let's say you have a form with two text boxes, one for a name, and one for an age. Lets call them NameTB and AgeTB .
The user can enter information in these text boxes and press a button that addes them to the database.
First, we need to get the data from the text boxes into a form we can use.
string name = NameTB.Text;
int age = Convert.ToInt32(AgeTB.Text);
Since text boxes only deal with strings we have to convert the string into a number (an Int32 - a 32bit integer)
Now, we need to set up the database connection and command in order to insert this. I'll assume you already have a connections string to your database, I've called it myConnectionString for this example.
SqlConnection myConnection = new SqlConnection(myConnectionString);
SqlCommand myCommand = new SqlCommand("INSERT Person(NameField, AgeField) "+
VALUES (@nameParam, @ageParam)", myConnection);
I've now set up the SQL Command with an insert statement. I've assumed there is a table called Person and it has two columns called NameField and AgeField . I'm also going to insert the values via parameters, which I've indicated with @nameParam and @ageParam . SQL Server requires that all parameter names start with an @ symbol. Other databases may vary.
myCommand.Parameters.AddWithValue("@nameParam", name);
myCommand.Parameters.AddWithValue("@ageParam", age);
I've now added the parameters into the SQL command. I've given it each parameter with the value we got earlier. Finally:
myConnection.Open();
myComment.ExecuteNonQuery();
myConnection.Close();
This opens the connection, runs the INSERT statement and closes the connection again. I'm using ExecuteNonQuery because we don't expect any results back from SQL Server. If we were expecting data back (e.g. because we were using a SELECT statement) we could use ExecuteReader (for many rows/columns) or ExecuteScalar (for a single value).
I hope this helps.
Man who stand on hill with mouth open wait long time for roast duck to drop in
|
|
|
|
|
Thank you colins. I think I have to read and understand this before any futher questions.
Ademola
|
|
|
|
|
it seems to me there is no difference between generics Dictionary<> and
SortedList<>.
but since they are defined, there should be some difference.
then what is the difference then?
thanks.
|
|
|
|
|
|
It wouldn't hurt to read some of the MSDN documentation; they state:
The Dictionary< (Of < (TKey, TValue> )> ) generic class provides a mapping from a set of keys to a set of values. Each addition to the dictionary consists of a value and its associated key. Retrieving a value by using its key is very fast, close to O(1), because the Dictionary< (Of < (TKey, TValue> )> ) class is implemented as a hash table.
The SortedList< (Of < (TKey, TValue> )> ) generic class is a binary search tree with O(log n) retrieval, where n is the number of elements in the dictionary. In this, it is similar to the SortedDictionary< (Of < (TKey, TValue> )> ) generic class. The two classes have similar object models, and both have O(log n) retrieval. Where the two classes differ is in memory use and speed of insertion and removal: ...
hence performance is the key; you choose based on (1) the need for having things sorted, and (2) the balance between insertions and lookups.
Luc Pattyn [Forum Guidelines] [My Articles]
DISCLAIMER: this message may have been modified by others; it may no longer reflect what I intended, and may contain bad advice; use at your own risk and with extreme care.
|
|
|
|
|
Luc Pattyn wrote: It wouldn't hurt to read some of the MSDN documentation
Yes
Luc Pattyn wrote: you choose based on
Reading the documentation or reading what someone in a forum writes. Of course if you choose to read the documentation you don't get to tweet.
|
|
|
|
|
Hi All. In my rich text editor everything's finished except for one thing. I want the user to be able to save the file as EITHER RTF or TXT. But when I use this:
Rtb.SaveFile(Sf.FileName, RichTextBoxStreamType.PlainText);
... It never works. The next time they open the TXT file it opens but with all the RTF formatting codes in it which makes the file ugly and unreadable and unacceptable. Does anybody know how I can fix this, or know of any other ways to do this.
I have tried doing this also, but doesn't always work:
string text = Rtb.Text;
File.WriteAllText(Sf.FileName, text);
I have been to MSDN and Google and there's a whole lot of junk on Google with no solutions. (Btw, I'm beginning to favor Microsoft's new search: Bing over Google nowadays). I'm completely baffled as to why the PlainText option does not work.
Any thoughts? Anyone?
regards,
jase
|
|
|
|
|
Never mind, I'm going to begin looking into making my own input control. Wish me luck!
|
|
|
|
|
Hi,
I have a WinForm that hosts a WebControl. Is there any way to disable all java scripts in that webControl?
thanx
|
|
|
|
|
I tried injecting a script that would prevent the control to popup other windows, but it did not work
In the webBrowser1_Navigated handler:
HtmlElement head = webBrowser1.Document.GetElementsByTagName("head")[0];
HtmlElement scriptEl = webBrowser1.Document.CreateElement("script");
IHTMLScriptElement element = (IHTMLScriptElement)scriptEl.DomElement;
string alertBlocker = "window.alert = function () { }";
element.text = alertBlocker;
head.AppendChild(scriptEl);
Also, when I right-click 'view page source' I don't see the script I injected.
|
|
|
|
|
Can anybody help out with this?
|
|
|
|
|
I'm trying to add spacing to the beginning of some text so that I can put small symbols before specific lines of text. The size of the symbols is always the same, 10x10 pixels, however the number of symbols can range from none to half a dozen.
The problem I'm running in to is figuring out the number of spaces to indent the text. Normally I'd simply move the bounding box over by the size of the bullets, but in this case, the next line of text needs to wrap under the graphics I'm inserting, so moving the bounding box cuts down on the space available for text.
Right now I'm just adding in 3 * the number of spaces as there are symbols in hopes of getting close, but it's a bit off. I've tried using MeasureString to get the size of a space, then adding enough spaces to move the text, but it comes out too little with only 1 symbol and not nearly enough by the time there are 4 or more symbols.
Any suggestions?
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|
|
Why not always start the text with a symbol? Use a blank one instead of your spaces and off you go. I don't think spaces are a good way to go; the width of a space can vary depending on the font - start a reply to this and compare the width of a space in the "Subject" and "Text" areas. The font size is the same, but the space width is different.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
That's why I was using measurestring to check the size of the space in whatever font was being used at that moment. As for always using a symbol, that would be simple, except the number of symbols changes.
What would be nice is if there were a way to indent text within a bounding box, by a set number of pixels. Just like in a text editor where if you go to the beginning of a paragraph and insert tab, everything indents and the word wrap updates to fit each following line.
The only other thing I've thought of so far would be doing the word wrap manually by measuring each line and breaking up the string until it fit. I've just hoped to avoid that since it's going to involve rounding between lines, making the line spacing squirrely.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|
|
But if your symbols are always the same size (as you said) just use the appropriate number of blank symbols. You cannot guarentee to get the same size with spaces - there may not be a neat multiple to ten, twenty and so on pixels.
Avoid manual word wrap - I had to do it yonks ago (pre windows) and it is a PITA.
No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced.
This message is made of fully recyclable Zeros and Ones
|
|
|
|
|
Each Symbol is the same size, however if there's only 1 symbol and I leave room for 6 of them, then I've lost half of that line of text. The print out is about 2.25" x 2.25" a little smaller than half a playing card. So each bit of space is a premium.
The true man wants two things: danger and play. For that reason he wants woman, as the most dangerous plaything.
|
|
|
|
|