|
Hi,
I'm new to c# and was wondering if there is a quicker way of doing to following code without using the try and catch method as it is slow.
int result;
try
{
int str1_num = int.Parse(str1.Replace(",", ""));
int str2_num = int.Parse(str2.Replace(",", ""));
if (lvi1.ListView.Sorting == SortOrder.Descending)
{
if (str1_num < str2_num)
result = 1;
else
result = 0;
}
else
{
if (str1_num > str2_num)
result = 1;
else
result = 0;
}
}
catch
{
if (lvi1.ListView.Sorting == SortOrder.Descending)
result = String.Compare(str1, str2);
else
result = String.Compare(str2, str1);
}
Cheers
Fly
hmmm pie
|
|
|
|
|
Hi,
have a look at
int.TryParse().
This will avoid using try-catch.
Regards
Sebastian
|
|
|
|
|
As mentioned in the other post, use the TryParse method.
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon
"Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham
|
|
|
|
|
Hello,
I am making a MSI setup with VS 2005 and have a custom action DLL added. I've been searching for a solution for my problem for a week now... My problem is exiting the custom action when the user clicks cancel on my dialog. The issue is that if I throw a InstallException, I get a error message box that refers to unhandeld exception. How can you exit a the installation from MSI without having this error message box? By exit I mean, rollback...
|
|
|
|
|
In Rollback part just add the dll...
I think it should work...jus try it
|
|
|
|
|
I added to rollback my dll too, the problem is: if I call Rollback directly nothing happens, if I say 'throw new InstallException("Canceld by user");' it rolls back nicely but before that, a general error MessageBox is shown...
|
|
|
|
|
Hello my friends!
I wants to create a chat application as client-server (one server and multiple clients). Please help me that which way is best to make it. With Sockets or Remoting objects (HttpChannel or TcpChannel)?
As you know if increases clients, server traffic is a serious problem.
Please describe your advices and suggestions...
Thanks.
|
|
|
|
|
You have two options for this:
1) Do some [deleted] research (qv google)
2) Post your requirements on rent-a-coder
Panic, Chaos, Destruction.
My work here is done.
|
|
|
|
|
Hi
What i want to do ?
I am displaying a picture in picture box
When user drags the picture then it has to appear in the another picture box
What i Did ..
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
isdragged = true;
}
private void pictureBox2_MouseEnter(object sender, EventArgs e)
{
if (isdragged == true)
{
pictureBox2.ImageLocation = picpath;
isdragged = false;
}
}
isdragged is a boolean variable ....
picpath is a string i have stored the image location
After clicking on the picturebox1 and dragging it to the picture box 2 .. private void pictureBox2_MouseEnter event is not raising
I mean to say no effect when mouse enters into the picturebox2
I know i have done a mistake somewhere
Please tell me what i must do to solve this
(* Dont tell me to read a basic book .. i have read it twice ...*)
Joking
Thanks and Regards
If You win You need not Explain............
But If You Loose You Should not be there to Explain......
|
|
|
|
|
If you click and drag, you don't get any MouseUp event in the first picture box...
Krazy Programmer wrote: (* Dont tell me to read a basic book .. i have read it twice ...*)
Third time is the charm.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Guffa wrote: Third time is the charm.
YES , I know someone will tell this .. but not the correct answer
Anyway Thanks
I will be reading it again
If You win You need not Explain............
But If You Loose You Should not be there to Explain......
|
|
|
|
|
Krazy Programmer wrote: but not the correct answer
I think that it is. If you don't understand that you don't get an MouseUp event on a control where you don't release the mouse button, you are clearly missing some of the most fundamental ideas of the graphical interface and the event handling. The logical solution would be to go back and fill in the gaps.
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
why drag?
why dont you browse a picture and load it in a picturebox?
OpenFileDialog open_file_dialog = new OpenFileDialog();
DialogResult dialog_result = open_file_dialog.ShowDialog();
if (dialog_result == DialogResult.OK)
{
mypicturebox.ImageLocation = open_file_dialog.FileName;
}
After that, dont know if you want a path to the picture or load/save them from/to a database.
nelsonpaixao@yahoo.com.br
trying to help & get help
|
|
|
|
|
nelsonpaixao wrote: why drag?
Requirement is drag and drop
If You win You need not Explain............
But If You Loose You Should not be there to Explain......
|
|
|
|
|
Hi all,
A while ago I posted a question on this topic, and received a great response. I made use of the article posted by Sacha Barber called Beginners Guide To Threading In .NET Part 5 of n. In this article he uses the SynchonizationContext to marshall the background work to the UI thread.
But the thing is that when I send response back to a listview on the GUI thread x amount of time in quick succession the whole application just freezes / hangs.
Is this a bad idea doing it like this or am I just doing something wrong (maybe a different approach)
private SynchronizationContext context;
.....
.....
context = SynchronizationContext.Current;
if (context == null)
{
context = new SynchronizationContext();
}
.....
.....
try
{
for (int i = 0; i < (int)e.Argument; i++)
{
context.Send(new SendOrPostCallback(delegate(object state)
{
lViewTransactionHistory.Items.Add(lViewTransactionHistory.Items.Count.ToString(), DateTime.Now.ToLongTimeString(), 0);
}), null);
}
}
catch (InvalidOperationException oex)
{
MessageBox.Show(oex.Message);
}
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hi,
Is that 1) freeze/hang and never recover or 2) freeze/hang and then come back to life after a period of time. If it's the latter then you are sending too many messages to the UI thread and overwhelming the message queue. You may be able to work around this by sending the UI updates in batches rather than individually.
Alan.
|
|
|
|
|
Alan N wrote: Is that 1) freeze/hang and never recover or 2) freeze/hang and then come back to life after a period of time.
Good question, I haven't waited long enough to check if the UI comes back to life, but I understand your point. Thank you
Regards,
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
Hi,
Answer: 1) freeze/hang and never recover
How should this be treated?
Many thanks again
Kind regards,
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
I am not sure about this case but generally it's better to use AddRange method for adding many items to the listview. Also, use BeginUpdate() EndUpdate() methods so that your listview isn't repainted any time an item is added.
|
|
|
|
|
Giorgi Dalakishvili wrote: Also, use BeginUpdate() EndUpdate() methods so that your listview isn't repainted any time an item is added.
Thanks for the response. I noticed that when I change the listview control to a listbox the problem does not occur anymore, so I will try the above mentioned response.
Many thanks
Kind regards,
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
|
Which overload of AddRange method are you using? Try using the one that takes ListViewItem[] as a parameter.
|
|
|
|
|
To be exact:
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(delegate
{
lViewTransactionHistory.BeginUpdate();
ListViewItem item1 = new ListViewItem(DateTime.Now.ToLongTimeString(),0);
item1.SubItems.Add(i.ToString());
item1.SubItems.Add(TcpServer.connectData[i].Request);
item1.SubItems.Add("OK");
item1.SubItems.Add("CMD_PING");
lViewTransactionHistory.Items.AddRange(new ListViewItem[] { item1 });
lViewTransactionHistory.EndUpdate();
}));
}
else
{
...
}
So yes I am using the correct method overload.
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|
|
You are doing practically the same thing you were doing in your original code. Instead of adding items one by one, store them in an array and add them in batch. If you are using backgroundworker you can use Result property to store listviewitem[] and access it when the worked is completed. In this case you can just add the items without the need of Invoke.
|
|
|
|
|
Ok, I understand what your saying, the only thing is that I wanted to display each of the events of the TCP server as it was received, real time. But if I put it into a batch / array and display it every x amount of time, this won't be the case. So this is what I wanted to achieve. But thank you very much for the info and help.
regards,
The only programmers that are better those C# programmers are those who code in 1's and 0's |
Programm3r
My Blog: ^_^
|
|
|
|