Xamarin - Morse code app usuing the flaslight - c#

I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}

first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}

Related

if statements in C# for a textbox to change back to normal when i press a button for the second time

This is the code that i used to change the text in the text box from "Livre" to "Ocupado"
What code should i use to change it from "Ocupado" to "Livre"
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text="Ocupado";
}
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text == "Livre")
{
textBox1.Text = "Ocupado";
}
else
{
textBox1.Text = "Livre";
}
}
you can add a variable to your class
e.g.:
bool livre = true;
private void button1_Click(object sender, EventArgs e)
{
if (livre)
{
textBox1.Text="Ocupado";
}
else
{
textBox1.Text="Livre";
}
livre = !livre;
}

Can't put more than one number in the textbox. in my Simple Calculator program

Need help in making a simple calculator. i can't put more than one number in my calculator's textbox. Everytime i put a second number it replaces the first one need help!
I can't exceed more than one input number in my Calculator's Textbox instead it replaces the first number with a second number input
namespace Calculator_Project
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void InputOutputArea_TextChanged(object sender, EventArgs e)
{
}
private void One_Click(object sender, EventArgs e)
{
int Input = 1;
InputOutputArea.Text = Input.ToString();
}
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text = Input.ToString();
}
private void Three_Click(object sender, EventArgs e)
{
}
private void Four_Click(object sender, EventArgs e)
{
}
private void Five_Click(object sender, EventArgs e)
{
}
private void Six_Click(object sender, EventArgs e)
{
}
private void Seven_Click(object sender, EventArgs e)
{
}
private void Eight_Click(object sender, EventArgs e)
{
}
private void Nine_Click(object sender, EventArgs e)
{
}
private void Eql_Click(object sender, EventArgs e)
{
}
private void AddB_Click(object sender, EventArgs e)
{
}
private void Minus_Click(object sender, EventArgs e)
{
}
private void MultiplyB_Click(object sender, EventArgs e)
{
}
private void DivideB_Click(object sender, EventArgs e)
{
}
private void Zero_Click(object sender, EventArgs e)
{
}
private void ResetB_Click(object sender, EventArgs e)
{
InputOutputArea.Clear();
}
}
}
You should use
InputOutputArea.Text += Input.ToString();
(note the '+') in order to append to a text box.
private void Two_Click(object sender, EventArgs e)
{
int Input = 2;
InputOutputArea.Text += Input.ToString();
}
You must use += to add other text to next of first text
Here is your problem:
InputOutputArea.Text = Input.ToString();
This replaces the content of the textbox instead of adding to it.
InputOutputArea.Text += Input.ToString();
the above code should do as you ask.
Good to remember is that concatenating strings with + is rather inefficient, so don't do this in performance critical code unless absolutely necessary. In those cases a String-builder is almost always better.
Every answers talking about the Concatenation of the previous text with the current, But I would like to suggest something more than that;
You need not to create separate event handlers for all your buttons that are doing same tasks, Hope that the Text of each button will be the number that you need to display in the textBox(say btnOne will holds 1 and btnTwoholds 2 and so on). By make use of this Text we can reuse the handlers like the following, Let btnNumber_Click be the handler and which is defined like the following:
private void btnNumber_Click(object sender, EventArgs e)
{
Button currentButton = sender as Button;
InputOutputArea.Text += currentButton.Text;
}

c# winforms button and richtextbox

I made ​​a winform with 1 richtextbox and two buttons,
and I hope that when I click on the yes button , it will show a method soal2 in richtextbox1 , and then when I click again it will show soal3 , how to do that?
this is my design
public void soal1()
{
richTextBox1.Text = "Hemofilia is xxxxx";
}
public void soal2()
{
richTextBox1.Text = "xxxxxxx";
}
public void soal3()
{
richTextBox1.Text = "yyyyyy";
}
private void Quiz1_Load(object sender, EventArgs e)
{
soal1();
}
private void button1_Click(object sender, EventArgs e)
{
}
/* ... */
bool alreadyShownSoal2 = false;
private void button1_Click(object sender, EventArgs e)
{
if(alreadyShownSoal2)
soal3();
else
soal2();
alreadyShownSoal2 = true;
}
or
/* ... */
bool alreadyShownSoal2 = false;
public void soal2()
{
if(alreadyShownSoal2)
soal3();
else
richTextBox1.Text = "xxxxxxx";
alreadyShownSoal2 = true;
}
/* ... */
private void button1_Click(object sender, EventArgs e)
{
soal2();
}
This is an absolutely terrible design, but unless you give more specifications... it'd definitely do what you are asking

Add items from a listbox to another listbox on another form

Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU

Problem with appendText in c#

I have an app that has 3 text boxes, (users name, what company they are from, and who they are visiting) A button to print, and a keyboard on the screen (monitor is touch screen). I have everything working and functioning...
BUT, the one thing that does not work is when the user points to previous character in the text box that has already been typed, the buttons that "AppendText" (keyboard) do not start typing where the user pointed but it continues typing at the end of what has been typed.
Is this because of "AppendText" or some other issue that I have in my code?
I also am trying to get the first text box (Name_Box) to be sent to form one, which then will be split into two labels (1, first name| 2, last name) right now I have it being sent to one label But I want to split it so the first name is stacked above the second name in the next form (which is printed out).
Thank you so much.
Here is my code: First Form
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Printing;
using System.IO;
namespace SMART
{
public partial class Form1 : Form
{
private TextBox tbSelected; // Last focused TextBox
private int posCaret; // Caret position
private int selLength; // Selection length
public Form1()
{
InitializeComponent();
// We will use leave event for textboxes
Name_Box.Leave += new System.EventHandler(textBox_Leave);
Company_Box.Leave += new System.EventHandler(textBox_Leave);
Visiting_Box.Leave += new System.EventHandler(textBox_Leave);
// Set initial selection to the first textbox
Name_Box.Select();
tbSelected = Name_Box;
posCaret = 0;
selLength = 0;
}
// Leave event handler
private void textBox_Leave(object sender, EventArgs e)
{
// Remember the last focused thextbox,
// the caret position in it and the selection length
tbSelected = (TextBox)sender;
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
// Helper method to restore selection
private void RestoreLastSelection()
{
tbSelected.Select();
posCaret = tbSelected.SelectionStart;
selLength = tbSelected.SelectionLength;
}
private void Form1_Load(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
}
private void button1_Click(object sender, EventArgs e)
{
label5.Text = DateTime.Now.ToString();
Form2 frm = new Form2(Name_Box.Text);
frm.Show();
frm.Close();
StreamWriter sw;
sw = File.AppendText ("C:\\SignIn.txt");
sw.WriteLine ("Date and Time: " + label5.Text + " | Name: " + Name_Box.Text + " | Company: " + Company_Box.Text + " | Visiting: " + Visiting_Box.Text + " |");
sw.Close ();
Name_Box.Clear();
Company_Box.Clear();
Visiting_Box.Clear();
}
private void button42_Click(object sender, EventArgs e)
{
//SPACE BAR
tbSelected.AppendText(" ");
}
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
private void button12_Click(object sender, EventArgs e)
{
tbSelected.AppendText("-");
}
private void button13_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Q");
}
private void button14_Click(object sender, EventArgs e)
{
tbSelected.AppendText("W");
}
private void button15_Click(object sender, EventArgs e)
{
tbSelected.AppendText("E");
}
private void button16_Click(object sender, EventArgs e)
{
tbSelected.AppendText("R");
}
private void button17_Click(object sender, EventArgs e)
{
tbSelected.AppendText("T");
}
private void button18_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Y");
}
private void button19_Click(object sender, EventArgs e)
{
tbSelected.AppendText("U");
}
private void button20_Click(object sender, EventArgs e)
{
tbSelected.AppendText("I");
}
private void button21_Click(object sender, EventArgs e)
{
tbSelected.AppendText("O");
}
private void button22_Click(object sender, EventArgs e)
{
tbSelected.AppendText("P");
}
private void button25_Click(object sender, EventArgs e)
{
tbSelected.AppendText("A");
}
private void button26_Click(object sender, EventArgs e)
{
tbSelected.AppendText("S");
}
private void button27_Click(object sender, EventArgs e)
{
tbSelected.AppendText("D");
}
private void button28_Click(object sender, EventArgs e)
{
tbSelected.AppendText("F");
}
private void button29_Click(object sender, EventArgs e)
{
tbSelected.AppendText("G");
}
private void button30_Click(object sender, EventArgs e)
{
tbSelected.AppendText("H");
}
private void button31_Click(object sender, EventArgs e)
{
tbSelected.AppendText("J");
}
private void button32_Click(object sender, EventArgs e)
{
tbSelected.AppendText("K");
}
private void button33_Click(object sender, EventArgs e)
{
tbSelected.AppendText("L");
}
private void button35_Click(object sender, EventArgs e)
{
tbSelected.AppendText("Z");
}
private void button36_Click(object sender, EventArgs e)
{
tbSelected.AppendText("X");
}
private void button37_Click(object sender, EventArgs e)
{
tbSelected.AppendText("C");
}
private void button38_Click(object sender, EventArgs e)
{
tbSelected.AppendText("V");
}
private void button39_Click(object sender, EventArgs e)
{
tbSelected.AppendText("B");
}
private void button40_Click(object sender, EventArgs e)
{
tbSelected.AppendText("N");
}
private void button41_Click(object sender, EventArgs e)
{
tbSelected.AppendText("M");
}
private void button2_Click_1(object sender, EventArgs e)
{
tbSelected.AppendText("'");
}
private void button3_Click(object sender, EventArgs e)
{
tbSelected.Clear();
}
}
}
Heres is my code: Second Form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;
namespace SMART
{
public partial class Form2 : Form
{
public Form2(string strTextBox)
{
InitializeComponent();
label3.Text = strTextBox;
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
PrintDocument pd = new PrintDocument();
Margins margins = new Margins(0, 0, 0, 0);
pd.DefaultPageSettings.Margins = margins;
pd.PrintPage += new PrintPageEventHandler(PrintImage);
pd.Print();
/*
//My sad attempt at splitting the Name
var fullname = strTextBox;
var names = fullname.Split (" ");
label3.Text = names[0];
label5.Text = names[1];
*/
}
void PrintImage(object o, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
this.DrawToBitmap(img, bounds);
Point p = new Point(0, 0);
e.Graphics.DrawImage(img, p);
}
}
}
You're right in that your problem is the use of AppendText, which always appends to the end (that's what append means).
You need to Insert the character at the current carat position.
You might do better to post a message that simulates a keypress from a physical keyboard.
If you want to insert text at the user's current position, you can use SelectedText. This will replace the current selection (if the user has selected characters):
tbSelected.SelectedText = "V";
Edit: The problem is in here:
private void button24_Click(object sender, EventArgs e)
{
//DELETE
string t = tbSelected.Text;
if (t.Length > 0)
{
tbSelected.Text = t.Remove(t.Length - 1);
}
}
You set the text, which returns the cursor to the beginning of the textbox. You should set tbSelected.SelectionStart after you clear the text.

Categories

Resources