c# winforms button and richtextbox - c#

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

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;
}

Xamarin - Morse code app usuing the flaslight

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 += "-";
}

How to disable a combobox on clicking on another combox list data in C sharp?

I want to design a windows form using C sharp on Visual Studio 2013.
I go through the Source from here. but did not got it properly.
for that I have 3 combobox. I want to disable combobox2 when I click on combobox1 NSSCM element and enable when click on NSSFO element.
Below is my part of code snippet:
namespace NSE_First_Form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MaximizeBox = false;
MinimizeBox = false;
if (true)
{
comboBox1.Items.Add(Exchange.NSSCM.ToString());
comboBox1.Items.Add(Exchange.NSSFO.ToString());
comboBox1.Items.Add(Exchange.BSSCM.ToString());
}
}
private void button1_Click(object sender, EventArgs e)
{
string selectedItem = string.Empty;
ProcessValue(selectedItem);
}
public enum Exchange
{
NSSCM = 1,
NSSFO = 2,
BSSCM = 3
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
Try this:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 0)
comboBox2.Enabled = false;
if (comboBox1.selectedIndex == 1)
comboBox2.Enabled = true;
}
Try this:
//This will disable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = false;
}
//This will enable combobox2 on the click of it
private void comboBox1_Click(object sender, EventArgs e)
{
comboBox2.Enabled = true;
}
Because you want it on click, use the CLICK event, instead of SelectedIndexChange event.

How write letters using the buttons in various textboxs?

I have small app and I want write text in different textboxs using buttons.
This is my code , but click on button do not write text to text .
Please advise me.
What should I change?. In notepad it all works , but not in Textboxs.
enter image description here
public partial class Form1 : Form
{
protected override CreateParams CreateParams
{
get
{
CreateParams param = base.CreateParams;
param.ExStyle |= 0x08000000;
return param;
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
{
SendKeys.Send("A");
}
}
private void button3_Click(object sender, EventArgs e)
{
{
SendKeys.Send("B");
}
}
private void button2_Click(object sender, EventArgs e)
{
{
SendKeys.Send("C");
}
}
private void button4_Click(object sender, EventArgs e)
{
{
SendKeys.Send("D");
}
}
}
}
May be you should do something like this
private void button3_Click(object sender, EventArgs e)
{
//SendKeys.Send("B");
txtBox.Text += "B";
}
But because you don't know which text box to edit, you need to introduce variable
private TextBox _currTextBox;
// wire all text boxes to this "enter" event
private void txtBox_Enter(object sender, EventArgs e)
{
_currTextBox = (TextBox)sender;
}
// and accordingly
private void button3_Click(object sender, EventArgs e)
{
_currTextBox.Text += "B";
}

c# error cant understand what i should do to solve it

hi i have this code(and i get the ERROR that i must declare a body because it is not not marked abstract,extern or partial...by the way i couldnt get it to post it here but right over the public partial class Form1 : Form i also have written bool play1 = true;bool play2 = false; int x=1;int o=10; )when i click on it it goes up to that area that i marked with fat letters
could someone please show me what is wrong and how to solve it step by step
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
værdier();
int[] status = new int[9];
zeihne();
}
private string[] status;
private void value()
{
int[] status = new int[9];
zeihne();
}
private void zeihne()
{
button1.Text = status[0];
button2.Text = status[1];
button3.Text = status[2];
button4.Text = status[3];
button5.Text = status[4];
button6.Text = status[5];
button7.Text = status[6];
button8.Text = status[7];
button9.Text = status[8];
}
// private void label1_Click(object sender, EventArgs e)
{
}
**private void Form1_Load(object sender, EventArgs e);**
private void button1_Click(object sender, EventArgs e)
{
if (play1 == true)
{
play1 = true;
button1.Text = "X";
play1 = false;
}
else
{
play2 = true;
button1.Text = "O";
play2 = false;
play1 = true;
}
You haven't defined the method body for
private void Form1_Load(object sender, EventArgs e);
That's an abstract method and they are only allowed on abstract classes
Something like this would do the trick
private void Form1_Load(object sender, EventArgs e)
{
}
Or you can just get rid of the method if you don't plan to have any code on it.
EDIT
Something else that looks weird on your code is
// private void label1_Click(object sender, EventArgs e)
{
}
What are you trying to do? Two options:
// Option1: All the method commented
*/private void label1_Click(object sender, EventArgs e)
{
}*/
// Option2: Nothing commented
private void label1_Click(object sender, EventArgs e)
{
}
Hello Hwoarang H i tryed your code in my form and its working fine. You have to just follow step.
You have difine your method like this
private void Form1_Load(object sender, EventArgs e);
This is not right way to define method like this.You must declare like bellow
private void Form1_Load(object sender, EventArgs e)
{ }
Now remove your lable click event and brackets
// private void label1_Click(object sender, EventArgs e)
//{
//}
Now you got warning like x is issigned to but never used, o is assigned to but never used and status is assigned to but never used.No warry about that bcos you assign x and o as integer and used as string so this warning is come,i dont know whats your logic but if you want to remove this warning than use this int x,and o.Also play2 is never used so put condition like bellow
if (play1 == true)
{
btnSendNotification.Text = "x";
play1 = false;
}
else if(play2==true)
{
btnSendNotification.Text = "o";
play2 = false;
play1 = true;
}
Hope this is help you if not get solution than comment me.

Categories

Resources