Hi guys,
Anyone know how to do this:
I open Form2 when a button is clicked on Form1. To return back to Form1 I have a button to do so on Form2. However, is it possible to return back to Form1 automatically after Form2 has displayed for a set amount of time - lets say 2 seconds? This way I could do away with the button on Form2 - which is my goal.
Thanks much, magohn
Drop a timer from the tool box on to Form2 and set its interval to 2000 (2 sec). Then insert the following code into the Form.Activated() and Timer.Tick() events
private void Form2_Activated(object sender, EventArgs e)
{
this.timer1.Enabled = true;
}
private void timer1_Tick(object sender, EventArgs e)
{
this.timer1.Enabled = false;
this.Close();
}
Thank you SO much...
Thanks again stephj - I modified your code a little as I needed Form2 to be "re-useable" and repro the same action over and over. Thanks!
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Timer1.Enabled = False
Me.Hide()
Timer1.Enabled = True
End Sub
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
I did think afterwards that my example was a bit resource hungry as the Form.Close() method actually destroys the form rather than 'deactivate' it. I threw it together as a quick test, just to prove the point.
Glad it all works.
"I love it when a plan comes together." John "Hannibal" Smith
Related
hello, I'm trying develop an application for my HTC Touch Pro using C# in Visual Studio 2008. Though programming is not new to me, but programming for Window Mobile is brand new to me. I'm currently stuck on trying to make 3d button so it could look nicer but couldn't figure out how to do that. If someone could show me how to do it, it would be great, or if you have some source code I could take a look, it would also be great. I tried to search in google but w/ no luck.
Im not sure on how to do it, But you should upload it to the forums after your done.
It doesn't have to be a button. Use a picturebox object instead and load it with the image of your button. It still has Click and Double Click events that can be used to detect mouse clicks.
To make it look really good, create an image of your button in the 'button pressed' mode and trap the mouse down and mouse up events to switch the button image. Store the two images in an imagelist object and swap them in and out.
Code:
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Image = imagelist.Images[1];
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Image = imagelist.Images[0];
}
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
// Todo button click action code goes here.
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[0];
}
Form load code required to set it up. Just tried this all out. Works a treat. Buttons created with Steffan Gerlach's ZPaint, a great bit of freeware.
It doesn't quite behave exactly as a Button, if you click and hold the mouse down, then move it off the PictureBox, the image remains "down" until the mouse button is released.
stephj said:
It doesn't have to be a button. Use a picturebox object instead and load it with the image of your button. It still has Click and Double Click events that can be used to detect mouse clicks.
To make it look really good, create an image of your button in the 'button pressed' mode and trap the mouse down and mouse up events to switch the button image. Store the two images in an imagelist object and swap them in and out.
private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
{
pictureBox1.Image = imagelist.Images[1];
}
private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
{
pictureBox1.Image = imagelist.Images[0];
}
private void pictureBox1_Click(object sender, MouseEventArgs e)
{
// Todo button click action code goes here.
}
private void Form1_Load(object sender, EventArgs e)
{
pictureBox1.Image = imageList1.Images[0];
}
Form load code required to set it up. Just tried this all out. Works a treat. Buttons created with Steffan Gerlach's ZPaint, a great bit of freeware.
It doesn't quite behave exactly as a Button, if you click and hold the mouse down, then move it off the PictureBox, the image remains "down" until the mouse button is released.
Click to expand...
Click to collapse
Thank you.
Hi Everyone,
Using the SkeletonApp default code, how would I insert a void or whatever (sorry not knowing the terminology yet) that would run each cycle of the program like a counter or something?
Just keep it simple, just a "run this always" loop while maintaining the button functions would be great.
thanks and hope you all had a happy thanksgiving
So I added this and it does not work, how in the world do I get a counter working ?
Please, Please someone point me in the right direction?
All I want it to do (in the default SkellyApp) is count to 30, then end the proggy. And yes, I did make the mytimer a variable at the top of my proggy.
public int mytimer;
-------- stuff ---------
@Override
public void onResume() {
mytimer = (mytimer + 1);
if (mytimer == 30) finish();
super.onResume();
}
thank you in advance for taking the time to look, and HOPEFULLY answer this for me.
[Q] How to code a "Really quit?" question when user press Back/Home buttons ?
Hello,
I am a noob ANDROID developer.
(I have a simple problem, I searched the forums and didn't found a solution.)
I am developing a strategy game/wargame on Android.
I am looking for a way to code a "really quit?" routine when the user press the back/home buttons.
(if that happens by mistake now, the whole app starts from scratch on restart)
I've seen tons of code examples, but not for this. I have only one (commercial) app that shows that exact behavior, but no access to that particular source code.
I've fiddle with the onPause(), onRestart(), onStop() , onDestroy() methods, trying to halt the process before exiting, but only got into error messages.
Basically when "back" is pressed I loose my "game screen" instantly. I've also looked into the onBackPressed() method, but then this seems to be an issue between Android 1.5/2.x. I'd like my app to run on both platforms.
Thanks for your help in this matter.
Mmm, as far as I know, you can also try to override the onKeyPressed (don't sure of the name, check the API) and check if the key pressed is back key. I think this is compatible with 1.X
http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html
Android < 2.0
Code:
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
Android > 2.0
Code:
@Override
public void onBackPressed() {
// do something on back.
return;
}
If you don't add any code, a back button press will be ignored.
Call finish() method of your running Activity to stop your application.
Deleted post (question already answered)
Thanks!
Here the "quick and dirty" way I did it: User now have to press Back twice to get out!
===
int backPressed=0;
@Override
public boolean onKeyDown(int keyCode, KeyEvent event){
if(keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0){
backPressed++;
}
if(backPressed>1){
return super.onKeyDown(keyCode,event);
}
}
return false;
}
===
(Sorry for the indentation, tabs didn't copy..)
Hi, I'm trying to do a simple login with Facebook in my app but I'm having trouble with Shared Preferences.
The idea is to start the app, it opens Activity A, checks if it's logged, and if it isn't, it sends you to activity B, you login and then go back to A.
My problem is that I can't get the SharedPreferences. I can save it, but I can't get it in the other activity.
So, it gets in a loop: A can't get the SP, so thinks it's not logged in, so send you to B, but B is logged on, and sends you to A...
That's my code in B:
Code:
public void onComplete(Bundle values) {
// TODO Auto-generated method stub
Editor edit = fbSP.edit();
edit.putString("access_token", fb.getAccessToken());
edit.putLong("access_expires", fb.getAccessExpires());
edit.commit();
aIMG();
ir();
}
And that's my code in A, where the problem is:
Code:
private SharedPreferences prefs;
public static String TOKEN = null;
public static final String FACEBOOK_DATA = "FacebookStuff";
long EXPIRES = 0;
...
private void SharedP() {
// TODO Auto-generated method stub
prefs = getSharedPreferences(FACEBOOK_DATA, MODE_PRIVATE);
TOKEN = prefs.getString("access_token", null);
EXPIRES = prefs.getLong("access_expires", 0);
if (TOKEN == null && EXPIRES == 0) { //If it's not logged in...
Intent login = new Intent("android.intent.action.FACELOGIN");
startActivity(login);
}
}
Edit: I got it. I was iniciating fbSP with getPreferences, not getSharedPreferences.
Hi everyone
I was searching one app for android, but i cannot find any like that.
Basically i need to understand when my flat went down on electricity
beacuse i have food in the fridge, fish in the acquarium or whatever you may have.
I was thinking i could leave a currently unused phone on charge in the wall socket.
When electricity goes off the phone stop charging and it can notify me with email.
Do you think something like that exist?
A general event driven notifier that support email would be fine.
Is there anything like that? on the market even a paid app?
Best regards, Andrea
You could try using Tasker for it.
"To err is human, to forgive is divine"
Sent from my SGS II
Tasker do half of the job...
it detects what i need, it compose the email...
but it cannot send it!!!
It can open a web url directly, so i can program a simple php script to send an email.
But if i could do without a webserver helping me it would be better.
I think that SMS notification will be more reliable in this case. Anyway all that you need is a very simple app, probably no more than several lines of code. If you are still interested in this I can help you to write one (if you have at least basic programming knowledge) or write one for you.
qubas said:
I think that SMS notification will be more reliable in this case. Anyway all that you need is a very simple app, probably no more than several lines of code. If you are still interested in this I can help you to write one (if you have at least basic programming knowledge) or write one for you.
Click to expand...
Click to collapse
solved with tasker. i make it open weburl, on web url i made application that send email.
cause tasker compose but cannot send.
i have programming knowledge,i would like to start,i just don t like java
maybe just a problem of mine
do you have some empty app franework to look at?
asturur said:
solved with tasker. i make it open weburl, on web url i made application that send email.
cause tasker compose but cannot send.
i have programming knowledge,i would like to start,i just don t like java
maybe just a problem of mine
do you have some empty app franework to look at?
Click to expand...
Click to collapse
There are many tutorials for installing Android Development Tools (ADT) and creating simple "Hello world" app. It probably takes 1 hour to do that. For simple app you don't really have to know Java, copy-paste some code samples from the Internet should do the trick, you just need to have at least some basic programming knowledge.
I still think that sending SMS is the most reliable solution. Using Wifi is a bad idea, because it won't work when there is no AC, unless you have a UPS. With 3G its much better, but still you have to rely on the web service you use to send email.
Anyway, here is my code:
Code:
package com.example.powermonitoring;
import android.os.BatteryManager;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.telephony.SmsManager;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String NUMBER = "+48123456789";
private static final int THRESHOLD = 10;
private static final String AC_ONLINE = "AC is back online!";
private static final String AC_OFFLINE = "AC is offline!";
private Handler handler = new Handler();
private int counter;
private boolean lastPowerState;
private boolean powerState;
private Runnable runnable = new Runnable() {
[user=439709]@override[/user]
public void run() {
powerState = isPlugged(MainActivity.this);
if (powerState != lastPowerState) {
counter++;
if (counter == THRESHOLD) {
counter = 0;
lastPowerState = powerState;
String message;
if (powerState) {
message = AC_ONLINE;
} else {
message = AC_OFFLINE;
}
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show(); //for testing
//SmsManager sms = SmsManager.getDefault();
//sms.sendTextMessage(NUMBER, null, message, null, null);
}
}
handler.postDelayed(this, 1000);
}
};
public static boolean isPlugged(Context context) {
Intent intent = context.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
int plugged = intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
return plugged == BatteryManager.BATTERY_PLUGGED_AC || plugged == BatteryManager.BATTERY_PLUGGED_USB;
}
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lastPowerState = isPlugged(MainActivity.this);
handler.postDelayed(runnable, 1000);
}
}
You just have to remember about including permission in AndroidManifest.xml :
Code:
<uses-permission
android:name="android.permission.SEND_SMS"
/>