custom single line messagebox [duplicate] - c#

This question already has answers here:
How to create a custom MessageBox?
(4 answers)
Closed 9 years ago.
I'm trying to make a custom message box for my application. The problem is, I want to code it in a way so that I can use it as regular message box.
MyCustomBox("My Message");
intead of doing
FormMessage frm = new FormMessage();
frm.message = "My Message";
frm.show();
How can I accomplish this? Thanks!

You can add a static method to FormMessage class
public static void ShowBox(string message)
{
using (FormMessage frm = new FormMessage())
{
frm.Message = message;
frm.ShowDialog();
}
}
And then
FormMessage.ShowBox("My Message");

Create the form with the appropriate controls, etc. Then add a static method to the class that handles all the messy bits - creating an instance (if necessary), setting properties, etc.
I wish I could write more on this, but it's pretty simple stuff. Just call MyCustomBox.ShowMessage() or whatever you call the static method.

Related

Readonly property in multiple texbox [duplicate]

This question already has answers here:
Make all Controls on a Form read-only at once
(7 answers)
Closed 4 years ago.
I'm making a Windows Form Application.
Well, C# programming is new to me and maybe this is a silly question, but how I can apply ReadOnly property in multiple textbox elements? I tried this code:
public void DoReadOnly(Control control){
foreach (Control c in control.Controls){
if (c.Controls != null && c.Controls.Count > 0){
DoReadOnly(c);
}
else if (c is TextBox){
(c as TextBox).ReadOnly = true;
}
}
}
public void getData(){
DoReadOnly(this.Form);
}
The trouble is that I don't know which parameter I should put when I'm call doReadOnly's function. Visual Studio doesn't recognize this.Form like a valid argument.
To call use this.
DoReadOnly(this)
If the method is on Form class
Pass only 'this', that object is your current form
public void getData(){
DoReadOnly(this);
}
Use DoReadOnly(this); instead of DoReadOnly(this.Form);
another thing, why getDate if you are going to put or change a propity, use setData

How to write to a textbox that is in another Form? [duplicate]

This question already has answers here:
Passing a value from one form to another form
(9 answers)
Writing to a textbox on a separate form (C#)
(4 answers)
Closed 5 years ago.
I have a .NET application which has 2 forms: inside Form1 there is all the application stuff, while inside LogForm there is only a readonly textbox. I want to print some text to this textbox inside LogForm from Form1, while Form1 is performing all the work.
I open my LogForm via the
LogForm logForm = new LogForm();
logForm.Show();
But then? How can I do that?
You must have the reference to this TextBox.
Put your access modifier to public in your visual studio form designer and access your TextBox by logForm1.YourTextBox.Text += "new line \r\n";
You can make your LogForm to accept Arguments on initialization:
string ValueFromForm1 = null;
public LogForm(string input)
{
ValueFromForm1 = input;
}
The on Form_Load set the value of textbox:
TextBox1.Text = ValueFromForm1 ;
Create a public variable in Form1 and call in LogForm
Form 1
public static string logformtext;
logformtext="Required text"; //Value which you want to pass to LogForm
LogForm
TextBox1.Text=Form1.logformtext;
Either you can set the text in the constructor of LogForm:
public LogForm(string text)
{
InitializeComponent();
textBox1.Text = text;
}
or you can set the modifier of the TextBox to Internal (or even Public) on the Designer and then access it from Form1 like this:
logForm.textBox1.Text = "Your text";
But keep in mind that while your program is working, the text will not show up on your LogForm, unless you repaint it, or use a BackgroundWorker to have the work be done in a different thread.

How to open new form in C# with button name? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
i'm making a little application and i don't know how to open new window of app with parameters of clicked button.
For example: If i click to hydrogen, i want to open form called prvek, which will display informations about it.
Sorry for my bad English. Here's screenshot of main window:
Main window
Opening a form in Windows Forms simply involves creating an instance of that form and calling .Show() on that instance. For example:
var someForm = new SomeForm();
someForm.Show();
If you want to pass values to that form, you can set them as constructor arguments. For example, in SomeForm:
public SomeForm(int someValue)
{
// do something with someValue
}
Then when you create it:
var someForm = new SomeForm(aValue);
someForm.Show();
Or if the values aren't necessarily required, but you happen to have them available at this time, maybe set them as properties. In SomeForm:
public int SomeValue { get; set; }
Then when you create it:
var someForm = new SomeForm();
someForm.SomeValue = aValue;
someForm.Show();
or:
var someForm = new SomeForm { SomeValue = aValue };
someForm.Show();
Where you get your values, of course, is up to you. I'm not sure what you mean by "parameters of the clicked button". But in the click event there should be an object sender which is a reference to the UI element that triggered the event.
So, for example, if you want a property from the Button that was clicked, you can cast sender to Button and read its information. Something like this:
var buttonText = ((Button)sender).Text;
You should be able to give your second form, prvek, a property that you can set from your first. For example:
public string Element { get; private set; };
Then, in your button_onClick method you should be able to do the following:
ElementForm myForm = new ElementForm(); //Whatever the class name is of your second form
myForm.Element = ((Button)this).Name; //Get the name of the button
myForm.Show();
In your second form's constructor or initializer method, you'll want to set the title of the form:
public ElementForm()
{
InitializeComponent()
this.Text = Element;
}

List Trigger Event Error [duplicate]

This question already has answers here:
C#: Inheritance Problem with List<T>
(4 answers)
Closed 8 years ago.
i have a code:
public class _clientSockets<Socket> : List<Socket>
{
public event EventHandler OnAdd;
public void Add(Socket s)
{
if (OnAdd != null)
OnAdd(this, null);
base.Add(s);
}
}
that every client that will try to connect to the server will be stored on the list. And i tried to call it using this code:
_clientSockets<Socket> s = new _clientSockets<Socket>();
s.OnAdd += new EventHandler(clientAlerted);
s.Add(socket);
But the problem is got an error something like:
" MainWindow._clientSockets.Add(Socket)' hides inherited member 'System.Collections.generic.List.Add(Socket)'. Use the new keyword if hiding was intended. "
How can i solve this stuff? :)
How can i solve this?
Either don't call your method Add or don't inherit from List. You may find that composition works better than inheritance here, since clients could just treat your class as a plain List<T> and bypass your "new" Add method.

Curious as how to get a console inside of a windows form [duplicate]

This question already has answers here:
Embedding a DOS console in a windows form
(3 answers)
Closed 10 years ago.
Here's an axample
Just curious, I'm new with C#, but I decided to use it to develop my server for my Java game simply because I wanted a nice server-gui, but I'm starting to realize this would be just as easy to do with Swing...
I did a similar project over the weekend, I used this link to forward all Console data to a textbox. You could use any similar control with minor tweaks.
public class TextBoxStreamWriter : TextWriter
{
TextBox _output = null;
public TextBoxStreamWriter(TextBox output)
{
_output = output;
}
public override void Write(char value)
{
base.Write(value);
_output.AppendText(value.ToString()); // When character data is written, append it to the text box.
}
public override Encoding Encoding
{
get { return System.Text.Encoding.UTF8; }
}
}
All of the Write(...) and WriteLine(...) trickle down to Write(char) so it's the only method that you need to override.
My example required the form to also have a public TextBox property which exposed the private TextBox inside the form.
TextWriter consoleRedirect = new Tools.TextBoxStreamWriter(consoleForm.TxtOuputDisplay);
Console.SetOut(consoleRedirect);
Well I already wrote this up before I saw the previous comment. But since I took the time I will post it anyway.
public void WriteLog(TextBox tb ,string log)
{
tb.AppendText(log + "\n");
}
private void button1_Click(object sender, EventArgs e)
{
WriteLog(textBox1, "[App]: This is a log string");
WriteLog(textBox1, "[App]: Another log string");
WriteLog(textBox1, "[App]: Yet another etc etc.");
}
Where textBox1 is a Multi-line textbox with a black backcolor and blue foreground text.
The above comment is a more elegant solution but this will get you by if you want something quick and easy. I dont have enough rep to post an image inline but here is what it looks like. http://imageshack.us/photo/my-images/198/logwindow.jpg/

Categories

Resources