|
|
In C#, I'm trying to read a text file into a combo box and use it as the items. When I try to do it, I get the newline characters and it's all smashed into one line. Any ideas or example on how to do this would be appreciated.
|
|
|
|
|
How are you doing it ? File.ReadAllLines will give you a string array, which is a viable data source for a combobox.
Christian Graus - Microsoft MVP - C++
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I was goofing with StreamReader, this worked fine!
Thanks!
|
|
|
|
|
Hi
I have to Marshal a class which is holding a collection , How to achieve this??Is there any way to Marshal a collection in a class,I know that string, bool having MarshalAs methods, but collection dont have ??
prompt reply is appreciated as it is quite urgent..
Thanks
|
|
|
|
|
Hi
I want to populate a propertygrid passing treeview node argument in WPF.Please advise..I am new to WPF.
Thanks
|
|
|
|
|
Hi all,
Currently am working in a project, where in my application there is a window form, holding several pages(using WPF).now i've to implement session timeout functionality in my application using window hook for keyboard and mouse. i've a implemented a timer(System.Threading)TimerCallback, which periodically examine, whether, any key board message or mouse message occure ONLY ON MY APPLICATION. this means, minimizing my application form, i can play computer game, but after certain period my application can throw timeout session.
now the problem is, delegate(by which i'm calling window hook function whenever key or mouse pressed) is firing event even if my function is minimized. (i know this will fire), but the only requirement is how i manipulate these messages (key and mouse messages)only haapen to my application window. i don't know how to do this.. please help
please go through these urls, as i'm trying to develop window hook, by help of these.....
here he has used WH_KEYBOARD_LL Hook which is global
1. "http://blogs.msdn.com/toub/archive/2006/05/03/589423.aspx"
2. "http://blogs.msdn.com/toub/archive/2006/05/03/589468.aspx"
Please please help......
-- modified at 18:14 Saturday 8th September, 2007
kamalesh
|
|
|
|
|
I've read a couple of "solutions" to the problem of ensuring that only one instance of a form opens within an MDI app - notably this one http://www.codeproject.com/csharp/single_instance.asp[^]
To me, that and other similar solutions, look overly complicated. Is there anything particularly wrong with using the following?
<br />
uxLoginForm login = null;<br />
<br />
private void toolStripButton1_Click(object sender, EventArgs e)<br />
{<br />
if (login == null || login.IsDisposed )<br />
login = new uxLoginForm();<br />
login.Show();<br />
}<br />
Me: Can you see the "up" arrow?
User:Errr...ummm....no.
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
|
|
|
|
|
martin_hughes wrote: Is there anything particularly wrong with using the following?
It'll work if that's what you mean, perhaps where you could improve is upon the extensibility. I haven't thought this through very much but perhaps a slightly more extensible version would be:
Dictionary<Type, Form> m_SingleInstanceForms = new Dictionary<Type, Form>();
protected Dictionary<Type, Form> SingleInstanceForms
{
get { return this.m_SingleInstanceForms; }
}
protected virtual void ActivateChildForm<T>() where T : Form, new()
{
if (!this.SingleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
this.SingleInstanceForms.Add(newForm);
newForm.MdiParent = this;
}
Form formToActivate = this.SingleInstanceForms[typeof(T)];
formToActivate.Show();
formToActivate.Activate();
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
this.ActivateChildForm<uxLoginForm>();
} Now you have one method which will activate (and if required, create) any class derived from System.Windows.Forms.Form.
Last modified: 1min after originally posted -- Edited bug in code
|
|
|
|
|
Problem with this is that once the login form has been opened and closed once, it gets disposed, so neither the Show() or Activate() methods will work.
Easily fixed by hacking your example and adding an additional else if:
if (!this.SingleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
this.SingleInstanceForms.Add(typeof(T),newForm);
newForm.MdiParent = this;
}
else if (this.SingleInstanceForms[typeof(T)].IsDisposed)
{
T newForm = new T();
this.SingleInstanceForms[typeof(T)] = newForm;
newForm.MdiParent = this;
}
But doing so looks a bit errrmm inellegant - although I'm sure that it could be refactored out to be made nicer (but it's late and I've had a couple of beers). Or microsoft could implement an UnDispose() method
Anyway, cheers - it was interesting having a look at generics, which I haven't really dipped into yet.
A quick question:
protected virtual void ActivateChildForm<T>() where T : Form, new()
I guess that the where T : Form limits what can be passed in as <T> , but what's the new() about?
Me: Can you see the "up" arrow?
User:Errr...ummm....no.
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
|
|
|
|
|
martin_hughes wrote: I guess that the where T : Form limits what can be passed in as
Correct
martin_hughes wrote: but what's the new() about?
It constrains the types passed in, they must have a default constructor (public ClassName() { } ). Alternatively if you didn't what this you could use:
Activator.CreateInstance<T>(); instead of new T() . If you remove the constraint you'll see the compiler throw an exception because the class may not necessarily implement a default constructor.
martin_hughes wrote: But doing so looks a bit errrmm inellegant
One answer is to hook into the child form's closing event and remove the entry from there and dispose of it (frees up resources too).
if (!this.SingleInstanceForms.ContainsKey(typeof(T)))
{
T newForm = new T();
newForm.MdiParent = this;
newForm.FormClosing += new FormClosingEventHandler(delegate (object sender, FormClosingEventArgs e)
{
Form senderForm = (sender as Form);
senderForm.Dispose();
this.SingleInstanceForms.Remove(senderForm.GetType());
}
));
this.SingleInstanceForms.Add(typeof(T), newForm);
}
... Now you don't have to check if there's a disposed form there because they are automatically removed from the dictionary (and properly disposed of).
|
|
|
|
|
Very, very cool . You should write an article on this, as you've managed to show me all manner of things about generics and delegates. Cheers!
Me: Can you see the "up" arrow?
User:Errr...ummm....no.
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
|
|
|
|
|
martin_hughes wrote: You should write an article on this
On what exactly? The single instance thing or something on generics. What would you like to see in it?
|
|
|
|
|
Ed.Poore wrote: What would you like to see in it?
Perhaps even a wider scope. Given the solution you suggested, I've spent some time today reading about and trying out some "Generic" techniques. It is my intention in the coming week to revisit an old (C# 1.1) project with a view to converting it to C# 2.0 and seeing where Generics could be used instead of the non-generic methods of the older language.
Your example has not only given me an insight into Generics, but by solving a problem I was facing (no matter that it was a simple, yet seemingly common, problem) has enhanced my understanding of the language. In this respect I'm pretty sure I'm like a lot of people - reading about the solution to an abstact problem, or a problem one has no investment in does not necessarily lead to understanding. However, solve a problem that one does have an investment in and understading is almost certain to follow (except for those lazy so and so's who have no interest in learning) - and given that this single form mdi question seems to come up from time to time, I'm sure a lot of people could benefit.
I should also point out that you used a number of what I would term advanced language features in your example; for instance:
-Generic collections
-Generic methods
-Inline delegates
-Hooking into events
These may well be everyday semantics for the professional programmer, but to beginners / hobbyists (like me) they're something new - and definietely something to investigate further.
What would also be an interesting discussion in an article is why given my original question you thought of Generics as a possible solution - what in your experience lead you to that conclusion.
Me: Can you see the "up" arrow?
User:Errr...ummm....no.
Me: Can you see an arrow that points upwards?
User: Oh yes, I see it now!
-Excerpt from a support call taken by me, 08/31/2007
|
|
|
|
|
martin_hughes wrote: what in your experience lead you to that conclusion.
I haven't a bloody clue
|
|
|
|
|
|
hi
i need an algorithm like 'Word Wrap' in notepad that place my words in some lines that fit in my border(like richtextBox), i wrote this code, but it's not work correctly :
private void ShowText()<br />
{ <br />
string mainText = System.IO.File.ReadAllText("test.txt");<br />
Graphics g = this.richTextBox1.CreateGraphics();<br />
int widthBorder = this.richTextBox1.Width;<br />
StringBuilder sb = new StringBuilder();<br />
SizeF sizeText = new SizeF();<br />
SizeF sizeCurrentLine = new SizeF();<br />
ArrayList allLines = new ArrayList();<br />
string firstWordNextLine = string.Empty;<br />
<br />
string[] textCol = mainText.Split(new char[] { ' ' });<br />
foreach (string text in textCol)<br />
{ <br />
if (firstWordNextLine != string.Empty)<br />
{<br />
sb.Append(firstWordNextLine);<br />
firstWordNextLine = string.Empty;<br />
}<br />
int widthText = (int)g.MeasureString(text, this.richTextBox1.Font).Width;<br />
int widthCurrentLine = (int)g.MeasureString(sb.ToString(), this.richTextBox1.Font).Width;<br />
int totalWidthLine = widthText + widthCurrentLine;<br />
if (widthCurrentLine <= widthBorder && totalWidthLine <= widthBorder)<br />
{<br />
if (sb.ToString() == "")<br />
sb.Append(text);<br />
else<br />
sb.Append(" " + text);<br />
}<br />
else<br />
{<br />
sw.WriteLine(sb.Length.ToString()); <br />
allLines.Add(sb.ToString());<br />
firstWordNextLine = text;<br />
sb = new StringBuilder();<br />
}<br />
}<br />
<br />
for (int i = 0; i <= allLines.Count - 1; i++)<br />
{<br />
this.richTextBox1.Text += allLines[i].ToString() + "\n";<br />
} <br />
}
how to solve my problem and where does my mistake in code ?
thanks
|
|
|
|
|
RichTextBox class has WordWrap property. Setting it to true will make it wrap the text. Why don't use it?
|
|
|
|
|
Hi there,
I'm trying to solve a little problem. I created a simple web service in Java that sends a file as a byte array to the client. This array is sended as a MTOM attachment. Now, I'm trying to make a C# client application like this:
using System; <br />
using System.Collections.Generic; <br />
using System.ComponentModel; <br />
using System.Data; <br />
using System.Drawing; <br />
using System.Text; <br />
using System.Windows.Forms; <br />
<br />
namespace ImgClient <br />
{ <br />
public partial class Form1 : Form <br />
{ <br />
public Form1() <br />
{ <br />
InitializeComponent(); <br />
} <br />
<br />
private void button1_Click(object sender, EventArgs e) <br />
{ <br />
ImageService.ImageServiceService service = new ImageService.ImageServiceService(); <br />
byte[] data = service.getJavaImage(); <br />
MessageBox.Show(data.Length.ToString()); <br />
} <br />
} <br />
}
but when I click on the button I'll receive an InvalidOperationException with this message:
Client found response content type of 'multipart/related;type="application/xop+xml";boundary="uuid:44b8d14c-dcbb-4024-996d-078c5985a152";start-info="text/xml", but expected "text/xml"... on line :
byte[] data = service.getJavaImage(); <br />
I made also a Java client application like this and it worked. So web service realy works. I also downloaded WSE 3.0 and set messaging->client mode to "on". But it didn't help.
Has anybody ever solved a problem like this? Or could anybody help with this?
thx in advance.
|
|
|
|
|
|
Well, thx. I read it twice. The only thing I could do else was:
"Sometimes VS misbehaves and does not generate this class. I don't understand why, but the work-around is to Show-all-files in the WinForms project and expand the web service > Reference.map > Reference.cs. Edit this file and change public partial class MTOM : System.Web.Services.Protocols.SoapHttpClientProtocol to public partial class MTOMWse : Microsoft.Web.Services3.WebServicesClientProtocol. Also make sure to update the constructor to match the new class name. Then it should compile fine."
Still no change. I'm still receiving the same exception. Here I'm giving a piece of web service code written in java to show how is the mtom composed.
package imageservice;
import javax.annotation.Resource;
import javax.jws.WebService;
import javax.xml.ws.BindingType;
import javax.xml.ws.WebServiceContext;
import javax.jws.WebMethod;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.io.IOException;
/**
*
* @author Hellburn
*/
@WebService()
@BindingType(value=javax.xml.ws.soap.SOAPBinding.SOAP11HTTP_MTOM_BINDING)
public class ImageService {
@Resource
private WebServiceContext wsContext;
/**
* Web service operation
*/
@WebMethod
public byte[] getJavaImage() {
byte[] data = null;
wsContext.getMessageContext().put(com.sun.xml.ws.developer.JAXWSProperties.MTOM_THRESHOLOD_VALUE,0);
// TODO implement operation
try{
File f = new File("c:/SomeFile.jpg");
FileInputStream fis = new FileInputStream(f);
long length = f.length();
data = new byte[(int)length];
int offset = 0;
int numRead = 0;
while (offset < data.length && (numRead=fis.read(data, offset, data.length-offset)) >= 0) {
offset += numRead;
}
fis.close();
}catch(FileNotFoundException fnfe){
fnfe.printStackTrace();
}catch(IOException ioe){
ioe.printStackTrace();
}
return data;
}
}
Maybe someone will come up with something what else I should do with my C# client application to make it run. I really don't know.
-- modified at 18:00 Sunday 9th September, 2007
|
|
|
|
|
Good afternoon gentlemen,
I was hoping somebody could explain what is wrong with the following implementation of a simple Generic function
<br />
protected void SetValue<<T>>(T value, int length)<br />
{<br />
byte[] data = BitConverter.GetBytes(value);<br />
if (SrbData.Length < (length + data.Length))<br />
throw new InvalidOperationException();<br />
Array.Copy(data, 0, SrbData, length, data.Length);<br />
}<br />
ps, the <<t>> is because I could not work out how to get a single greater than to display and SrbData is a byte array
The Visual Studio error is
Error 1 The best overloaded method match for 'System.BitConverter.GetBytes(bool)' has some invalid arguments C:\Documents and Settings\Paul\My Documents\Visual Studio 2005\Projects\Comet\SrbLibrary\SrbHeader.cs 17 27 SrbLibrary
I do not have much experience with Generics so this is most likely a silly problem, but thanks for the help anyway
Paul
|
|
|
|
|
BitConverter.GetBytes() accepts only several types. It seams that the type that you are substituting T with cannot be expected by BitConverter.GetBytes(). Msdn has more information about this function
|
|
|
|
|
Thanks for the response
However this I understand, but I was hoping to overcome the need to duplicate a function for each type that I need to add to the bit array. Hence the attempt with Generics.
My understanding is that the type T will become what ever T i pass in to it!
Is the problem because the base function will only accept 9 types and generics potentially means that far more types could be passed in??
Ta
Paul
Help, Urgent, Need answers Urgent, Quick Help arggggghhhhhhhhhh
|
|
|
|
|
Yes, T will become the type that you pass but GetBytes can accept only 9 types. So either the type of T should be one of them or you should write a new class like that acts like bitconverter and has a function called GetBytes that accepts many types but that means quite a lot of work
|
|
|
|