|
Thank you. What if I don't know how many buttons or controls exist? How can I create a setting for each control? I'm working on a programme that users can add controls at run time.
|
|
|
|
|
You can have these settings saved for you automatically. Just have a google for 'App.Config' or 'Saving Form Location' and read up about it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi I am trying to handle a file not found exception by creating the specified text file if it does not exists.
void RetrieveData()
{
if(File.Exists("books.txt") == false) // if file doesnt exists
{
File.Create("books.txt"); // create the file
}
//Create a stream reader object to read from "books.txt"
StreamReader DataReader = new StreamReader("books.txt");
}
I did a test run and remove the books.txt file from the current working directory on purpose and when i start debugging all I get is this exception :
The process cannot access the file 'C:\Documents and Settings\Administrator\My Documents\NYP Work\C#\MiniProject 2009\PhotoAlbum_V2\bin\Debug\books.txt' because it is being used by another process.
Any suggested solutions ?
|
|
|
|
|
Your problem lies with the File.Create method. It returns a FileStream instantiation. Until you close the FileStream, you cannot read the file without requesting different access to it. Try something like this
void RetrieveData()
{
Stream bookStream = new FileStream("books.txt", FileMode.OpenOrCreate);
using(StreamReader booksReader = new StreamReader(bookStream))
{
}
}
The trick here is creating a new FileStream with FileMode.OpenOrCreate. If the file exists, it is opened; if it doesn't exist, it is created and then opened. If you want to check whether the file is empty (it's pointless reading from an empty file), check whether bookStream.Length is equal to zero. This will also tell you if the file is newly created because the newly created file will be empty
|
|
|
|
|
thanks it worked well that way.
|
|
|
|
|
How do we build a network ..
|
|
|
|
|
|
Or here http://www.facebook.com/[^]
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
|
|
|
|
|
hi,
how i can move the toolbar in the running time using c#?
|
|
|
|
|
|
Hi
I have written code for client server communication using Tcp and Udp..It works well...I have created a function for the client and called the function from the form1.cs in VS.NET...Now this acts as the client and the server is a separate code..
Now i want both the client and server to communicate...
With a button click i want the message from the client to be sent to the server...So i used a ListBox control in the form...with a button click im able to send message to the server through the socket connection..
Server runs in the console...I am able to send message from the tool i.e client to the console...Once the server the receives the message i require the server to send a message back to the client as an acknowledgement (i.e. the message must be displayed to the listbox control in the form) How do i do this?Please help me out with this.thanks for any help in advance..
|
|
|
|
|
If you got the client to communicate with the server, then the same method is used to talk back from the server to the client
to show your message in the listbox use
ListBox.Items.Add()
Here is ListBox documentation[^]
Yusuf
|
|
|
|
|
if i use listBox1.Items.Add()...
it gives me an error stating
listBox1 does not exist in the current context
|
|
|
|
|
You need to add a ListBox control to your form. listBox1 will then refer to that control, and add whatever you call the Add method with as arguments. Be careful though; it will add the items, but displays the ToString representation of them. If it's your own class, override the method to provide your own implementation of ToString
|
|
|
|
|
Hi,
I have a form1.cs which has a listBox control...
I have defined the server class in another file using(File->
New->File option)but in the same project...So im not able to
use listBox1.Items.Add()...
If i do so it gives me an error
|
|
|
|
|
Use the server class in the usual way. When you get told that you have received a message, add it to the listbox
|
|
|
|
|
 Hi,
This is the server code:
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpServer
{
public static void Main()
{
try
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
EndPoint tmpRemote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
string wel = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(wel);
newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
listBox1.Items.Add(data);
ASCIIEncoding asen = new ASCIIEncoding();
newsock.Send(asen.GetBytes("The string was recieved by the server."));
Console.WriteLine("\nSent Acknowledgement");
listBox1.Items.Add("The string was recieved by the server.");
newsock.Close();
}
catch(Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}}}
This is Form1.cs where i have called the client
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Windows.Forms;
namespace WindowsApplication13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Callback();
}
private void Callback()
{
try
{
byte[] data = new byte[1024];
listBox1.Items.Add("Connecting....");
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket server = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
listBox1.Items.Add("Connected");
data = Encoding.ASCII.GetBytes("welcome");
listBox1.Items.Add("Transmitting...");
server.SendTo(data, data.Length, SocketFlags.None, ipep);
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
EndPoint tmpRemote = (EndPoint)sender;
int recv = server.ReceiveFrom(data, ref tmpRemote);
listBox1.Items.Add( tmpRemote.ToString());
listBox1.Items.Add(Encoding.ASCII.GetString(data, 0, recv));
listBox1.Items.Add("Stopping client");
server.Close();
}
catch (Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}}}}
I have sent a string Welcome to the server and this works well
The following lines are displayed in the list box control
But the other listBox1.Items.Add()does not work..
What changes should i make to display
listBox1.Items.Add("Connecting....");
listBox1.Items.Add("Connected");
listBox1.Items.Add("Transmitting...");
|
|
|
|
|
 Hi,
I have made some changes to the above code and i have made the client and the server to communicate...
Now i need to perform a small check...
i have sent a string hello from the client to server...
the string is received in the server...Once it is received i want to check whether the received string is right...if it is right it must send another message to the client...So i used a" if" loop....When i use this loop im not able to receive the message from the server...
Please tell me the changes
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
public class UdpServer
{
public static void Main()
{
try
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
Socket newsock = new Socket(AddressFamily.InterNetwork,SocketType.Dgram, ProtocolType.Udp);
newsock.Bind(ipep);
Console.WriteLine("Waiting for a client...");
IPEndPoint sender = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9050);
EndPoint tmpRemote = (EndPoint)(sender);
recv = newsock.ReceiveFrom(data, ref tmpRemote);
Console.WriteLine("Message received from {0}:", tmpRemote.ToString());
Console.WriteLine(Encoding.ASCII.GetString(data, 0, recv));
if (Encoding.ASCII.GetString(data)=="hello")
{
data = new byte[1024];
string ss = "Welcome to the Server";
data = Encoding.ASCII.GetBytes(ss);
newsock.SendTo(data, data.Length, SocketFlags.None, tmpRemote);
}
Console.WriteLine("\nSent Acknowledgement");
newsock.Close();
}
catch(Exception e)
{
Console.WriteLine("Error..... " + e.StackTrace);
}
}
}
|
|
|
|
|
Hi Evryone
How can I copy Form (Code + designing) in project and change his Name ?
(I work on WinCE)
thank's for any help
|
|
|
|
|
right click on the Form you want to copy then select "Copy". Now, select the project you want to copy the form to, then right click and select "Paste"
Once the Form is copied, make sure it is selected then right click and select "Rename". This will only rename the file, not the class. you will need to change your class name manually.
Yusuf
|
|
|
|
|
Thank's for the help
But what whit the Code ?
I need the code, I need that all the function of the Form will work too
|
|
|
|
|
what code? Didn't you ask to copy Form+Code?
Yusuf
|
|
|
|
|
One way to do this is to select your form in Solution Explorer, then from the File menu select Save <your form name here> AS... and save it with the new name. Then right click on the project in Solution Explorer and select Add | Existing Item... , navigate to the old form and select it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I've got a program I've written that uses an Access Database for a backend. I've been using it on XP for awhile now with no issues. However recently I've got a new user who has Vista 64 bit. My application crashes before it even finishes loading. Using some debug code in a custom compile I did for him I've narrowed it down to the following section of code:
string sAppPath = (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)).Replace("file:\\", "");
public System.Data.OleDb.OleDbConnection DBConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + (sAppPath + "\\Database.mdb"));
sSQLStatement = ("SELECT Filename FROM Scriptures");
System.Data.OleDb.OleDbCommand SQLCommand = new System.Data.OleDb.OleDbCommand(sSQLStatement, DBConnection);
try
{
if (DBConnection.State.ToString() == "Closed") DBConnection.Open();
System.Data.OleDb.OleDbDataReader DataReader = SQLCommand.ExecuteReader();
while (DataReader.Read())
{
}
DataReader.Close();
DBConnection.Close();
}
The program crashes on this line:
if (DBConnection.State.ToString() == "Closed") DBConnection.Open();
I'm at a loss as to why this would work on XP but not Vista. I don't have a Vista machine readily available for testing at this time.
Any help would be greatly appreciated. Thanks!
|
|
|
|
|
First off, you really need to get yourself a good C# book and do some reading, because you have some serious fundamental flaws in your code. The Database connection State is an Enumerated type (see example usage[^]), you need not cast it to a String and then compare, you should be comparing the Enumerated types. Additionally, you should really make use of Using[^] statements in your code, it will make for cleaner code, and will ensure the objects are probably closed and disposed.
I see one potential problem, in that you're creating an instance of a SQLCommand using a connection object that might be null; and to top it off, you're doing that outside the scope of your Try/Catch block.
See this line: System.Data.OleDb.OleDbCommand SQLCommand = new System.Data.OleDb.OleDbCommand(sSQLStatement, DBConnection);
If you're not sure if the connection has been created and opened, then you should do that first. Once you have a valid connection, THEN create your SQL Command and hook that up to the connection and execute your query (see Example[^] on MSDN)
But seriously, go read about Enumerated types and Using statements, and hopefully this will help you over come your problem.
Last modified: 13mins after originally posted --
|
|
|
|