namespace PostingQueueMonitoring_V2
{
public partial class Helper : Form
{
public Helper()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(input_richTextBox1.Text.Trim()))
{
MessageBox.Show("Please input text...");
return;
}
string[] myText = new string[] {input_richTextBox1.Text};
foreach (string element in myText)
{
output_richTextBox2.Text = element;
System.Console.WriteLine(output_richTextBox2.Text);
}
System.Console.WriteLine();
//output_richTextBox2.Rtf = input_richTextBox1.Rtf;
}
private void Helper_Resize(object sender, EventArgs e)
{
this.MinimumSize = new Size(736, 466);
this.MaximumSize = new Size(736, 466);
}
}
}
How to update the code that will format the text from input_richTextBox1 and shown in output_richTextBox2. Example below.
input_richTextBox1:
Apple\n
Banna\n
Coconut\n
Durian\n
Orange
Expected Result into output_richTextBox2
('Apple',
'Banna',
'Coconut',
'Durian',
'Orange')
Please try this.
private void button1_Click(object sender, EventArgs e)
{
if (String.IsNullOrEmpty(richTextBox1.Text.Trim()))
{
MessageBox.Show("Please input text...");
return;
}
var textValues = richTextBox1.Text.Split('\n').Select(txt => $"'{txt}'");
var concatValues = string.Join(",", textValues);
richTextBox2.Text = concatValues;
System.Console.WriteLine();
}
Related
This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 4 years ago.
I am trying to get read data from rs 485 communication interface and write in the textbox but i am not getting the data from this code. I am working on water measurement and I am beginner in c#. I have seen similar question like this but unable to get the answer. The data format is like this. D014802,+000.042,+000.082,003680,+000805.66,+025.25,0193FA,0.99697,0000,B7C9
Help me out .
public partial class MainForm : Form
{
SerialPort aSerialPort;
InputRegister mobjGlobalform2;
Form3 mobjGlobalform3;
LoginForm AdminLogin = new LoginForm();
//Form6 softwareVersion = new Form6();
bool isUserMode = true;
public MainForm()
{
InitializeComponent();
getAvailablePorts();
}
private void btn_close_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
InputRegister mobjform2 = new InputRegister();
if (checkBox1.Checked)
{
mobjGlobalform2 = mobjform2;
mobjform2.Show();
if(isUserMode==true)
{
mobjform2.groupbox_form2Takuwa.Hide();
mobjform2.Height = 215;
mobjform2.Width = 575;
}
}
else
{
mobjGlobalform2.Close();
//mobjform2.Close();
}
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
Form3 mobjform3 = new Form3();
if (checkBox2.Checked)
{
mobjGlobalform3 = mobjform3;
mobjform3.Show();
if(isUserMode==true)
{
mobjform3.groupbx_form3takuwamode.Hide();
mobjform3.Height = 254;
mobjform3.Width = 407;
}
}
else
{
mobjGlobalform3.Close();
//mobjform2.Close();
}
}
private void timer1_Tick_1(object sender, EventArgs e)
{
label13.Text = DateTime.Now.ToString();
// splitContainer1.Panel2.Controls.Add(label13);
if(AdminLogin.isAdminMode)
{
lbl_AdminLogout.Show();
isUserMode = false;
}
}
private void btn_Clear_Click(object sender, EventArgs e)
{
txtbox_ShowData.Text = String.Empty; // clear the data from text box
}
private void MenuStrip_AdminLogin_Click(object sender, EventArgs e)
{
//Form5 mobjform5 = new Form5();
AdminLogin.Show();
}
private void lbl_AdminLogout_Click(object sender, EventArgs e)
{
AdminLogin.isAdminMode = false;
isUserMode = true;
lbl_AdminLogout.Hide();
MessageBox.Show("Logout Successful");
}
private void btn_SoftwareVersion_Click(object sender, EventArgs e)
{
//softwareVersion.Show();
Form6 Versionform6 = new Form6();
Versionform6.Height = 272;
Versionform6.Width = 507;
Versionform6.Show();
}
private void btn_connectaddress_Click(object sender, EventArgs e)
{
Setting addressconnectform6 = new Setting();
addressconnectform6.Show();
if(isUserMode == true)
{
addressconnectform6.groupbx_takuwaform7.Hide();
addressconnectform6.Height = 257;
addressconnectform6.Width = 657;
}
else
{
addressconnectform6.groupbx_takuwaform7.Show();
}
}
#region combox
// port show in combobox
private void getAvailablePorts()
{
string[] ports = SerialPort.GetPortNames();
cbbx_comport.Items.Clear();
foreach (string comport in ports)
{
cbbx_comport.Items.Add(comport);
}
}
#endregion
private void btn_Connect_Click(object sender, EventArgs e)
{
initializeSensor();
aSerialPort.DataReceived += new SerialDataReceivedEventHandler(Rs485DataReceivedEventHandler);
}
private void Rs485DataReceivedEventHandler(object sender, SerialDataReceivedEventArgs e)
{
SerialPort sData = sender as SerialPort;
string recvData = sData.ReadLine();
this.Invoke((MethodInvoker)delegate { DataReceived(recvData); });
}
private void initializeSensor()
{
aSerialPort = new SerialPort(cbbx_comport.Text);
aSerialPort.BaudRate = 38400;
aSerialPort.Parity = Parity.None;
aSerialPort.StopBits = StopBits.One;
aSerialPort.DataBits = 8;
if (aSerialPort.IsOpen == false)
{
try
{
aSerialPort.Open();
//aSerialPort.WriteLine("c"); //clear
//aSerialPort.WriteLine("o");
}
catch { }
}
}
private void DataReceived(string recvData)
{
txtbx_sensorData.Text = recvData;
}
You are instantiating a local variable, not the field.
Instead of
SerialPort aSerialPort = new SerialPort(cbbx_comport.Text);
Try
aSerialPort = new SerialPort(cbbx_comport.Text);
So, I got kind of stuck over my head while I tried to program something new.
I'm trying to add objectBeer_pluche or objectBeer_Elektro to my OBJberenlijst on the Beren Main form from the details Form, so I can add both instances of 2 classes to the same list.
I'm not even sure this is possible by the way. So, I would like feedback if what I am trying to do is possible to start with. I already figured VOID is not right but I am really clueless here.
This is my main beren.cs form with an OBJberenlist, that's where I try to add objectBeer_pluche or objectBeer_Elektro into it:
public partial class Beren : Form
{
public interface Berenlijst { }
public List<Berenlijst> OBJberenLijst = new List<Berenlijst>();
public Beren()
{
InitializeComponent();
}
private void Beren_Load(object sender, EventArgs e)
{
}
private void BTNToevoegen_Click(object sender, EventArgs e)
{
this.Hide();
Details Details = new Details();
if (Details.ShowDialog(this) == DialogResult.OK)
{
OBJberenLijst.Add(Details.getdetails());
}
Details.Close();
Details.Dispose();
}
public void LijstLaden()
{
foreach(Beer berenobject in OBJberenLijst)
{
LST_beren.Items.Add(berenobject.Naam);
}
}
}
}
from this form called details.cs
public partial class Details : Form
{
public Details()
{
InitializeComponent();
BTN_toevoegen.DialogResult = DialogResult.OK;
BTN_cancel.DialogResult = DialogResult.Cancel;
}
private void Details_Load(object sender, EventArgs e)
{
RDB_pluche.Checked = true;
BTN_ok.Enabled = false;
}
private void RDB_pluche_CheckedChanged(object sender, EventArgs e)
{
PANEL_pluche.Visible = true;
PANEL_elektro.Visible = false;
}
private void RDB_elektro_CheckedChanged(object sender, EventArgs e)
{
PANEL_pluche.Visible = false;
PANEL_elektro.Visible = true;
}
private void BTN_toevoegen_Click(object sender, EventArgs e)
{
open_foto.Filter = "jpg (*.jpg)|*.jpg|bmp(*.bmp)|*.bmp|png(*.png)|*.png";
if (open_foto.ShowDialog() == System.Windows.Forms.DialogResult.OK && open_foto.FileName.Length > 0)
{
TXT_adres.Text = open_foto.FileName;
PIC_beer.Image = Image.FromFile(open_foto.FileName);
}
}
private void BTN_ok_Click(object sender, EventArgs e)
{
}
public void getdetails()
{
if (RDB_pluche.Enabled == true)
{
Pluche_Beer objectBeer_pluche = new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");
}
else
{
Elektronische_Beer objectBeer_Elektro = new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());
}
}
private void Details_MouseMove(object sender, MouseEventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox textBox = c as TextBox;
if (textBox.Text != string.Empty)
{
BTN_ok.Enabled = true;
}
}
}
}
}
}
The problem is between this line...
OBJberenLijst.Add(Details.getdetails());
...and this line.
public void getdetails()
List.Add() requires an object to add, but getdetails() returns void. You probably want to change getdetails() to something like the following:
public Berenlijst getdetails()
{
if (RDB_pluche.Enabled == true)
{
return new Pluche_Beer(TXTNaam_pluche.Text, open_foto.FileName, "(Wasprogramma: " + TXT_wasprogramma.ToString() + " Graden Celsius");
}
return new Elektronische_Beer(TXTNaam_elekro.Text, open_foto.FileName, "aantal Batterijen: " + CMBOBatterijen.ToString());
}
Hopefully Pluche_Beer and Elektronisch_Beer inherent from Berenlijst. Otherwise you'll have to revise your logic in a broader way.
How can I click a Button(to generate a color) in one form and change text color in a RichTextBox in a another form? Thanks in advance. (Newbie trying to understand C#)
Some code:
1.WinForm
public delegate void ColorWindowEvent(Object sender, SecondrWindowEventArgs e);
public partial class ColorWindow : Form
{
public event ColorWindowEvent myEventHandler;
public ColorWindow ()
{
InitializeComponent();
}
public void MyEvent(Object sender, ColorWindowEventArgs e)
{
string s = "";
myEventHandler(this, new SecondWindowEventArgs(s));
}
private void btnRed_Click(object sender, EventArgs e)
{
Color c = Color.Red;
string s = c.ToString();
this.Close();
}
private void btnBlue_Click(object sender, EventArgs e)
{
Color c = Color.Blue;
string str = c.ToString();
this.Close();
}
private void btnGreen_Click(object sender, EventArgs e)
{
Color c = Color.Green;
string s = c.ToString();
this.Close();
}
}
public class SecondWindowEventArgs : EventArgs
{
private string s;
public SecondWindowEventArgs(string _s)
{
s = _s;
}
#region
public string S
{
get;
set;
}
#endregion
}
2.WinForm
public delegate void SecondWindowEvent(Object sender, FirstWindowEventArgs e);
public partial class SecondWindow : Form
{
public event SecondWindowEvent myEventHandler;
private string s;
public SecondWindow(String _s)
{
s = _s;
InitializeComponent();
}
public void MyEvent(Object sender, FirstWindowEventArgs e)
{
string str = rtf2.Text;
if (str != null)
{
myEventHandler(this, new FirstWindowEventArgs(str));
}
}
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void rtf2_TextChanged(object sender, EventArgs e)
{
if (myEventHandler != null)
{
myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length - 1)));
rtf2.ForeColor = Color.FromName(e.ToString());
}
}
private void btnClearText_Click(object sender, EventArgs e)
{
rtf2.Text = " ";
}
private void rtf2_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
{
FargeVindu fargeVindu = new FargeVindu();
fargeVindu.minEventHandler += new FargeVinduEvent(fargeVindu_minEventHandler);
fargeVindu.Show();
}
else if (e.KeyData == Keys.Delete)
{
}
}
protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
{
rtf2.ForeColor = Color.FromName(s);
}
Random random = new Random();
private void SecondWindow_Load(object sender, EventArgs e)
{
lblText.ForeColor = Color.FromArgb(random.Next(255),
random.Next(255), random.Next(255));
}
public Color getColor
{
get;
set;
}
}
3.WinForm
public partial class FirstWindow : Form
{
public FirstWindow()
{
InitializeComponent();
}
private void btnQuit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClick_Click(object sender, EventArgs e)
{
string str = " ";
SecondWindow secondWindow = new SecondWindow (str);
secondWindow.myEventHandler += new SecondWindowEvent(secondWindow_myEventHandler);
secondWindow.Show();
}
protected void secondWindow_myEventHandler(object sender, FirstWindowEventArgs e)
{
rtf1.AppendText(String.Format(e.Tekst));
}
public void btnClearText_Click(object sender, EventArgs e)
{
rtf1.Text = " ";
}
}
Clarification from comments
I would like the Form to Change the color on close after the button is clicked. This is what I tried:
private void btnRed_Click(object sender, EventArgs e)
{
Color c = Color.Red;
string s = c.ToString();
this.Close();
}
To answer your question, create a property on your Color Form that is set by your button you can then read it after the Form is closed when it returns from your ShowDialog statement.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
if(frm2.ShowDialog() == DialogResult.OK)
{
//this.BackColor=frm2.getColor; helps if I read the question more closely
richTextBox1.SelectionColor = frm2.getColor;
}
}
}
Form2
public partial class Form2 : Form
{
public Color getColor { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
getColor = Color.Red;
DialogResult = DialogResult.OK;
}
}
After playing with the code that you posted and added some missing handlers, it looks like the text of your color is coming in the Format of Color [Red] ColorFromName has no idea how to parse that so you will need to get the actual color name by using String.Split Something like this.
protected void ColorWindow_myEventHandler(object sender, SecondWindowEventArgs e)
{
rtf2.ForeColor = Color.FromName(e.S.Split(new string[]{"[","]"},StringSplitOptions.None)[1]);
}
I also noticed that you are setting your rtf2.ForeColor everytime that your text changes, I removed it and am now able to change the ForeColor of the RichText box. I would be a lot easier/cleaner IMHO if you just passed the actual Color Object instead of changing it to a string and back.
This is the modified TextChanged Method note the commented out rtf2.ForeColor statement it does not belong there.
private void rtf2_TextChanged(object sender, EventArgs e)
{
if (myEventHandler != null)
{
myEventHandler(this, new FirstWindowEventArgs(rtf2.Text.Substring(rtf2.Text.Length -1)));
// rtf2.ForeColor = Color.FromName(e.ToString());
}
}
Why does this not work?
There are two Radio buttons.
If I check radio button1 I should see gallery1.
If I check Radio button2 I should see galery2.
And button1"<<" button2 ">>" is meant to view gallery : back, forward.
http://s1v3.irc.lv/files/1/0/0/454/KddPouO7.png
http://s1v2.irc.lv/files/1/0/0/454/aHPiAt4k.png
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
List<string> galerija1;
List<string> galerija2;
List<string> aktualaGalerija;
int tekosaPozicija;
string galerija1 = new List<string>(){ "C:\\Galerija1\\aq1.png", "C:Galerija1\\aq2.png"};
string galerija2 = new List<string>(){ "C:Galerija2\\dr1.png", "C:Galerija2\\dr1.png"};**
///// He don`t like this place :( Someone can help?
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
aktualaGalerija = galerija1;
tekosaPozicija = 0;
IeladeAktualoBildi();
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
aktualaGalerija = galerija2;
tekosaPozicija = 0;
IeladeAktualoBildi();
}
private void IeladeAktualoBildi()
{
string aktualaBilde = aktualaGalerija[tekosaPozicija];
}
private void button2_Click(object sender, EventArgs e)
{
if (tekosaPozicija == aktualaGalerija.Count - 1)
return;
tekosaPozicija++;
IeladeAktualoBildi();
}
private void button1_Click(object sender, EventArgs e)
{
if (tekosaPozicija == 0)
return;
tekosaPozicija--;
IeladeAktualoBildi();
}
}
}
Replace
List<string> galerija1;
string galerija1 = new List<string>(){ ... };
with
List<string> galerija1 = new List<string> { ... };
The same with galerija2. Your code declares two fields with the same name, but different types.
I've looked up many question on how to do this, but none of them seem to work. I have a ListView on my main view, and I'm populating it from adding data to it from another form. On the other form, I have a couple strings from text fields I'd like to pass to it. Here's my code:
// Add new booking form:
namespace Booker
{
public partial class newBookingForm : Form
{
public newBookingForm()
{
InitializeComponent();
}
private void NewBookingForm_Load(object sender, EventArgs e)
{
roomField.Focus();
}
private void newBookingButton_Click(object sender, EventArgs e)
{
// Add to ListView
// Add to Parse
string room = this.roomField.Text;
string date = this.datePicker.Value.ToShortDateString();
string time = this.timeField.Text;
string person = "<username>"; // Get current Parse user
Booker booker = new Booker();
string[] array = new string[4] { room, date, time, person };
booker.UpdatingListView(array);
this.Hide();
}
}
}
// ListView form:
namespace Booker
{
public partial class Booker : Form
{
delegate void MyDelegate(string[] array);
public Booker()
{
InitializeComponent();
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
Environment.Exit(0);
}
private void Booker_Load(object sender, EventArgs e)
{
listView.View = View.Details;
listView.AllowColumnReorder = false;
}
/* Methods for the Booker form */
private void newBookingButton_Click(object sender, EventArgs e)
{
newBookingForm nbf = new newBookingForm();
nbf.Show();
}
public void UpdatingListView(string[] array)
{
if (this.listView.InvokeRequired)
this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
else
{
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(array[1]);
this.listView.Items.Add(lvi);
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Error - no action being sent
}
private void helpButton_Click(object sender, EventArgs e)
{
// Display help panel
}
private void contactButton_Click(object sender, EventArgs e)
{
// Open panel/email
}
private void newBooking_Click(object sender, EventArgs e)
{
newBookingButton_Click(sender, e);
//string[] row = { "Meeting room", "April 11, 2013", "12:00PM-1:00PM", "Ryan" };
//var listViewItem = new ListViewItem(row);
//listView.Items.Add(listViewItem);
}
Try this in UpdatingListView:
ListViewItem lvi = new ListViewItem(array[0],0);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
lvi.SubItems.Add(array[3]);
this.listView.Items.Add(lvi);
This will create a column in the first position of your ListView with the 4 items of your array as rows in that column.
To add items from another class, just access the other class' method to add from Program. Like this:
// Class we're adding from:
string name = "Ryan";
string site = "imryan.net";
string[] array = new string[2] = { name, site };
Program.classWithListView.UpdatingListView(array);
// Class we're adding to:
public void UpdatingListView(string[] array)
{
if (this.listView.InvokeRequired)
{
this.listView.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
} else {
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
this.listView.Items.Add(lvi);
}
}
The string array will be added to the ListView as SubItems.