|
I got the Solution. Actually that is a very generic database connection class which can be instantiated with any type of connection like oledb,sql etc.
i solved that problem by
public DbConnection mConnection =new OleDbConnection();
|
|
|
|
|
I have listbox control which is multiextended
Now i want to go to some of the values of listbox which are pre known
say item[0]
item[3] and item[6]
and check wether they are selected or not
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
You can get selected items by Listbox.SelectedItems property and then choose if it contains any of the items you are interested in.
|
|
|
|
|
I have to go through certain items
and perform action on them dependinng upon wether they are selected or not selected i am performing action in both cases
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
So what's the problem? If you have one array of items you should go through, you can get selected items by ListBox.SelectedItems, then iterate over your items array and check if it is in the selected items. You can use Contains method to do that
|
|
|
|
|
Generally You can use this.listBox1.SelectedItems orthis.listBox1.SelectedIndices properties for retrieving selected items
one of the solutions would be check the collection from SelectedIndeices property to see wether your desired indices are selected or not
System.Windows.Forms.ListBox.SelectedIndexCollection indices = this.listBox1.SelectedIndices;
if (indices.Contains(0) && indices.Contains(3) && indices.Contains(6))
but more alghoritmic way is
bool CheckIsSelected(int[] desiredIndices)
{
System.Windows.Forms.ListBox.SelectedIndexCollection indices = this.listBox1.SelectedIndices;
bool selected = true;
foreach (int index in desiredIndices)
if (!indices.Contains(index))
{
selected = false;
break;
}
if (selected) return true;
return false;
}
good luck
|
|
|
|
|
it's not showing "SelectedIndexCollection" after
System.Windows.Forms.ListBox.
i am using .net1.1
Ankit Aneja
"Nothing is impossible. The word itself says - I M possible"
|
|
|
|
|
I compiled in VS2003 (.NET1.1) and it is working ,may be it's the intellisense bug because I installed a plugin (Resharper) and don't use intellisense of VisualStudio and it showing it to me
just write it and see it would compile or not
i'm sure it would compile
good luck
|
|
|
|
|
Here is the complete code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
namespace ListBoxTest
{
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.Button button1;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
private void InitializeComponent()
{
this.listBox1 = new System.Windows.Forms.ListBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
this.listBox1.Items.AddRange(new object[] {
"1",
"2",
"3",
"4",
"5",
"6",
"7"});
this.listBox1.Location = new System.Drawing.Point(144, 24);
this.listBox1.Name = "listBox1";
this.listBox1.SelectionMode = System.Windows.Forms.SelectionMode.MultiExtended;
this.listBox1.Size = new System.Drawing.Size(352, 225);
this.listBox1.TabIndex = 0;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
this.button1.Location = new System.Drawing.Point(144, 272);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(352, 24);
this.button1.TabIndex = 1;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(632, 446);
this.Controls.Add(this.button1);
this.Controls.Add(this.listBox1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
}
private void button1_Click(object sender, System.EventArgs e)
{
int[] desiredIndices = new int[] { 0, 3, 6 };
MessageBox.Show(CheckIsSelected(desiredIndices).ToString());
}
bool CheckIsSelected(int[] desiredIndices)
{
System.Windows.Forms.ListBox.SelectedIndexCollection indices = this.listBox1.SelectedIndices;
bool selected = true;
foreach (int index in desiredIndices)
if (!indices.Contains(index))
{
selected = false;
break;
}
if (selected) return true;
return false;
}
}
}
good luck
|
|
|
|
|
In my application I need to create some files dynamically. So if the file is already in use, it will raise exception. So is there any better approach to identify file is in use instead of raising exception and checking ?
|
|
|
|
|
How can a file be in use if you are creating it? If you want to check whether the file exists or not you can use File.Exists("path to the file")
|
|
|
|
|
Giorgi Dalakishvili wrote: How can a file be in use if you are creating it?
Maybe a fielstream, which references the file, has not been closed.
If you than try to delete the file, it will throw an exception, I guess!
All the best,
Martin
|
|
|
|
|
Yes that is true but if you are creating a file it can't be in use if it doesn't already exist
|
|
|
|
|
True, I didn't got the question on the first read!
All the best,
Martin
|
|
|
|
|
Thanks,
My application will create file. File is in a network shared location. Assume another user is reading file, by the same time author who created file is trying to update the contents. So it should check file is in use and alert with a message.
I managed with putting try, catch. But I would like to know is this the correct approach to the problem, or is there any other better method available. ?
|
|
|
|
|
Giorgi Dalakishvili wrote: How can a file be in use if you are creating it?
It can,
Assume you created file and some other user in the network accessed the file. So when my application tries to modify the contents of the file, it will raise exception. For modification, I am recreating the file with same name. Is this making the problem ? So I was searching for a better approach for this problem.
|
|
|
|
|
Hi all, I'm trying to get a "read only" behaviour from common controls such as check boxes and comboboxes (disabling them is not an option, though, due to the color of the resulting text).
What I thought to do was catching a few events and, if the control is read only, then avoiding the control to respond to it.
Is there a way to 'handle' the event as with key events or with dispose with Java?
Would you otherwise suggest other approaches to get the read only behaviour?
Thank you
Alessandro
|
|
|
|
|
Hello,
Most controls react on the MouseDown event!
So it would be an option to inherit your special control from the control you would liek to modify.
Then you just have to add some properties which tell you how to react.
Your inherited control can than override the method which fires the MouseDown event, and block the call of the base method.
This will, of course lead to an unexpected reaction (nothing happens) for the user of your control.
Because if you do not change the colors or do some other notivication, he would expect the MouseDown to fir and for example the CheckBox changing their Checked state.
Here is an example for the CheckBox, where I just added a property called "ReadOnly" and overrided "OnMouseDown":
using System;
using System.ComponentModel;
namespace YourNamespace
{
public class ReadOnlyCheckBox : System.Windows.Forms.CheckBox
{
public ReadOnlyCheckBox()
{
}
private bool _readonly = true;
[Category("ReadOnlySpecials")]
[Description("This property enables/disables the functonality of the control")]
[DefaultValue(true)]
public bool ReadOnly
{
get
{
return _readonly;
}
set
{
if(value!=_readonly)
{
_readonly = value;
}
}
}
protected override void OnMouseDown(System.Windows.Forms.MouseEventArgs e)
{
if(!ReadOnly)
base.OnMouseDown (e);
}
}
}
Hope it helps!
All the best,
Martin
|
|
|
|
|
Thank you for replying, but I don't think this solves my problem: we already make it working for both comboboxes and checkboxes (the first thank to an article here on codeproject, the second using the property AutoCheck). We were also able to simulate read only behaviour for numericupdown, by setting Increment to 0 and readonly property to true;
Now we are trying with datetimepicker, without much success, and overriding the On****() methods seem to have no effect on the control...
Other ideas?
Thank you again,
Alessandro
|
|
|
|
|
Hello,
If you want to disable the Key Input, you have to override the OnKeyPress and set e.Handled to true:
protected override void OnKeyPress(System.Windows.Forms.KeyPressEventArgs e)
{
if(ReadOnly)
{
e.Handled = true;
}
base.OnKeyPress (e);
}
All the best,
Martin
|
|
|
|
|
Hello friends,
We have developed a small game(Windows Application) and now we wish to distribute it.
We just want to do authentication of user. That is licensed user only should use the software.
I just want to know, what steps should we follow and what are different techniques available to
achieve this.
Thanks in advance.
Rahul Kulkarni
|
|
|
|
|
one way of authentication is digital certificates
http://www.google.com.pk/search?hl=en&q=digital+certificates&btnG=Google+Search&meta=
|
|
|
|
|
Hi,
IMO the most important thing is not annoying your users with to heavy licensing
Probably for a game your users are just expecting a serial number. You need to pick where to store it, in your app settings, or the registry. Then you need some code to generate numbers, and some code to check if a serial number is valid.
http://www.codeproject.com/install/LicenseKeyGeneration.asp[^] - seems useful for that.
|
|
|
|
|
I tried using dispose() on a picturebox to remove the file being held open since I am trying to programmatically delete the file. But, it is giving an exception saying that the file is in use.
I am displaying the image in the following way.
pictureBox.Image = System.Drawing.Image.FromFile(path);
How can I remove it from being held open?
|
|
|
|
|
Try this code ~
private void button1_Click(object sender, EventArgs e)<br />
{<br />
string p = "C:\\a.bmp";<br />
Image img = Image.FromFile (p);<br />
pictureBox1.Image = img;<br />
MessageBox.Show ("Image added.");<br />
img.Dispose ();<br />
<br />
if ( !CheckIfFileIsBeingUsed (p) )<br />
{<br />
System.IO.File.Delete (p);<br />
MessageBox.Show ("Image deleted.");<br />
}<br />
<br />
}<br />
public bool CheckIfFileIsBeingUsed(string fileName)<br />
{<br />
System.IO.FileStream fs;<br />
try<br />
{<br />
<br />
fs = System.IO.File.Open (fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.None);<br />
fs.Close();<br />
}<br />
catch ( Exception exp )<br />
{<br />
Console.WriteLine (exp.Message);<br />
return true;<br />
}<br />
return false;<br />
<br />
}
Note that you should not directly set the image to PictureBox. Instead, use one buffer and dispose it if you don't need anymore.
Ref: This CheckIfFileIsBeingUsed method is from http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=547070&SiteID=1[^]
Hope it helps..
Thanks and Regards,
Michael Sync ( Blog: http://michaelsync.net)
If you want to thank me for my help, please vote my message by clicking one of numbers beside "Rate this message". Why vote? Plz Read it here. Thank you.
|
|
|
|