|
i have two Forms : Form1 and Form2
from Form1 i show Form2:
Form2 form2 = new Form2();
form2.Show();
i want from Form2 to Close Form1 by using the same code:
Form1 form1 = new Form1();
form2.Show();
but doesn't close Form1
|
|
|
|
|
Hello
I don't know why someone flamed you with score 1.0, and didn't even answer you -wasn't me btw-. Your question is quite very simple -forgive me for saying that-. I guess you didn't do much reading in OOP did you?;)
The first block of code is fine in creating a form, while the second is far from closing one:
m.m._2007 wrote: Form1 form1 = new Form1();
The above line creates a new form of type Form1. So refereing to it would be to the new form not your original form.
m.m._2007 wrote: form2.Show();
And this line shows the new form that you've just made in the previous block. Why on earth did you expect it to close the original one? Shouldn't you've called the method Close() at least?
To make your code work you have to pass reference to your original form to the Form2 object and call the Close() method for it.
Edit your Form2 constructor to
private Form1 orgigenal
Form2(Form1 or)
{
origenal = or;
}
Now show Form2 like this:
Form2 myForm = new Form2(this);
and in Form2 close Form1 like this
origenal.Close();
I hope you got it, and I strongly suggest you do more reading in C# OOP programming.
-- modified at 21:59 Tuesday 24th October, 2006
Regards
|
|
|
|
|
form2 is builded by form1
You close form1 in form2,and the application will terminate
The code like this:
Code in Form1:
private void button1_Click(object sender, System.EventArgs e)
{
Form2 form2 = new Form2();
form2.Frm = this;
form2.Show();
}
Code in Form2:
///
/// Form which will close
///
private Form _frm = null;
public Form Frm
{
get
{
return _frm;
}
set
{
_frm = value;
}
}
private void button1_Click(object sender, System.EventArgs e)
{
Frm.Close();
}
phenix-burn
|
|
|
|
|
How exactly does this method work?
It seems to me that it's supposed to check the Socket object state for the number of specified miliseconds and if during this period it will determine that the Socket is in state given in the method, it will return true or false if it's not and if the time elapses and the state won't match the definition given in the method it will return false. Is this correct? Does poll method block the socket?
Next questions concerns the POP3 service. I'm trying to connect to the service and authenticate user. My program always return exception when it is trying to connect to pop3 server for the first time after boot up. I used the polling to determine if the socket state is "read" (after connecting to service) with 20 second timeout. If it is then the the Receive method of the socket is called if it's not the exception is returned. I always get the exception when I try to connect after boot up.
What may be the reason of this? How would You get around it?
Thanks in advance.
p.s.: The socket is in blocking mode.
|
|
|
|
|
I need to compute the number of months between two dates (DateTime objects). Does anyone know a way to do this? I know I can count the days between the dates by subtracting one from the other to get a TimeSpan value and grab its Days value, but how about the number of months in between the dates?
|
|
|
|
|
That depends on how you define the difference in monhts.
You can calculate a linear month index by taking the year times twelve plus the month. Then you can compare the indices to get the difference is monhts. That of course means that the difference between 2006-10-31 and 2006-11-01 is one month.
---
b { font-weight: normal; }
|
|
|
|
|
with a control when it is double clicked? When a customized control is double clicked, it takes the right mouse button to cause the code to be executed. I checked for articles but could not find one that clearly explained why the left mouse button does not work with a customized control.
It works fine with a regular command button, but for some reason it does not work with a custom run-time control. What are the requirements for connecting event handlers so that the left mouse button will properly invoke the click and double click event handlers? Why is the right mouse button even involved when a customized run-time control is a either clicked or double clicked?
I would appreciate some assistance. I am certain that there are others that would like to know the reason and how to correct it.
-- modified at 18:31 Tuesday 24th October, 2006
|
|
|
|
|
Anybody know how to get a CollectionEditor derived class to properly edit a List<t> collection? What I have is an abstract base class that is passed to a List<> collection, like so:
public List<BaseThing> Things
{
...
}
I've got several classes that derive from BaseThing, and they're presented to the CollectionEditor via the CreateNewItemsTypes method. I can open the editor, but as soon as I click the 'Add' button, I get the following error:
The value 'DerivedThing' is not of type 'BaseThing' and connot be used in this generic collection.
This seems like a limitation of the editor, or in how I'm implementing something, because in my NUnit test cases, one of my tests is to create this List<basething>, and add several derived items to it, and it works.
Any ideas? Thanks in advance.
Kyosa Jamie Nordmeyer - Taekwondo Yi (2nd) Dan
Portland, Oregon, USA
|
|
|
|
|
Hi,
I am trying to use the settings.designer.cs file for a value that needs to be put into the app.config file.
My config(auto-generated by VS2005) file now looks like this:
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Settings1" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</sectionGroup>
</configSections>
<applicationSettings>
<General.Settings1>
<setting name="DatabasePath" serializeAs="String">
<value>C:\Web\App_Data\Messages.mdb;</value>
</setting>
</General.Settings1>
</applicationSettings>
</configuration>
From the code when i access this property i am using this code:
Settings1 set = Settings1.Default;
string str = set.DatabasePath;
return str;
The above line of code returns null... can anyone explain what am i missing here???
|
|
|
|
|
Try this:
Properties.Settings set = Properties.Settings.Default;<br />
string str = set.DatabasePath;<br />
return str;
or simply
return Properties.Settings.Default.DatabasePath;
Cheers,
Craig
** I'd rather try and fail than fail to try **
|
|
|
|
|
i know this question is dull but i need to ask it guys. If i have an array of strings lets say ( string[] array = {"1","2","4","boss","678"} ) and i need to pick one randomly without a specific string lets say "4" (the answer should not never be 4). May i know how ? Thanks for the help,
P.S: i can do it in different ways but i need it in one line of code and that, my friends , i dont know how.
Any help appreciated .... Thanks guys
|
|
|
|
|
I don't think your going to get it done in one line of code. If you are trying to keep your code simple, then I would write a routine that did the job and let calling it be your one line of code. So for example, I would write a function called GetRandomValue that handled all of the work, and then simply call that function when I needed it.
private object GetRandomValue(object[] possibleValues, object illegalValue)<br />
{<br />
Random randomIndexGenerator = new Random();<br />
int index = randomIndexGenerator.Next(possibleValues.Length-1);<br />
object randomValue = possibleValues[index];<br />
while(object == illegalValue)<br />
{<br />
index = randomIndexGenerator.Next(possibleValues.Length-1);<br />
randomValue = possibleValues[index];<br />
}<br />
return randomValue;<br />
}
|
|
|
|
|
Thanks man it has been very helpful ... but what i am really looking for is that i need to pick a random number from array0 excluding a certain number. the new array(the old one excluding that value)is array1(array1= array0-x) and then i need to pick randomly from array2 which is array1 excluding a new variable y) so array2 = array1 - y = (array0 - x)-y and so on. hope i made myself clear. I wanna thank you again
|
|
|
|
|
If you explain what you are trying to accomplish, it might be possible to give a useful answer. It might answer some of those questions, for example:
Is it the string value or the item in the array that is important?
May the string value occur more than once in the array?
Does the string value always occur at least once in the array?
Why do you need this in one line of code?
Why do you specify one line of code in a language that isn't line oriented?
How complex may that line of code be?
---
b { font-weight: normal; }
|
|
|
|
|
okay i am gonna explain what i am trying to do... i am doing a poker game as a project.... we cant have the same card twice. i am putting strings in an array {"11","21"..."131","12","22"..."132"..."}(ex:"34"is the card 3 of clubs, 3 number, 4 kind) . Since we cant have same card twice i used a while loop on each card. but since the while loop is causing the application to freeze with CPU usage 100% i thought i can ,on each card, pick up the card randomly from the an new array which is the old array excluding the names of the cards already picked.
Thanks again.
|
|
|
|
|
Use a list instead of an array, then you can remove the card from the list.
Here is a bit more object oriented approach, instead of using strings...
public enum Kind { Hearts, Clubs, Diamonds, Spades }
public class Card {
private Kind _kind;
private int _value;
public Card(Kind kind, int value) {
_kind = kind;
_value = value;
}
public Kind Kind { get { return _kind; } }
public int Value { get { return _value; } }
public string ValueString {
get {
switch (_value) {
case 1: return "Ace";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return _value.ToString();
}
}
}
public override ToString() {
return this.ValueString + " of " + _kind.ToString();
}
}
public class Deck {
private List<Card> _cards;
private Random _rnd;
public Deck() {
_cards = new List<Card>(52);
for (int i = 1; i <= 13; i++) {
_cards.Add(new Card(Kind.Hearts, i));
_cards.Add(new Card(Kind.Clubs, i));
_cards.Add(new Card(Kind.Diamonds, i));
_cards.Add(new Card(Kind.Spades, i));
}
_rnd = new Random();
}
public Card GetRandom() {
if (_cards.Count == 0) throw new ApplicationException("Deck is empty.");
int pos = _rnd.Next(_cards.Count);
Card card = _cards[pos];
_cards.RemoveAt(pos);
return card;
}
}
Example of usage:
Deck deck = new Deck();
List<Cards> hand = new List<Cards>();
for (int i=0; i<5; i++) {
hand.Add(deck.GetRandom());
}
for (int i=0; i<5; i++) {
Console.WriteLine(hand[i].ToString());
}
Example output:
King of Spades
4 of Diamonds
8 of Clubs
Queen of Hearts
Ace of Spaces
Disclaimer: None of the code here is tested.
---
b { font-weight: normal; }
|
|
|
|
|
Blackocebo wrote: Hello man,
I was impressed by your posts and i really need your help. i am creating a card game ( some kind of poker) to give it to my friend in order to play it for free and no more spend money to play it.
I have recently finished the game but i am facing a problem, when pressing the (DEAL) button sometimes, at some points, the application freezes. The CPU usage reaches 100%. So i want to ask you as an expert what should i do.
[lengthy code not included]
Well, let's look at a part of the code:
num2 = randomnum.Next(1, 14);
kind2 = randomnum.Next(1, 5);
string string2 = num2.ToString() + kind2.ToString();
while (string2 == string1)
{
num2 = randomnum.Next(1, 14);
kind2 = randomnum.Next(1, 5);
}
As you don't change any of the strings inside the loop, that you use in the condition, if you ever enter into the loop, you will never exit.
The same of course applies to the three following loops too.
---
b { font-weight: normal; }
|
|
|
|
|
I want to thank u very very much ...
Respect
|
|
|
|
|
i want to write a applicatin that delete files from a specific directory how can i do this?
can any one show me a sample code to achieve this or point me to the class that can be used to do this
kenny
|
|
|
|
|
Look at the System.IO namespace, particularly the Directory and Files classes. The Directory class as a static GetFiles member that gets a list of files and the File class has a static Delete method that can delete the file.
foreach(string file in Directory.GetFiles([path])<br />
{<br />
File.Delete(file);<br />
}
|
|
|
|
|
How can i manage database transactions between layers? Using Presentation, business and data layers. One solution i found is to declare transaction in the business layer then pass it as parameter to the DAL, but i think that's the worse i can do. Any ideas?
Never argue with an idiot. They drag you down to their level, then beat you with experience. - Dilbert
|
|
|
|
|
That depends on what you're using. If you're .Net 2.0 and all on the same machine, just use the System.Transactions namespace. Open up a TransactionScope and every call that gets made within that will subscribe to that transaction automatically. If you're in .Net 1.1, you could use COM+ (System.EnterpriseServices) and carry the transaction that way. If you're on different machines separated by web services or remoting, it gets a little more complex. In .Net 1.1 and 2.0 you can use TIP (Transaction Internet Protocol), which is a part of COM+ to handle transactions across machines. But an even better solution comes with .Net 3.0 where WCF gives you the ability to carry transactions across services very easily.
I have an article on TIP[^] and an article on WCF[^] that explains how to spread those transactions across machines. Essentially you can start a transaction in the presentation layer and even if the DAL is on another machine, it can subscribe to it.
|
|
|
|
|
ok. Everything is on one machine... i'm going to try with TransactionScope then, and look for some article. Thanks for answer this Dustin, i needed the advice because i'm just starting using layers.
Never argue with an idiot. They drag you down to their level, then beat you with experience. - Dilbert
|
|
|
|
|
Hello,
I have a .MHT file with my C# application, I use WebBrowser control to navigate to it at runtime. Is it true that the user should have outlook installed in order to display the mht file correctly as it's stated on MS site:
NOTE: "Web archive" is available only if Outlook Express is installed. The ability to save a Web page as a Web archive file is provided by the Inetcomm.dll file (the Microsoft Internet Messaging API file), which is installed by Microsoft Outlook Express 5.
http://support.microsoft.com/kb/221787
Please advice. Thanks.
|
|
|
|
|
I take it as gospel, after I tried it without Outlook Express installed. BTW, I think OE is installed by default in every Windows installation since Windows 98. You'd probably have to build a machine from scratch to test this. A Virtual Machine, like VMWare or Microsoft's Virtual PC, is great for stuff like this.
Dave Kreskowiak
Microsoft MVP - Visual Basic
|
|
|
|